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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

01使用Python分析科比生涯数据

發(fā)布時(shí)間:2023/12/10 python 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 01使用Python分析科比生涯数据 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、前言


本文所講的實(shí)戰(zhàn)內(nèi)容是用機(jī)器學(xué)習(xí)的方法來(lái)分析科比職業(yè)生涯數(shù)據(jù)的實(shí)例,利用隨機(jī)森林算法訓(xùn)練處一個(gè)預(yù)測(cè)科比投籃模型。主要用了python的numpy,pandas,matplotlib和sklearn庫(kù)。

本文出現(xiàn)的所有代碼,均可在我的github上下載,歡迎Follow、Star:github地址

二、設(shè)計(jì)思路


先來(lái)看看這份科比生涯的數(shù)據(jù)集:數(shù)據(jù)集下載


這個(gè)表格記錄了科比30000多個(gè)鏡頭的詳細(xì)數(shù)據(jù),共有25個(gè)標(biāo)簽。

具體的設(shè)計(jì)思路是將這25個(gè)標(biāo)簽代表的數(shù)據(jù)進(jìn)行分析,找出對(duì)科比投籃結(jié)果有影響的標(biāo)簽,利用機(jī)器學(xué)習(xí)中隨機(jī)森林的算法訓(xùn)練出可以預(yù)測(cè)科比是否能夠投籃命中的模型。

先來(lái)看看這25個(gè)標(biāo)簽具體代表什么(自己不是籃球的專(zhuān)業(yè)人士和愛(ài)好者,所以具體的內(nèi)容可能有所出入,不過(guò)不會(huì)影響到分析結(jié)果)

  • action_type(用什么方式投的籃)
  • combined_shot_type(結(jié)合什么方式投籃)
  • game_event_id(游戲事件ID)
  • game_id(游戲ID)
  • la(投籃的經(jīng)度)
  • loc_x (投籃的x坐標(biāo))
  • loc_y(投籃的y坐標(biāo))
  • lon(投籃的緯度)
  • minutes_remaining(離比賽結(jié)束還有多少分鐘)
  • period(第幾場(chǎng))
  • playoffs(是不是季后賽)
  • season(賽季)
  • seconds_remaining(離比賽結(jié)束還有多少秒)
  • 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(隊(duì)伍ID)
  • team_name(隊(duì)伍名字)
  • game_date(比賽時(shí)間)
  • matchup(比賽雙方隊(duì)伍)
  • opponent(自己所在隊(duì)伍名字)
  • shot_id(鏡頭ID)

可以看到,這25個(gè)標(biāo)簽中對(duì)于科比能否投籃命中有一些無(wú)關(guān)緊要的數(shù)據(jù),比如team_id,因?yàn)檫@30000多份樣本中全是在湖人隊(duì)打的,shot_id,game_id等等這個(gè)數(shù)據(jù)也無(wú)關(guān)緊要,具體的分析將會(huì)在下面講解。

三、數(shù)據(jù)分析


首先我們導(dǎo)入數(shù)據(jù),編寫(xiě)代碼如下

import pandas as pd# 導(dǎo)入數(shù)據(jù) filename= "data.csv" raw = pd.read_csv(filename) print(raw.shape) print(raw.head()) #head函數(shù)打印前5行,如果需要打印前10行,這樣寫(xiě)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

運(yùn)行結(jié)果如下

接下來(lái)我們?cè)賮?lái)分析這一份數(shù)據(jù)表,我們發(fā)現(xiàn)其中shot_made_flag這個(gè)標(biāo)簽竟然有缺失值,這個(gè)表示了科比是否進(jìn)球了,作為最重要的數(shù)據(jù),是不能隨意進(jìn)行填充的,我們必須刪除掉這些樣本進(jìn)行下一步的工作,代碼如下

import pandas as pd# 導(dǎo)入數(shù)據(jù) filename= "data.csv" raw = pd.read_csv(filename)kobe = raw[pd.notnull(raw['shot_made_flag'])] print(kobe.shape)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

