python 科比投籃數據可視化及簡單分析


數據來源:https://www.kaggle.com/c/kobe-bryant-shot-selection/data

參考:https://blog.csdn.net/qq_41888542/article/details/80390900

 1.導包

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt
import seaborn as sns
from pylab import mpl  

2.讀取文件

#設置字體
mpl.rcParams['font.sans-serif'] = ['SimHei'] 
#讀取csv文件
data=pd.read_csv("data.csv")
#輸出前5條數據
print(data.head())
#將shot_made_flag為空的數據清除
new_data = data[data['shot_made_flag'].notnull()]

 

 

可以看到 文件中的數據是  [5 rows x 25 columns]  

action_type 進攻方式(更具體)
combined_shot_type 進攻方式
game_event_id 比賽時間id
game_id 比賽ID
lat 投籃點
loc_x 投籃點
loc_y 投籃點
lon 投籃點
minutes_remaining 單節剩余時間(分鍾)
period 表示第幾節
playoffs 是否是季后賽
season 賽季
seconds_remaining 剩余時間(秒)
shot_distance 投籃距離
shot_made_flag 是否進球
shot_type 兩分球或三分球
shot_zone_area 投籃區域
shot_zone_basic 投籃區域(更具體)
shot_zone_range 投籃范圍
team_id 球隊ID
team_name 球隊名稱
game_date 比賽日期
matchup 比賽雙方
opponent 對手
shot_id 投籃ID

3.數據可視化

先來看一看科比的投籃位置,可以很明顯的看到3分線

plt.figure(figsize=(10,20))
jumpshot = new_data[new_data['combined_shot_type']=='Jump Shot']
layup = new_data[new_data['combined_shot_type']=='Layup']
dunk = new_data[new_data['combined_shot_type']=='Dunk']
tipshot = new_data[new_data['combined_shot_type']=='Tip Shot']
hookshot = new_data[new_data['combined_shot_type']=='Hook Shot']
bankshot = new_data[new_data['combined_shot_type']=='Bank Shot']

plt.scatter(jumpshot.loc_x, jumpshot.loc_y, color='grey')
plt.scatter(layup.loc_x, layup.loc_y, color='red')
plt.scatter(dunk.loc_x, dunk.loc_y, color='yellow' )
plt.scatter(tipshot.loc_x, tipshot.loc_y, color='green')
plt.scatter(hookshot.loc_x, hookshot.loc_y, color='black')
plt.scatter(bankshot.loc_x, bankshot.loc_y, color='blue')
label=['跳投','上籃','扣籃','補籃','勾手','擦板']
plt.legend(label,loc=7)
plt.title('投籃位置')

再來看看科比的出手方式的次數,很明顯跳投最多

attack_method = new_data['combined_shot_type'].value_counts()
a = np.array([1,2,3,4,5,6])
plt.bar(a,attack_method,align = 'center')
plt.xlabel('進攻方式')
plt.ylabel('進攻次數')
plt.title('科比進攻方式')
plt.grid(linestyle = '--',linewidth = 1)
plt.ylim(0,20000)
plt.xticks(a,('跳投','上籃','扣籃','補籃','勾手','擦板'))
plt.show()

投籃命中率上,扣籃無疑是命中率最高的

shooting = new_data[new_data['shot_made_flag']==1]['combined_shot_type'].value_counts()
list1 = attack_method.tolist()
list2 = shooting.tolist()
list3 = attack_method.tolist()
for i in range(len(list1)):
    list3[i]=list2[i]/list1[i]
hits_df=pd.Series(list3);
a = np.array([1,2,3,4,5,6])
plt.bar(a,hits_df,align = 'center')
plt.ylabel('命中率',fontsize=20)
plt.title('科比各種投籃方式命中率',fontsize=20)
plt.xticks(a,('跳投','上籃','扣籃','補籃','勾手','擦板'),fontsize=20)
plt.show()

從投籃距離上看,科比大多數是在中距離出手

 

area = new_data['shot_zone_basic'].value_counts()
b = np.array([0,1,2,3,4,5,6])
plt.barh(b,area,align ='center')
plt.yticks(b,('中距離','進攻有理區','底線之外的三分','除進攻有理區外的禁區','右邊底線三分','左邊底線三分','后場'))
plt.xlabel('投籃次數',fontsize=20)
plt.title('科比在各區域投籃',fontsize=20)
plt.tight_layout()# 緊湊顯示圖片,居中顯示
plt.show()

科比在每一節的出手次數,其中第三節最多,第一節次之,因為5,6,7節都是加時賽,自然少很多

shot_number = new_data['period'].value_counts().sort_index()
shot_hit = new_data[new_data['shot_made_flag']==1]['period'].value_counts().sort_index()
plt.bar(shot_number.index,shot_number,align='center')
plt.ylabel('出手次數',fontsize=20)
plt.xlabel('節數',fontsize=20)
plt.show()

每一節投籃命中率 可以看出科比的投籃命中率在第四節最低,可以見得體力對科比投籃命中率也是有些影響的

list1 = shot_number.tolist()
list2 = shot_hit.tolist()
for i in range(len(list1)):
    list2[i]=list2[i]/list1[i]
shot_hit=pd.Series(list2)
c = np.array([1,2,3,4,5,6,7])
plt.bar(c,shot_hit,align = 'center')
plt.ylabel('命中率',fontsize=20)
plt.title('節數',fontsize=20)
plt.xticks(c)
plt.show()

科比在13-14賽季僅出場6次,從下圖可以清楚的看到

f, axarr = plt.subplots(8, figsize=(15, 25))

sns.countplot(x="combined_shot_type", hue="shot_made_flag", data=new_data, ax=axarr[0])
sns.countplot(x="season", hue="shot_made_flag", data=new_data, ax=axarr[1])
sns.countplot(x="period", hue="shot_made_flag", data=new_data, ax=axarr[2])
sns.countplot(x="playoffs", hue="shot_made_flag", data=new_data, ax=axarr[3])
sns.countplot(x="shot_type", hue="shot_made_flag", data=new_data, ax=axarr[4])
sns.countplot(x="shot_zone_area", hue="shot_made_flag", data=new_data, ax=axarr[5])
sns.countplot(x="shot_zone_basic", hue="shot_made_flag", data=new_data, ax=axarr[6])
sns.countplot(x="shot_zone_range", hue="shot_made_flag", data=new_data, ax=axarr[7])

axarr[0].set_title('投籃方式',fontsize=20)
axarr[1].set_title('生涯',fontsize=20)
axarr[2].set_title('不同節數的投籃',fontsize=20)
axarr[3].set_title('賽季',fontsize=20)
axarr[4].set_title('投籃類型',fontsize=20)
axarr[5].set_title('投籃區域',fontsize=20)
axarr[6].set_title('距離划分投籃區域',fontsize=20)
axarr[7].set_title('投籃距離',fontsize=20)

plt.tight_layout()
plt.show()

0.0為投丟 1.0為投中

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM