你所需要的,不仅仅是一个好用的代理。
「DATA CHAT」是科赛网打造的品牌活动。拥有最强大脑、最独特视角的数据分析狂热者,将针对体育、影视、音乐、财经四大领域内最生活的话题,开展有趣又烧脑的数据分析探索之旅。
首期我们 从NBA话题切入 ,在官网发布了超全的NBA数据集(所有球员&球队的常规赛、季后赛,教练执教、球员各赛季薪金的数据),N位网友在线根据自己的兴趣,发布了数据分析项目,切入点很多样,也很有意思。如, 王者荣耀 — NBA数据分析 [增加梅西评分模型] 、 乔科詹库之全方位分析 ……
这里PO其中一份原创作品@大野人007 ,感兴趣的朋友也可直接登录kesci.com,Fork过来在K-Lab上开展个人分析。附:原贴链接。
说明:关于K-Lab。
K-Lab是科赛网重点打造的在线数据分析协作平台。 它涵盖了Python、R等主流语言,完成了90%以上数据分析&挖掘相关库的部署(如题主所提到的pandas, numpy, matplotlib),免去了本地搭建环境的烦恼,实现了即刻线上动手做分析项目。
1)数据维度
2)数据处理
!ls ../input/NBAdata/
<img src="https://pic4.zhimg.com/v2-0bbbbc92f1a30fae3d190b491d6f39bf_b.png" data-rawwidth="1020" data-rawheight="113" class="origin_image zh-lightbox-thumb" width="1020" data-original="https://pic4.zhimg.com/v2-0bbbbc92f1a30fae3d190b491d6f39bf_r.png">
# 导入必要的包.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
import numpy as np
import pylab
%matplotlib inline
warnings.filterwarnings('ignore')
读取球员场均数据
player_avg = pd.read_csv('avg.csv') player_avg.head()
展示要分析的数据
pd.set_option('display.max_columns',30)
player_avg[(player_avg['姓名'] == 'Kawhi Leonard') & (player_avg['赛季'] == '16--17')]
<img src="https://pic2.zhimg.com/v2-056dc93de42f5a2d974a3056a5fe1331_b.png" data-rawwidth="1301" data-rawheight="216" class="origin_image zh-lightbox-thumb" width="1301" data-original="https://pic2.zhimg.com/v2-056dc93de42f5a2d974a3056a5fe1331_r.png">
pd.set_option('display.max_columns',50)
L_1617_avg = player_avg[(player_avg['姓名'] == 'LeBron James') & (player_avg['赛季'] == '16--17')]
L_1617_avg
对比分析及数据可视化
class Radar(object):
n = 1
angles =None
def __init__(self, fig, titles, labels, rect=None):
if rect is None:
rect = [0.05, 0.05, 0.95, 0.95]
self.n = len(titles)
self.angles = np.arange(90, 90+360, 360.0/self.n)
self.axes = [fig.add_axes(rect, projection="polar", label="axes%d" % i)
for i in range(self.n)]
self.ax = self.axes[0]
self.ax.set_thetagrids(self.angles, labels=titles, fontsize=14)
for ax in self.axes[1:]:
ax.patch.set_visible(False)
ax.grid("off")
ax.xaxis.set_visible(False)
for ax, angle, label in zip(self.axes, self.angles, labels):
ax.set_rgrids(range(1, 6), angle=angle, labels=label)
ax.spines["polar"].set_visible(False)
ax.set_ylim(0, 5)
def angle(self, values, *args, **kw):
return np.deg2rad(np.r_[self.angles]),np.r_[values],values
def plot(self, values, *args, **kw):
angle = np.deg2rad(np.r_[self.angles, self.angles[0]])
values = np.r_[values, values[0]]
self.ax.plot(angle, values, *args, **kw)
titles_ = ['score','shoot','rebound','assist','three','penalty','steal','block']
titles = ['得分','投篮','篮板','助攻','三分','罚球','抢断','盖帽']
# titles = list("ABCDE")
<img src="https://pic2.zhimg.com/v2-782945bd3a9ef01a9166c2b452aa89c5_b.png" data-rawwidth="831" data-rawheight="752" class="origin_image zh-lightbox-thumb" width="831" data-original="https://pic2.zhimg.com/v2-782945bd3a9ef01a9166c2b452aa89c5_r.png">
我们发现:
赢
输
以上分析较宏观,不能反映事物的本质。下面从微观角度(每场比赛的数据)进行分析。
角度1:基于篮板球
# data_statistics函数主要是方便categoricl型的数据的统计显示,方便后续绘图使用
def data_statistics(Kawhi_season1617, Lebron_season1617, name):
Kawhi_season1617_ = pd.DataFrame(Kawhi_season1617.groupby(name)['球员'].count())
Kawhi_season1617_.columns = ['K_次数']
Kawhi_season1617_.reset_index(inplace=True)
Lebron_season1617_ = pd.DataFrame(Lebron_season1617.groupby(name)['球员'].count())
Lebron_season1617_.columns = ['L_次数']
Lebron_season1617_.reset_index(inplace=True)
data = pd.merge(Lebron_season1617_,Kawhi_season1617_,on = name , how ='outer')
data = data.fillna(0)
data = data.sort_values(name)
return data
用seaborn做柱状图可视化
rebounds['K_次数'] = rebounds['K_次数'] * -1
plt.figure(figsize=[16,6])
sns.barplot(x = '篮板', y = 'L_次数', data = rebounds, color='red')
sns.barplot(x = '篮板',y = 'K_次数', data = rebounds, color ='blue')
<img src="https://pic4.zhimg.com/v2-f5f92845993c98a3b26bc97ac330799b_b.png" data-rawwidth="996" data-rawheight="378" class="origin_image zh-lightbox-thumb" width="996" data-original="https://pic4.zhimg.com/v2-f5f92845993c98a3b26bc97ac330799b_r.png">
红色:詹姆斯,蓝色:伦纳德
可以看出:
角度2:基于得分
score['K_次数'] = score['K_次数'] * -1
plt.figure(figsize=[16,6])
sns.barplot(x = '得分', y = 'L_次数', data = score, color='red')
sns.barplot(x = '得分',y = 'K_次数', data = score, color ='blue')
<img src="https://pic4.zhimg.com/v2-543f64ec8197d6348aa622a2de9b421b_b.png" data-rawwidth="993" data-rawheight="395" class="origin_image zh-lightbox-thumb" width="993" data-original="https://pic4.zhimg.com/v2-543f64ec8197d6348aa622a2de9b421b_r.png">
红色:詹姆斯,蓝色:伦纳德
用seaborn做violin图可视化
plt.figure(figsize= [12,5])
total = pd.concat([Kawhi_season1617,Lebron_season1617])
total['Is_Kawhi'] = 0
total.loc[total['球员'] == 'Kawhi Leonard','Is_Kawhi'] = 1
total['A'] = 0
sns.violinplot(x= 'A' , y = '得分', hue = 'Is_Kawhi', data = total, split=True)
<img src="https://pic3.zhimg.com/v2-86834a22b8bbeda6cb0b1a0ec51bf1ae_b.png" data-rawwidth="809" data-rawheight="347" class="origin_image zh-lightbox-thumb" width="809" data-original="https://pic3.zhimg.com/v2-86834a22b8bbeda6cb0b1a0ec51bf1ae_r.png">
绿色:伦纳德,蓝色:詹姆斯
可以看出:
0 - 15分 定为档1
15 - 19 定为档2
20 - 24 定为档3
25 - 29 定为档4
30 - 定为档5
<img src="https://pic1.zhimg.com/v2-89e9cbddb5c16c025270c1590d9754c8_b.png" data-rawwidth="1010" data-rawheight="380" class="origin_image zh-lightbox-thumb" width="1010" data-original="https://pic1.zhimg.com/v2-89e9cbddb5c16c025270c1590d9754c8_r.png">
红色:詹姆斯,蓝色:伦纳德
可以看出:
角度3:基于投篮命中率
shoot = data_statistics(Kawhi_season1617, Lebron_season1617, '投篮')
shoot
# sns.distplot(Kawhi_season1617.groupby('得分')['球员'].count(),color='b')
# sns.distplot(Lebron_season1617.groupby('得分')['球员'].count(),color='r')
Kawhi_season1617.groupby('得分')['球员'].count().plot(figsize=(12,6),color='b',marker='*')
Lebron_season1617.groupby('得分')['球员'].count().plot(figsize=(12,6),color = 'r', marker='o')
<img src="https://pic3.zhimg.com/v2-92b2093a820a7464999cd363a8c64722_b.png" data-rawwidth="842" data-rawheight="409" class="origin_image zh-lightbox-thumb" width="842" data-original="https://pic3.zhimg.com/v2-92b2093a820a7464999cd363a8c64722_r.png">
红色:詹姆斯,蓝色:伦纳德
用柱状图进行对比分析
<img src="https://pic4.zhimg.com/v2-3299e7fcb0c01e0c7ecaa6abe3fd6d2f_b.png" data-rawwidth="962" data-rawheight="370" class="origin_image zh-lightbox-thumb" width="962" data-original="https://pic4.zhimg.com/v2-3299e7fcb0c01e0c7ecaa6abe3fd6d2f_r.png">
可以看出:
角度4:基于抢断和盖帽的分析
steal = data_statistics(Kawhi_season1617, Lebron_season1617, '抢断')
steal
steal['K_次数'] = steal['K_次数'] * -1
plt.figure(figsize=[16,6])
sns.barplot(x = '抢断', y = 'L_次数', data = steal, color='red')
sns.barplot(x = '抢断',y = 'K_次数', data = steal, color ='blue')
<img src="https://pic1.zhimg.com/v2-755988f7224d6caec9c2cf84d4a7e98c_b.png" data-rawwidth="959" data-rawheight="383" class="origin_image zh-lightbox-thumb" width="959" data-original="https://pic1.zhimg.com/v2-755988f7224d6caec9c2cf84d4a7e98c_r.png">
红色是詹姆斯,蓝色表示伦纳德
block = data_statistics(Kawhi_season1617, Lebron_season1617, '盖帽')
block
block['K_次数'] = block['K_次数'] * -1
plt.figure(figsize=[16,6])
sns.barplot(x = '盖帽', y = 'L_次数', data = block, color='red')
sns.barplot(x = '盖帽',y = 'K_次数', data = block, color ='blue')
<img src="https://pic3.zhimg.com/v2-a0a402c39e795ee822b551f2975587aa_b.png" data-rawwidth="948" data-rawheight="370" class="origin_image zh-lightbox-thumb" width="948" data-original="https://pic3.zhimg.com/v2-a0a402c39e795ee822b551f2975587aa_r.png">
红色是詹姆斯,蓝色表示伦纳德.
可以得到:
角度5:基于失误次数的分析
fault_num = data_statistics(Kawhi_season1617, Lebron_season1617, '失误')
fault_num
fault_num['K_次数'] = fault_num['K_次数'] * -1
plt.figure(figsize=[16,6])
sns.barplot(x = '失误', y = 'L_次数', data = fault_num, color='red')
sns.barplot(x = '失误',y = 'K_次数', data = fault_num, color ='blue')
<img src="https://pic4.zhimg.com/v2-1b6e818fe79a69773d9a96940db5a35b_b.png" data-rawwidth="949" data-rawheight="378" class="origin_image zh-lightbox-thumb" width="949" data-original="https://pic4.zhimg.com/v2-1b6e818fe79a69773d9a96940db5a35b_r.png">
红色是詹姆斯,蓝色表示伦纳德。
可以得到:
3)最终结论
詹姆斯赢伦纳德: 突破能力、组织能力、功能性的差距、稳定性的差距。
詹姆斯输伦纳德: 远投能力的差距。