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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

背景

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

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


練習

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

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:結合什么方式投籃
  • game_event_id:比賽時間id
  • game_id:比賽ID
  • lat:投籃的經度
  • loc_x:投籃的x坐標
  • loc_y:投籃的y坐標
  • lon:投籃的緯度
  • minutes_remaining:單場剩余時間(分鐘)
  • period:第幾場
  • playoffs:是不是季后賽
  • season:賽季
  • seconds_remaining:剩余時間(秒)
  • shot_distance:投籃離籃筐的的距離
  • shot_made_flag:是不是進球了(主要的標簽)
  • shot_type:2分球還是3分球區域
  • shot_zone_area:投籃區域的表示方法一
  • shot_zone_basic:投籃區域的表示方法二
  • shot_zone_range:投籃區域的表示方法三
  • team_id:隊伍ID
  • team_name:隊伍名字
  • game_date:比賽時間
  • matchup:比賽雙方隊伍
  • opponent:對手

請解決如下問題

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

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

參考答案

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

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


知識點

1. 列的添加

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

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

2. count和value_counts

  • count()函數:返回非缺失值元素個數。
  • value_counts()函數:返回每個元素有多少個。
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()可以得到該列中每個元素出現的次數,并由大到小排列。

3. 重復元素處理

  • drop_duplicates()函數:返回刪除了重復行的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構造新的數據集,并對該數據集drop_duplicates()去重,就能得到每場比賽的對手球隊。根據value_counts()可以得到與每個對手球隊比賽的次數并由大到小排列。

4. 第一章 Pandas基礎 的知識導圖


總結

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


后臺回復「搜搜搜」,隨機獲取電子資源!
歡迎關注,請掃描二維碼:



總結

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

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