機器學習 scikit-learn3 模型實踐 - 支援向量機和決策樹
阿新 • • 發佈:2018-11-24
支援向量機和決策樹 - 目錄
1 簡介
1.1 程式碼下載
程式碼路徑,歡迎 star~~
https://github.com/spareribs/kaggleSpareribs/blob/master/Overdue/ml/code/sklearn_config.py
https://github.com/spareribs/kaggleSpareribs/blob/master/Overdue/ml/code/sklearn_train.py
1.2 程式碼使用方法
- 【必須】config.py 設定檔案存放的路徑
- 【必須】先執行 features 中的 base.py 先把資料處理好 [PS:需要根據實際情況修改]
- 【可選】再通過 code 中的 sklearn_config.py 設定模型的引數[PS: 按需修改]
- 【必須】最後通過 code 中的 sklearn_train.py 訓練模型輸出結果
3 核心程式碼說明
3.1 模型配置
""" 開啟交叉驗證 """
status_vali = False
""" 模型引數 """
clfs = {
'svm': LinearSVC(C=0.5, penalty='l2', dual=True),
'svm_linear': SVC(kernel='linear', probability=True),
'svm_ploy': SVC(kernel='poly', probability=True),
'rf': RandomForestClassifier(n_estimators=10, criterion='gini'),
}
3.2 模型訓練
可以修改模型的選擇 [ svm, svm_linear, svm_ploy, rf ]
""" 1 讀取資料 """
data_fp = open(features_path, 'rb')
x_train, y_train = pickle.load(data_fp)
data_fp.close()
""" 2 訓練分類器, clf_name選擇需要的分類器 """
clf_name = "svm"
clf = clfs[clf_name]
clf.fit(x_train, y_train)
""" 3 在驗證集上評估模型 """
if status_vali:
print("測試模型 & 模型引數如下:\n{0}".format(clf))
print("=" * 20)
pre_train = clf.predict(x_train)
print("訓練集正確率: {0:.4f}".format(clf.score(x_train, y_train)))
print("訓練集f1分數: {0:.4f}".format(f1_score(y_train, pre_train)))
print("訓練集auc分數: {0:.4f}".format(roc_auc_score(y_train, pre_train)))
print("-" * 20)
pre_vali = clf.predict(x_vali)
print("測試集正確率: {0:.4f}".format(clf.score(x_vali, y_vali)))
print("測試集f1分數: {0:.4f}".format(f1_score(y_vali, pre_vali)))
print("測試集auc分數: {0:.4f}".format(roc_auc_score(y_vali, pre_vali)))
print("=" * 20)
3.3 輸出結果
3.3.1 LinearSVC
測試模型 & 模型引數如下:
LinearSVC(C=0.5, class_weight=None, dual=True, fit_intercept=True, intercept_scaling=1, loss='squared_hinge',
max_iter=1000, multi_class='ovr', penalty='l2', random_state=None, tol=0.0001, verbose=0)
====================
訓練集正確率: 0.8022
訓練集f1分數: 0.4489
訓練集auc分數: 0.6422
--------------------
測試集正確率: 0.7954
測試集f1分數: 0.4449
測試集auc分數: 0.6396
====================
3.3.2 SVC linear
測試模型 & 模型引數如下:
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape='ovr', degree=3,
gamma='auto_deprecated', kernel='linear', max_iter=-1, probability=True, random_state=None,
shrinking=True, tol=0.001, verbose=False)
====================
訓練集正確率: 0.7977
訓練集f1分數: 0.3910
訓練集auc分數: 0.6181
--------------------
測試集正確率: 0.7884
測試集f1分數: 0.3837
測試集auc分數: 0.6146
====================
3.3.2 SVC ploy
測試模型 & 模型引數如下:
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='auto_deprecated',
kernel='poly', max_iter=-1, probability=True, random_state=None,
shrinking=True, tol=0.001, verbose=False)
====================
訓練集正確率: 0.8206
訓練集f1分數: 0.4373
訓練集auc分數: 0.6398
--------------------
測試集正確率: 0.7526
測試集f1分數: 0.2067
測試集auc分數: 0.5482
====================
3.3.2 rf
測試模型 & 模型引數如下:
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini', max_depth=None, max_features='auto',
max_leaf_nodes=None, min_impurity_decrease=0.0, min_impurity_split=None, min_samples_leaf=1,
min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=None,
oob_score=False, random_state=None, verbose=0, warm_start=False)
====================
訓練集正確率: 0.9814
訓練集f1分數: 0.9609
訓練集auc分數: 0.9624
--------------------
測試集正確率: 0.7730
測試集f1分數: 0.3721
測試集auc分數: 0.6060
====================