運(yùn)行結(jié)果如下

此時(shí)我們只有25697個(gè)數(shù)據(jù)進(jìn)行訓(xùn)練了。

接著我們分析lat,loc_x,loc_y,lon這4個(gè)標(biāo)簽,這4個(gè)標(biāo)簽說(shuō)明了科比投籃的位置,而具體指的是什么呢,有什么關(guān)系嗎,我們畫(huà)散點(diǎn)圖來(lái)看一下。

編寫(xiě)代碼如下

import pandas as pd import matplotlib.pyplot as plt# 導(dǎo)入數(shù)據(jù) filename= "data.csv" raw = pd.read_csv(filename)#刪除shot_made_flag為空的數(shù)據(jù)項(xiàng),并且命名為kobe用作訓(xùn)練 kobe = raw[pd.notnull(raw['shot_made_flag'])]#畫(huà)散點(diǎn)圖用來(lái)分析lat loc_x loc_y lon這4個(gè)標(biāo)簽 alpha = 0.02 #指定一個(gè)數(shù)字,用于后面的透明度 plt.figure(figsize=(6,6)) #指定畫(huà)圖域 # loc_x and loc_y plt.subplot(121) #一行兩列 第一個(gè)位置 plt.scatter(kobe.loc_x, kobe.loc_y, color='R', alpha=alpha) #畫(huà)散點(diǎn)圖 plt.title('loc_x and loc_y') # lat and lon plt.subplot(122) #一行兩列 第二個(gè)位置 plt.scatter(kobe.lon, kobe.lat, color='B', alpha=alpha) plt.title('lat and lon') plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

運(yùn)行結(jié)果如圖所示

我們大致可以看出,這4個(gè)坐標(biāo)大致表示了距離籃筐的距離,那樣的話,我們接下來(lái)用于數(shù)據(jù)處理的時(shí)候選擇其中的一組數(shù)據(jù)即可了。

shot_type,shot_zone_area,shot_zone_basic,shot_zone_range 這4個(gè)標(biāo)簽代表了投籃的區(qū)域,其實(shí)還是說(shuō)明一件事,這里就不做贅述了,當(dāng)然shot_zone_area,shot_zone_basic,shot_zone_range這3個(gè)標(biāo)簽將投籃區(qū)域相比于shot_type來(lái)說(shuō)分的更細(xì),直接刪掉是不是會(huì)有問(wèn)題,其實(shí)大可不必?fù)?dān)心,因?yàn)?#xff0c;接下來(lái)我們將會(huì)用極坐標(biāo)的形式表示科比的投籃位置,將更會(huì)細(xì)化科比的投籃區(qū)域。

四、數(shù)據(jù)處理


首先處理我們上節(jié)所說(shuō)的極坐標(biāo)的問(wèn)題,然后我們會(huì)發(fā)現(xiàn)算出來(lái)的dist和,shot_distance竟然具有正相關(guān)性。

import pandas as pd import numpy as np import matplotlib.pyplot as plt import time from sklearn.ensemble import RandomForestClassifier #導(dǎo)入隨機(jī)森林分類(lèi)器 from sklearn.cross_validation import KFold from sklearn.metrics import log_loss# 導(dǎo)入數(shù)據(jù) filename= "data.csv" raw = pd.read_csv(filename)#刪除shot_made_flag為空的數(shù)據(jù)項(xiàng),并且命名為kobe用作訓(xùn)練 kobe = raw[pd.notnull(raw['shot_made_flag'])]#對(duì)于lat,loc_x,loc_y,lon這4個(gè)標(biāo)簽,我們?nèi)oc_x和loc_y這2個(gè)標(biāo)簽,并將其轉(zhuǎn)化為極坐標(biāo)的形式 #dist表示離籃筐的距離,angle表示投籃的角度,這樣將會(huì)更好的科比投籃的反應(yīng)結(jié)果` raw['dist'] = np.sqrt(raw['loc_x']**2 + raw['loc_y']**2) loc_x_zero = raw['loc_x'] == 0 raw['angle'] = np.array([0]*len(raw)) raw['angle'][~loc_x_zero] = np.arctan(raw['loc_y'][~loc_x_zero] / raw['loc_x'][~loc_x_zero]) raw['angle'][loc_x_zero] = np.pi / 2#畫(huà)圖展示dist和shot_distance的正相關(guān)性 plt.figure(figsize=(5,5)) plt.scatter(raw.dist, raw.shot_distance, color='blue') plt.title('dist and shot_distance') plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

運(yùn)行結(jié)果如下

這樣我們可以保留其中的一個(gè)(這里我們保留了dist這個(gè)標(biāo)簽),接著我們將minutes_remaining和seconds_remaining轉(zhuǎn)化成一個(gè)標(biāo)簽remaining_time,然后刪除不必要的列,非數(shù)值型的轉(zhuǎn)換成onehot編碼格式

具體編寫(xiě)代碼如下,具體說(shuō)明在代碼注釋中

import pandas as pd import numpy as np import matplotlib.pyplot as plt# 導(dǎo)入數(shù)據(jù) filename= "data.csv" raw = pd.read_csv(filename)#刪除shot_made_flag為空的數(shù)據(jù)項(xiàng),并且命名為kobe用作訓(xùn)練 kobe = raw[pd.notnull(raw['shot_made_flag'])]#對(duì)于lat,loc_x,loc_y,lon這4個(gè)標(biāo)簽,我們?nèi)oc_x和loc_y這2個(gè)標(biāo)簽,并將其轉(zhuǎn)化為極坐標(biāo)的形式 #dist表示離籃筐的距離,angle表示投籃的角度,這樣將會(huì)更好的科比投籃的反應(yīng)結(jié)果 raw['dist'] = np.sqrt(raw['loc_x']**2 + raw['loc_y']**2) loc_x_zero = raw['loc_x'] == 0 raw['angle'] = np.array([0]*len(raw)) raw['angle'][~loc_x_zero] = np.arctan(raw['loc_y'][~loc_x_zero] / raw['loc_x'][~loc_x_zero]) raw['angle'][loc_x_zero] = np.pi / 2#對(duì)于minutes_remaining:離比賽結(jié)束還有多少分鐘;seconds_remaining:離比賽結(jié)束還有多少秒(0-60),這 #2個(gè)屬性我們合成距離比賽結(jié)束的時(shí)間 raw['remaining_time'] = raw['minutes_remaining'] * 60 + raw['seconds_remaining']#機(jī)器學(xué)習(xí)只能識(shí)別數(shù)值型的數(shù)據(jù) #將賽季中'Jan-00' 'Feb-01' 'Mar-02' ··· '1998-99'轉(zhuǎn)換成 # 0 1 2 ··· 99 raw['season'] = raw['season'].apply(lambda x: int(x.split('-')[1]) )#刪除對(duì)于比賽結(jié)果沒(méi)有影響的數(shù)據(jù) drops = ['shot_id', 'team_id', 'team_name', 'shot_zone_area', 'shot_zone_range', 'shot_zone_basic','matchup', 'lon','lat', 'seconds_remaining', 'minutes_remaining','shot_distance', 'loc_x', 'loc_y', 'game_event_id', 'game_id','game_date'] for drop in drops:raw = raw.drop(drop, 1)#將非數(shù)值型的數(shù)據(jù)轉(zhuǎn)換成為onehot編碼的格式,加入到數(shù)據(jù)中并且將原來(lái)的數(shù)據(jù)刪除 categorical_vars = ['action_type', 'combined_shot_type', 'shot_type', 'opponent', 'period', 'season'] for var in categorical_vars:raw = pd.concat([raw, pd.get_dummies(raw[var], prefix=var)], 1)raw = raw.drop(var, 1) print(raw)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

結(jié)果調(diào)試器里面顯示的效果不太好,只附上最后的行和列吧

為什么會(huì)有129行之多,是因?yàn)槲覀冇昧薿nehot編碼,具體什么是onehot編碼這里就不做贅述了,感興趣的可以谷歌或者百度一下。

最后我們總結(jié)一下,到底這25個(gè)標(biāo)簽還剩下什么,首先除去和比賽結(jié)果無(wú)關(guān)的標(biāo)簽,’shot_id’, ‘team_id’, ‘team_name’, ‘shot_zone_area’, ‘shot_zone_range’, ‘shot_zone_basic’,’matchup’, ‘lon’,
‘lat’, ‘seconds_remaining’, ‘minutes_remaining’,’shot_distance’, , ‘game_event_id’, ‘game_id’,
‘game_date’

然后’loc_x’, ‘loc_y’轉(zhuǎn)換成了極坐標(biāo)的形式,變成了’dist’,’angle’;’seconds_remaining’和’minutes_remaining’合并成了’remaining_time’。

最后將’action_type’, ‘combined_shot_type’, ‘shot_type’, ‘opponent’, ‘period’, ‘season’轉(zhuǎn)換成onehot編碼格式。

至此我們的數(shù)據(jù)處理工作基本完成了。

五、利用sklearn來(lái)進(jìn)行數(shù)據(jù)的處理


具體的思路是利用隨機(jī)森林分類(lèi)器配合著交叉驗(yàn)證的方法進(jìn)行數(shù)據(jù)的分析,先找到最佳的樹(shù)的個(gè)數(shù),和樹(shù)的深度。

編寫(xiě)代碼如下

