04-11 隨機森林代碼(葡萄酒質量檢測)

更新、更全的《機器學習》的更新網站,更有python、go、數據結構與算法、爬蟲、人工智能教學等着你:http://www.javashuo.com/article/p-vozphyqp-cm.htmlpython

隨機森林代碼(葡萄酒質量檢測)

1、導入模塊

import pandas as pd
from sklearn import datasets
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

2、導入數據

X, y = datasets.load_wine(return_X_y=True)

3、數據預處理

le = LabelEncoder()
# 把label轉換爲0和1
y = le.fit_transform(y)

# 訓練集和測試集比例爲7:3
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.30,  random_state=1)

4、訓練模型

rf = RandomForestClassifier(n_estimators=1000, criterion='gini',
                            max_features='sqrt', min_samples_split=2, bootstrap=True)
rf = rf.fit(X_train, y_train)

5、度量模型

y_train_pred = rf.predict(X_train)
y_test_pred = rf.predict(X_test)

# 度量隨機森林的準確性
tree_train = accuracy_score(y_train, y_train_pred)
tree_test = accuracy_score(y_test, y_test_pred)

print('隨機森林訓練集和測試集準確度分別爲:{:.2f}/{:.2f}'.format(tree_train, tree_test))
隨機森林訓練集和測試集準確度分別爲:1.00/0.98
相關文章
相關標籤/搜索