文章目录
1. 维基百科词条EDA数据可视化
1.1 查看数据
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import re
train = pd.read_csv('train_1.csv').fillna(0) #补充
train.head()
1.2 转换数据格式
- 数据为浮点型,可转为int类型,数据占内存小
for col in train.columns[1:]:
train[col] = pd.to_numeric(train[col],downcast='integer')
train.head()
1.3 统计各国家的点击量
def get_language(page):
res = re.search('[a-z][a-z].wikipedia.org',page)
#print (res.group()[0:2])
if res:
return res.group()[0:2]
return 'na'
train['lang'] = train.Page.map(get_language)
from collections import Counter
#统计不同个数
print(Counter(train.lang))
1.4 按国家对数据进行分类
lang_sets = {}
lang_sets['en'] = train[train.lang=='en'].iloc[:,0:-1]
lang_sets['ja'] = train[train.lang=='ja'].iloc[:,0:-1]
lang_sets['de'] = train[train.lang=='de'].iloc[:,0:-1]
lang_sets['na'] = train[train.lang=='na'].iloc[:,0:-1]
lang_sets['fr'] = train[train.lang=='fr'].iloc[:,0:-1]
lang_sets['zh'] = train[train.lang=='zh'].iloc[:,0:-1]
lang_sets['ru'] = train[train.lang=='ru'].iloc[:,0:-1]
lang_sets['es'] = train[train.lang=='es'].iloc[:,0:-1]
print(lang_sets)
sums = {}
for key in lang_sets:
sums[key] = lang_sets[key].iloc[:,1:].sum(axis=0) / lang_sets[key].shape[0]#各个国家每天点击量的平均值
1.5 统计各个国家每天平均点击量的折线图
days = [r for r in range(sums['en'].shape[0])] #天数
fig = plt.figure(1,figsize=[10,10])
plt.ylabel('Views per Page')
plt.xlabel('Day')
plt.title('Pages in Different Languages')
labels={'en':'English','ja':'Japanese','de':'German',
'na':'Media','fr':'French','zh':'Chinese',
'ru':'Russian','es':'Spanish'
}
for key in sums:
plt.plot(days,sums[key],label = labels[key] )
plt.legend()
plt.show()
1.6 绘制en(English)这个国家某个词条600天的点击量折线图
def plot_entry(key,idx):
data = lang_sets[key].iloc[idx,1:]
fig = plt.figure(1,figsize=(10,5))
plt.plot(days,data)
plt.xlabel('day')
plt.ylabel('views')
plt.title(train.iloc[lang_sets[key].index[idx],0])
plt.show()
idx = [1, 5, 10, 50, 100, 250,500, 750,1000,1500,2000,3000,4000,5000]
for i in idx:
plot_entry('en',i)
- 第一条图片展示
1.7 统计每个国家各个词条的总量
npages = 5
top_pages = {}
for key in lang_sets:
print(key)
sum_set = pd.DataFrame(lang_sets[key][['Page']])
sum_set['total'] = lang_sets[key].sum(axis=1)
sum_set = sum_set.sort_values('total',ascending=False)
print(sum_set.head(10))
top_pages[key] = sum_set.index[0]#保存第一行的索引
print('\n\n')
1.8 绘制每个国家总600天点击量最多的词条的变化折线图
for key in top_pages: #遍历国家
print(key)
fig = plt.figure(1,figsize=(10,5))
cols = train.columns
cols = cols[1:-1]
print(cols)
data = train.loc[top_pages[key],cols] #索引,列
plt.plot(days,data)
plt.xlabel('Days')
plt.ylabel('Views')
plt.title(train.loc[top_pages[key],'Page'])
plt.show()
- English600天点击量最多的词条的变化折线图: