社交媒体用户行为分析代码
时间: 2024-06-23 11:00:42 浏览: 216
社交媒体用户行为分析通常涉及到数据收集、处理、挖掘和可视化,目的是理解用户的兴趣、活动模式、互动行为等。以下是一个简单的Python代码框架,使用Pandas库进行数据预处理,然后可能用到像NetworkX或Gephi进行网络分析,以及matplotlib或seaborn进行可视化:
```python
# 导入必要的库
import pandas as pd
import numpy as np
from collections import Counter
import networkx as nx
import matplotlib.pyplot as plt
# 1. 数据加载和清洗
def load_data(file_path):
df = pd.read_csv(file_path, parse_dates=['timestamp'])
# 进行缺失值处理、异常值检测等
df = df.dropna(subset=['user_id', 'activity'])
return df
# 2. 数据统计与分析
def user_behavior_analysis(df):
user_activity_counts = df['activity'].value_counts()
most_common_activities = user_activity_counts.head(10)
# 3. 创建社交网络图(如果有用户间的互动)
if 'friend_id' in df.columns:
G = nx.from_pandas_edgelist(df, source='user_id', target='friend_id')
# 计算度中心性等网络指标
degree_centrality = nx.degree_centrality(G)
# 4. 可视化结果
fig, axs = plt.subplots(2, 1, figsize=(10, 8))
most_common_activities.plot(kind='bar', ax=axs)
axs.set_title('Top 10 User Activities')
if 'G' in locals():
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, with_labels=True)
nx.draw_networkx_edges(G, pos, alpha=0.5)
axs.axis('off')
axs.set_title('Social Network of Users')
# 示例
data_file = 'social_media_data.csv'
df = load_data(data_file)
user_behavior_analysis(df)
```
阅读全文