机器学习使用sklearn进行模型训练、预测和评价

时间:2023-11-22 15:48:02

cross_val_score(model_name, x_samples, y_labels, cv=k)

作用:验证某个模型在某个训练集上的稳定性,输出k个预测精度。

K折交叉验证(k-fold)

把初始训练样本分成k份,其中(k-1)份被用作训练集,剩下一份被用作评估集,这样一共可以对分类器做k次训练,并且得到k个训练结果。

 from sklearn.model_selection import cross_val_score
clf = sklearn.linear_model.LogisticRegression()
# X:features y:targets cv:k
cross_val_score(clf, X, y, cv=5)

模型的训练、预测和评价

 def svm_model():
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score, recall_score, f1_score
from sklearn.svm import SVC
# 模型训练
clf = SVC(kernel='linear')
clf.fit(x_train_samples, y_train_labels)
# 模型存储
joblib.dump(clf, './model/svm_mode.pkl')
# 模型评估
predict_labels = clf.predict(x_test_samples)
Accuracy = accuracy_score(y_test_labels, predict_labels)
Precision = precision_score(y_test_labels, predict_labels, pos_label=0)
Recall = recall_score(y_test_labels, predict_labels, pos_label=0)
F1_scores = f1_score(y_test_labels, predict_labels, pos_label=0)

整个过程结束。需要说明的是调用K折交叉验证,结果输出的是准确率,其它的指标不会输出。所以,建议还是前期,使用train_test_split()函数划分训练集和验证集,后期根据实际需求评估模型