Python專案實戰 科比資料集分析

2021-09-27 01:25:14 字數 2802 閱讀 3260

資料集及完整**位址

1 讀取資料

2 特徵工程

2.1 去除無效特徵

2.2 缺失值處理

填充fillna()

2.3 特徵組合

2.4 相似特徵保一

2.5 特徵型別轉換

自定義字典呼叫replace

去除特定字元

1 隨機森林

1.1 原理

1.2 流程

1.3 優點

1.4 缺點

2 引數調優

2.1 引數詳解

對於隨機森林存在很多引數,但是用的比較多的一般為下面幾個:

max_depth:

min_samples_leaf:

min_samples_split:

**演示

# find the best n_estimators for randomforestclassifier

from sklearn.ensemble import randomforestclassifier

from sklearn.cross_validation import kfold

print('finding best n_estimators for randomforestclassifier...')

min_score = 100000

best_n = 0

scores_n =

range_n = np.logspace(0,2,num=3).astype(int)

for n in range_n:

print("the number of trees : ".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])

#rfc_score += rfc.score(train.iloc[test_k], train_y.iloc[test_k])/10

pred = rfc.predict(train_kobe.iloc[test_k])

rfc_score += log_loss(train_label.iloc[test_k], pred) / 10

if rfc_score < min_score:

min_score = rfc_score

best_n = n

t2 = time.time()

print('done processing trees (sec)'.format(n, t2-t1))

print(best_n, min_score)

# find best max_depth for randomforestclassifier

print('finding best max_depth for randomforestclassifier...')

min_score = 100000

best_m = 0

scores_m =

range_m = np.logspace(0,2,num=3).astype(int)

for m in range_m:

print("the max depth : ".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])

#rfc_score += rfc.score(train.iloc[test_k], train_y.iloc[test_k])/10

pred = rfc.predict(train_kobe.iloc[test_k])

rfc_score += log_loss(train_label.iloc[test_k], pred) / 10

if rfc_score < min_score:

min_score = rfc_score

best_m = m

t2 = time.time()

print('done processing trees (sec)'.format(m, t2-t1))

print(best_m, min_score)

結果:

隨機森林(原理/樣例實現/引數調優)

Python專案實戰

匯入pygame模組 import time import pygame from pygame.constants import def main 完成整個程式的控制函式 1.建立乙個視窗 screen pygame.display.set mode 1200 700 0,32 2.建立乙個,當作...

fashion mnist資料集實戰

fashion mnist資料集的大小同手寫數字 並且也是10分類問題,大小一樣為28 28的單通道 此程式採用5層網路結構,adam優化器,cross entry為loss進行train 需要注意的細節 對資料的預處理及型別轉換,train data需要shuffle和batch,test dat...

Python爬蟲實戰 糗事百科

前面我們已經說了那麼多基礎知識了,下面我們做個實戰專案來挑戰一下吧。這次就用前面學的urllib和正規表示式來做,python爬蟲爬取糗事百科的小段子。爬取前我們先看一下我們的目標 1.抓取糗事百科熱門段子 2.過濾帶有的段子 首先我們確定好頁面的url,糗事百科的 是 但是這個url不方便我們後面...