import pandas as pd import numpy as np import matplotlib.pyplot as plt import time from sklearn.ensemble import RandomForestClassifier #導(dǎo)入隨機(jī)森林分類(lèi)器 from sklearn.cross_validation import KFold from sklearn.metrics import log_loss# 導(dǎo)入數(shù)據(jù) filename= "data.csv" raw = pd.read_csv(filename)#刪除shot_made_flag為空的數(shù)據(jù)項(xiàng),并且命名為kobe用作訓(xùn)練 kobe = raw[pd.notnull(raw['shot_made_flag'])]#對(duì)于lat,loc_x,loc_y,lon這4個(gè)標(biāo)簽,我們?nèi)oc_x和loc_y這2個(gè)標(biāo)簽,并將其轉(zhuǎn)化為極坐標(biāo)的形式 #dist表示離籃筐的距離,angle表示投籃的角度,這樣將會(huì)更好的科比投籃的反應(yīng)結(jié)果` raw['dist'] = np.sqrt(raw['loc_x']**2 + raw['loc_y']**2) loc_x_zero = raw['loc_x'] == 0 raw['angle'] = np.array([0]*len(raw)) raw['angle'][~loc_x_zero] = np.arctan(raw['loc_y'][~loc_x_zero] / raw['loc_x'][~loc_x_zero]) raw['angle'][loc_x_zero] = np.pi / 2# 對(duì)于minutes_remaining:離比賽結(jié)束還有多少分鐘;seconds_remaining:離比賽結(jié)束還有多少秒(0-60),這 # 2個(gè)屬性我們合成距離比賽結(jié)束的時(shí)間 raw['remaining_time'] = raw['minutes_remaining'] * 60 + raw['seconds_remaining']#機(jī)器學(xué)習(xí)只能識(shí)別數(shù)值型的數(shù)據(jù) #將賽季中'Jan-00' 'Feb-01' 'Mar-02' ··· '1998-99'轉(zhuǎn)換成 # 0 1 2 ··· 99 raw['season'] = raw['season'].apply(lambda x: int(x.split('-')[1]) )# 刪除對(duì)于比賽結(jié)果沒(méi)有影響的數(shù)據(jù) drops = ['shot_id', 'team_id', 'team_name', 'shot_zone_area', 'shot_zone_range', 'shot_zone_basic','matchup', 'lon','lat', 'seconds_remaining', 'minutes_remaining','shot_distance', 'loc_x', 'loc_y', 'game_event_id', 'game_id','game_date'] for drop in drops:raw = raw.drop(drop, 1)#將非數(shù)值型的數(shù)據(jù)轉(zhuǎn)換成為onehot編碼的格式,加入到數(shù)據(jù)中并且將原來(lái)的數(shù)據(jù)刪除 categorical_vars = ['action_type', 'combined_shot_type', 'shot_type', 'opponent', 'period', 'season'] for var in categorical_vars:raw = pd.concat([raw, pd.get_dummies(raw[var], prefix=var)], 1)raw = raw.drop(var, 1)#將數(shù)據(jù)分為訓(xùn)練集和測(cè)試集 train_kobe = raw[pd.notnull(raw['shot_made_flag'])] train_label = train_kobe['shot_made_flag'] train_kobe = train_kobe.drop('shot_made_flag', 1)test_kobe = raw[pd.isnull(raw['shot_made_flag'])] test_kobe = test_kobe.drop('shot_made_flag', 1)print('尋找隨機(jī)森林分類(lèi)器的的最佳樹(shù)的數(shù)量...') min_score = 100000 best_n = 0 scores_n = [] range_n = np.logspace(0, 2, num=10).astype(int) for n in range_n:print('樹(shù)的數(shù)量 : {0}'.format(n))t1 = time.time()rfc_score = 0.rfc = RandomForestClassifier(n_estimators=n)for train_k, test_k in KFold(len(train_kobe), n_folds=10, shuffle=True):rfc.fit(train_kobe.iloc[train_k], train_label.iloc[train_k])pred = rfc.predict(train_kobe.iloc[test_k])rfc_score += log_loss(train_label.iloc[test_k], pred) / 10scores_n.append(rfc_score)if rfc_score < min_score:min_score = rfc_scorebest_n = nt2 = time.time()print('建造 {0} 顆樹(shù)(耗時(shí){1:.3f}秒)'.format(n, t2 - t1)) print("最佳樹(shù)的顆樹(shù)為 : {0},得分為: {1}".format(best_n,min_score))print('\n')print('尋找隨機(jī)森林分類(lèi)器的最佳樹(shù)的最佳深度...') min_score = 100000 best_m = 0 scores_m = [] range_m = np.logspace(0, 2, num=10).astype(int) for m in range_m:print("樹(shù)的最大的深度 : {0}".format(m))t1 = time.time()rfc_score = 0.rfc = RandomForestClassifier(max_depth=m, n_estimators=best_n)for train_k, test_k in KFold(len(train_kobe), n_folds=10, shuffle=True):rfc.fit(train_kobe.iloc[train_k], train_label.iloc[train_k])pred = rfc.predict(train_kobe.iloc[test_k])rfc_score += log_loss(train_label.iloc[test_k], pred) / 10scores_m.append(rfc_score)if rfc_score < min_score:min_score = rfc_scorebest_m = mt2 = time.time()print('樹(shù)的最大深度為: {0}(耗時(shí){1:.3f}秒)'.format(m, t2 - t1)) print('最佳樹(shù)的深度: {0},得分為:{1}'.format(best_m, min_score))plt.figure(figsize=(10,5)) plt.subplot(121) plt.plot(range_n, scores_n) plt.ylabel('score') plt.xlabel('number of trees')plt.subplot(122) plt.plot(range_m, scores_m) plt.ylabel('score') plt.xlabel('max depth') plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122

運(yùn)行結(jié)果如下,說(shuō)明當(dāng)樹(shù)的顆樹(shù)為100,并且深度為12的時(shí)候,損失函數(shù)最小,下面是具體的得分圖示。

