日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

技术图文:基于“科比投篮”数据集学Pandas

發(fā)布時間:2023/12/10 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 技术图文:基于“科比投篮”数据集学Pandas 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

背景

joyful-pandas 是Datawhale Github 倉庫中非常優(yōu)秀的開源教程之一,目前有 582 個Forks,1835 個Star。這個假期 Python、Numpy 的組隊學(xué)習(xí)材料已經(jīng)整理完畢,想著把 Pandas 刷一遍就開始 sklearn 了。

怎么刷 Pandas 呢?我的方法就是用輸出來倒逼輸入,把 joyful-pandas 中的所有習(xí)題做完并用圖文的形式展現(xiàn)出來。 一是記錄自己的學(xué)習(xí)過程,二是給大家提供一個學(xué)習(xí) Pandas 的參考。


練習(xí)

現(xiàn)有一份關(guān)于科比的投籃數(shù)據(jù)集,該數(shù)據(jù)集包含科比·布萊恩特20年職業(yè)生涯中嘗試的每一個射門得分的位置和情況。如下所示:

import pandas as pddf = pd.read_csv('data/Kobe_data.csv', index_col='shot_id') print(df.info()) # <class 'pandas.core.frame.DataFrame'> # Int64Index: 30697 entries, 1 to 30697 # Data columns (total 24 columns): # # Column Non-Null Count Dtype # --- ------ -------------- ----- # 0 action_type 30697 non-null object # 1 combined_shot_type 30697 non-null object # 2 game_event_id 30697 non-null int64 # 3 game_id 30697 non-null int64 # 4 lat 30697 non-null float64 # 5 loc_x 30697 non-null int64 # 6 loc_y 30697 non-null int64 # 7 lon 30697 non-null float64 # 8 minutes_remaining 30697 non-null int64 # 9 period 30697 non-null int64 # 10 playoffs 30697 non-null int64 # 11 season 30697 non-null object # 12 seconds_remaining 30697 non-null int64 # 13 shot_distance 30697 non-null int64 # 14 shot_made_flag 25697 non-null float64 # 15 shot_type 30697 non-null object # 16 shot_zone_area 30697 non-null object # 17 shot_zone_basic 30697 non-null object # 18 shot_zone_range 30697 non-null object # 19 team_id 30697 non-null int64 # 20 team_name 30697 non-null object # 21 game_date 30697 non-null object # 22 matchup 30697 non-null object # 23 opponent 30697 non-null object # dtypes: float64(3), int64(10), object(11) # memory usage: 5.9+ MB

其各字段的含義如下所示:

  • shot_id:投籃ID
  • action_type:用什么方式投的籃
  • combined_shot_type:結(jié)合什么方式投籃
  • game_event_id:比賽時間id
  • game_id:比賽ID
  • lat:投籃的經(jīng)度
  • loc_x:投籃的x坐標(biāo)
  • loc_y:投籃的y坐標(biāo)
  • lon:投籃的緯度
  • minutes_remaining:單場剩余時間(分鐘)
  • period:第幾場
  • playoffs:是不是季后賽
  • season:賽季
  • seconds_remaining:剩余時間(秒)
  • shot_distance:投籃離籃筐的的距離
  • shot_made_flag:是不是進(jìn)球了(主要的標(biāo)簽)
  • shot_type:2分球還是3分球區(qū)域
  • shot_zone_area:投籃區(qū)域的表示方法一
  • shot_zone_basic:投籃區(qū)域的表示方法二
  • shot_zone_range:投籃區(qū)域的表示方法三
  • team_id:隊伍ID
  • team_name:隊伍名字
  • game_date:比賽時間
  • matchup:比賽雙方隊伍
  • opponent:對手

請解決如下問題

(a)哪種action_type和combined_shot_type的組合是最多的?

(b)在所有被記錄的game_id中,遭遇到最多的opponent是一個支?(由于一場比賽會有許多次投籃,但對陣的對手只有一個,本題相當(dāng)于問科比和哪個隊交鋒次數(shù)最多)

參考答案

import pandas as pddf = pd.read_csv('data/Kobe_data.csv', index_col='shot_id') df_type = df.assign(type_1=df['action_type'] + ' ' + df['combined_shot_type']) print(df_type['type_1'].value_counts().index[0]) # Jump Shot Jump Shot print(df_type['type_1'].value_counts()[0]) # 18880df_game = df[['game_id', 'opponent']] print(df_game.drop_duplicates()['opponent'].value_counts().index[0]) # SAS print(df_game.drop_duplicates()['opponent'].value_counts()[0]) # 91

從上面程序的運行結(jié)果可以看出,科比投籃最常用的方式就是跳投Jump Shot,在科比的職業(yè)生涯中與 圣安東尼奧馬刺隊(SAS) 隊交鋒最多,達(dá)到91次。


知識點

1. 列的添加

class DataFrame(NDFrame):def assign(self, **kwargs) -> "DataFrame":

使用assign()方法可以在原有數(shù)據(jù)集中添加列,在本案例中就是添加了type_1列,該列為action_type和combined_shot_type的組合,以方便確定該組合中哪一種出現(xiàn)的次數(shù)最多。

2. count和value_counts

  • count()函數(shù):返回非缺失值元素個數(shù)。
  • value_counts()函數(shù):返回每個元素有多少個。
class IndexOpsMixin:def value_counts(self, normalize=False, sort=True, ascending=False, bins=None, dropna=True):class Series(base.IndexOpsMixin, generic.NDFrame):def count(self, level=None):

使用value_counts()可以得到該列中每個元素出現(xiàn)的次數(shù),并由大到小排列。

3. 重復(fù)元素處理

  • drop_duplicates()函數(shù):返回刪除了重復(fù)行的Series或DataFrame。
class Series(base.IndexOpsMixin, generic.NDFrame):def drop_duplicates(self, keep="first", inplace=False) -> Optional["Series"]:class DataFrame(NDFrame):def drop_duplicates(self,subset: Optional[Union[Hashable, Sequence[Hashable]]] = None,keep: Union[str, bool] = "first",inplace: bool = False,ignore_index: bool = False,) -> Optional["DataFrame"]:

由game_id和opponent構(gòu)造新的數(shù)據(jù)集,并對該數(shù)據(jù)集drop_duplicates()去重,就能得到每場比賽的對手球隊。根據(jù)value_counts()可以得到與每個對手球隊比賽的次數(shù)并由大到小排列。

4. 第一章 Pandas基礎(chǔ) 的知識導(dǎo)圖


總結(jié)

通過做練習(xí)題來鞏固 Pandas 的知識點是熟悉 Pandas 的一種有效方式,以往遇到問題直接就摸索著來用了,這次跟著 joyful-pandas 系統(tǒng)的學(xué)習(xí)一遍夯實基礎(chǔ)。大家一起努力。See You!


后臺回復(fù)「搜搜搜」,隨機(jī)獲取電子資源!
歡迎關(guān)注,請掃描二維碼:



總結(jié)

以上是生活随笔為你收集整理的技术图文:基于“科比投篮”数据集学Pandas的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。