01使用Python分析科比生涯数据
一、前言
本文所講的實(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)題。
- 上一篇: 洛谷 2921 记忆化搜索 tarjan
- 下一篇: MybatisCodeHelperNew