下面我們用100,12這個(gè)參數(shù)訓(xùn)練模型,并且預(yù)測(cè)出5000個(gè)’shot_made_flag’的缺失值。

import pandas as pd import numpy as np import matplotlib.pyplot as plt import time from sklearn.ensemble import RandomForestClassifier #導(dǎo)入隨機(jī)森林分類(lèi)器 from sklearn.cross_validation import KFold from sklearn.metrics import log_loss# 導(dǎo)入數(shù)據(jù) filename = "data.csv" raw = pd.read_csv(filename)# 刪除shot_made_flag為空的數(shù)據(jù)項(xiàng),并且命名為kobe用作訓(xùn)練 kobe = raw[pd.notnull(raw['shot_made_flag'])]# 對(duì)于lat,loc_x,loc_y,lon這4個(gè)標(biāo)簽,我們?nèi)oc_x和loc_y這2個(gè)標(biāo)簽,并將其轉(zhuǎn)化為極坐標(biāo)的形式 # dist表示離籃筐的距離,angle表示投籃的角度,這樣將會(huì)更好的科比投籃的反應(yīng)結(jié)果` raw['dist'] = np.sqrt(raw['loc_x']**2 + raw['loc_y']**2) loc_x_zero = raw['loc_x'] == 0 raw['angle'] = np.array([0]*len(raw)) raw['angle'][~loc_x_zero] = np.arctan(raw['loc_y'][~loc_x_zero] / raw['loc_x'][~loc_x_zero]) raw['angle'][loc_x_zero] = np.pi / 2# 對(duì)于minutes_remaining:離比賽結(jié)束還有多少分鐘;seconds_remaining:離比賽結(jié)束還有多少秒(0-60),這 # 2個(gè)屬性我們合成距離比賽結(jié)束的時(shí)間 raw['remaining_time'] = raw['minutes_remaining'] * 60 + raw['seconds_remaining']# 機(jī)器學(xué)習(xí)只能識(shí)別數(shù)值型的數(shù)據(jù) # 將賽季中'Jan-00' 'Feb-01' 'Mar-02' ··· '1998-99'轉(zhuǎn)換成 # 0 1 2 ··· 99 raw['season'] = raw['season'].apply(lambda x: int(x.split('-')[1]) )#刪除對(duì)于比賽結(jié)果沒(méi)有影響的數(shù)據(jù) drops = ['shot_id', 'team_id', 'team_name', 'shot_zone_area', 'shot_zone_range', 'shot_zone_basic','matchup', 'lon','lat', 'seconds_remaining', 'minutes_remaining','shot_distance', 'loc_x', 'loc_y', 'game_event_id', 'game_id','game_date'] for drop in drops:raw = raw.drop(drop, 1)#將非數(shù)值型的數(shù)據(jù)轉(zhuǎn)換成為onehot編碼的格式,加入到數(shù)據(jù)中并且將原來(lái)的數(shù)據(jù)刪除 categorical_vars = ['action_type', 'combined_shot_type', 'shot_type', 'opponent', 'period', 'season'] for var in categorical_vars:raw = pd.concat([raw, pd.get_dummies(raw[var], prefix=var)], 1)raw = raw.drop(var, 1) # print(raw)# 將數(shù)據(jù)分為訓(xùn)練集和測(cè)試集 train_kobe = raw[pd.notnull(raw['shot_made_flag'])] train_label = train_kobe['shot_made_flag'] train_kobe = train_kobe.drop('shot_made_flag', 1)test_kobe = raw[pd.isnull(raw['shot_made_flag'])] test_kobe = test_kobe.drop('shot_made_flag', 1)# 訓(xùn)練模型并且用預(yù)測(cè)shot_made_flag的缺失值 model = RandomForestClassifier(n_estimators=100, max_depth=12) model.fit(train_kobe, train_label) predictions = model.predict(test_kobe) result=pd.DataFrame({'shot_id':test_shot_id['shot_id'].as_matrix(),'shot_made_flag':predictions.astype(np.int32)}) result.to_csv("result.csv", index=False)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

