日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

基于python的科比职业生涯命中率分析

發布時間:2023/12/10 python 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于python的科比职业生涯命中率分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

科比職業生涯命中率分析

數據集來自于:https://www.kaggle.com
本文章探索的問題:

  • 總命中率以及每場比賽的命中率
  • 各節命中率及各賽季命中率
  • 2分球和3分球命中率
  • 各個位置的命中率
  • 主客場命中率
  • 得分方式命中率
  • 得分方式的使用概率
  • 各個位置的出手概率

導入包

import numpy as np import pandas as pd import matplotlib.pyplot as plt %matplotlib inline # 解決中文亂碼問題 plt.rcParams['font.sans-serif']='SimHei' plt.rcParams['axes.unicode_minus']=False %config InlineBackend.figure_format='svg'

讀取數據

# 讀取數據 data = pd.read_csv('data.csv') # 預覽數據 data.head() data.info()
  • shot_made_flag字段存在缺失,刪除
  • game_event_id、lat、lon、team_id、team_name、seconds_remaining、minutes_remaining、matchup無用刪除

預處理

  • 刪除存在缺失值的樣本
  • 刪除無用字段
  • 增加主客場字段
kobe = data.drop(['game_event_id', 'lat', 'lon', 'team_id', 'team_name', 'seconds_remaining', 'minutes_remaining', 'matchup'], axis=1) # 0代表客場,1代表主場 kobe['home'] = data['matchup'].apply(lambda x: 0 if x[4]=='@' else 1) kobe['period_seconds_remaining'] = 60 * data['minutes_remaining'] + data['seconds_remaining'] kobe.dropna(inplace=True) kobe['game_date'] = kobe.game_date.apply(lambda x: pd.to_datetime(x))

總命中率以及每場比賽的命中率

# 總命中率 kobe.shot_made_flag.mean() # 每場比賽的命中率 x = kobe.pivot_table(index='game_id', values='shot_made_flag', aggfunc='mean') # 可視化命中率變化 plt.plot(range(len(x.values)), x.values) plt.title('科比每場命中率變化示意圖')


從該圖可以看出,科比每場比賽的命中率總體在0.3-0.6波動。

各節/各賽季命中率

# 職業生涯各節命中率 kobe.pivot_table(index='period', values='shot_made_flag', aggfunc='mean') # 職業生涯各賽季命中率 season = kobe.pivot_table(index='season', values='shot_made_flag', aggfunc='mean') fig, ax = plt.subplots() for label in ax.xaxis.get_ticklabels():label.set_rotation(90) plt.plot(season.index, season.values) plt.title('科比職業生涯各賽季命中率示意圖') plt.xlabel('賽季') plt.ylabel('命中率')

# 職業生涯各賽季各節命中率 p_1 = kobe[kobe['period'] == 1].pivot_table(index='season', values='shot_made_flag', aggfunc='mean') # 第一節 p_2 = kobe[kobe['period'] == 2].pivot_table(index='season', values='shot_made_flag', aggfunc='mean') # 第二節 p_3 = kobe[kobe['period'] == 3].pivot_table(index='season', values='shot_made_flag', aggfunc='mean') # 第三節 p_4 = kobe[kobe['period'] == 4].pivot_table(index='season', values='shot_made_flag', aggfunc='mean') # 第四節 fig, ax = plt.subplots() plt.plot(p_1) plt.plot(p_2) plt.plot(p_3) plt.plot(p_4) for label in ax.xaxis.get_ticklabels():label.set_rotation(90) plt.title('科比職業生涯各節命中率示意圖') plt.legend(('period 1', 'period 2', 'period 3', 'period 4'), loc='best') plt.xlabel('賽季') plt.ylabel('命中率')

2分球和3分球命中率

kobe.pivot_table(index='shot_type', values='shot_made_flag', aggfunc='mean')
  • 2分球命中率為47.7%,3分球命中率為32.9%

各個位置的命中率

kobe.pivot_table(index='shot_zone_area', values='shot_made_flag', aggfunc='mean') kobe.pivot_table(index='shot_zone_basic', values='shot_made_flag', aggfunc='mean') kobe.pivot_table(index='shot_zone_range', values='shot_made_flag', aggfunc='mean')

主客場命中率

kobe.pivot_table(index='home', values='shot_made_flag', aggfunc='mean')
  • 主場45.6%,客場43.6%

得分方式命中率(粗分類)

kobe.pivot_table(index='combined_shot_type', values='shot_made_flag', aggfunc='mean')

得分方式的使用概率

# 利用to_frame()將Series對象轉為DataFrame對象,并重命名columns shot_attempt = kobe.groupby(['combined_shot_type', 'action_type']).count().shot_id.to_frame('attempt') shot_attempt['percentage'] = shot_attempt.attempt / shot_attempt.attempt.sum() shot_attempt.groupby('combined_shot_type').sum().percentage shot_attempt.percentage.nlargest(5) # 可視化科比得分方式的使用概率 tmp = shot_attempt.percentage.nlargest(9).to_frame() tmp.index = tmp.index.map(lambda x: x[1]) tmp.loc['rest'] = 1 - tmp['percentage'].sum() plt.pie(tmp, labels=tmp.index, autopct='%.0f%%') plt.show()

各個位置的出手概率

shot_attempt = kobe.groupby(['shot_zone_area', 'shot_zone_basic', 'shot_zone_range'])['shot_id'].count().to_frame('attempt') shot_attempt['percentage'] = shot_attempt.attempt / shot_attempt.attempt.sum() tmp_1 = shot_attempt.groupby('shot_zone_area').sum() plt.pie(tmp_1['percentage'], labels=tmp_1.index, autopct='%.0f%%') plt.show()

tmp_2 = shot_attempt.groupby('shot_zone_basic').sum() plt.pie(tmp_2['percentage'], labels=tmp_2.index, autopct='%.0f%%') plt.show()

tmp_3 = shot_attempt.groupby('shot_zone_range').sum() plt.pie(tmp_3['percentage'], labels=tmp_3.index, autopct='%.0f%%') plt.show()

總結

以上是生活随笔為你收集整理的基于python的科比职业生涯命中率分析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。