運(yùn)行結(jié)果如下圖

這里給出了5000個(gè)缺失值。

六、總結(jié)

本篇文章主要用了機(jī)器學(xué)習(xí)的sklearn庫(kù),配合著numpy,pandas,matplotlib的技術(shù)路線,利用隨機(jī)森林算法對(duì)科比生涯數(shù)據(jù)進(jìn)行分析,對(duì)缺失值進(jìn)行了預(yù)測(cè)。

總結(jié)

以上是生活随笔為你收集整理的01使用Python分析科比生涯数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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

主站蜘蛛池模板: 97超视频 | 中文在线观看av | 色八戒av | 精品无码人妻一区二区免费蜜桃 | 欧美成人精品欧美一级 | 一级a性色生活片久久毛片 爱爱高潮视频 | 国产精品电影在线观看 | 亚洲性综合网 | 欧美成人精品二区三区99精品 | 精品视频中文字幕 | 亚洲综合五区 | 激情999| 日批视频免费看 | 国产孕妇孕交大片孕 | 欧美日韩一二三 | 国产视频一区二区三区四区五区 | 婷婷99| 99在线观看 | 大香蕉视频一区二区 | 日本精品在线播放 | 国产亚洲天堂网 | 国产第一精品 | 国产午夜成人久久无码一区二区 | 亚洲成a人片77777精品 | 久久久免费毛片 | 91免费高清视频 | 黄色国产在线播放 | 久久国产精品久久久久 | 欧美乱操| 国产中文字幕在线 | 欧美日韩国产片 | v天堂在线| 国产福利二区 | 欧美激情 国产精品 | 国产第三区 | 欧美日日夜夜 | 深夜福利视频在线观看 | 国内性视频 | 色呦呦网站入口 | 久久久久久久国产视频 | 亚洲精品福利网站 | 欧美福利网站 | 青娱乐在线免费观看 | 少妇被黑人到高潮喷出白浆 | 免费观看在线高清 | 久久久精品日本 | 自拍偷拍精品视频 | 久久婷婷五月综合色国产香蕉 | 偷拍自拍一区 | 亚洲国产精品久久AV | 亚洲人午夜射精精品日韩 | 国产女人18水真多毛片18精品 | 夜夜操夜夜爱 | av大全免费 | 男女曰逼视频 | 日本免费一区二区三区四区五六区 | 怒海潜沙秦岭神树 | 国产一级视频免费观看 | 国产精品毛片久久久久久久av | 在线观看www. | 热久久国产 | 亚日韩在线 | 婷婷啪啪| 欧美视频区 | 日韩在线一区二区 | 九九热视 | 在线观看免费高清视频 | 国产精品毛片在线 | 日韩精品一区二区三区色欲av | 中文字幕av网 | 国产又黄又粗的视频 | 香蕉视频在线免费播放 | www.白浆 | 亚洲系列中文字幕 | 成人亚洲精品777777ww | 国产精品毛片一区视频播 | 欧美激情精品久久久久久免费 | avtt在线观看 | 欧美女优在线 | 观看av在线| 黄色特级网站 | 黄色在线网 | 久久久久99人妻一区二区三区 | 久久蜜桃视频 | 一区二区日本视频 | 777久久久精品一区二区三区 | 男人天堂网在线视频 | 国产精品尤物 | 奇米激情 | 高h喷水荡肉少妇爽多p视频 | 国产精品秘入口18禁麻豆免会员 | 欧美在线免费观看视频 | 美女黄视频网站 | 欧美日韩亚洲精品一区二区 | 毛片毛片毛片毛片毛片毛片毛片 | 亚洲精品激情视频 | av在线影片 | 男阳茎进女阳道视频大全 | 中文字幕精品一区久久久久 |