在Python中,我们通常使用scikit-learn库来进行机器学习模型的性能评估。以下是一些常用的性能评估指标和方法:
from sklearn.metrics import accuracy_score
y_true = [0, 1, 2, 3, 4]
y_pred = [0, 2, 2, 3, 4]
accuracy = accuracy_score(y_true, y_pred)
print(f"Accuracy: {accuracy}")
from sklearn.metrics import precision_score, recall_score, f1_score
y_true = [0, 1, 2, 3, 4]
y_pred = [0, 2, 2, 3, 4]
precision = precision_score(y_true, y_pred, average='macro')
recall = recall_score(y_true, y_pred, average='macro')
f1 = f1_score(y_true, y_pred, average='macro')
print(f"Precision: {precision}")
print(f"Recall: {recall}")
print(f"F1 Score: {f1}")
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
y_true = [0, 1, 0, 1, 0, 1]
y_scores = [0.1, 0.4, 0.35, 0.8, 0.7, 0.6]
fpr, tpr, thresholds = roc_curve(y_true, y_scores)
roc_auc = auc(fpr, tpr)
plt.figure()
plt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (area = {roc_auc:.2f})')
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic')
plt.legend(loc="lower right")
plt.show()
from sklearn.metrics import mean_squared_error
y_true = [3, -0.5, 2, 7]
y_pred = [2.5, 0.0, 2, 8]
mse = mean_squared_error(y_true, y_pred)
print(f"MSE: {mse}")
from sklearn.metrics import mean_squared_error
import numpy as np
rmse = np.sqrt(mean_squared_error(y_true, y_pred))
print(f"RMSE: {rmse}")
from sklearn.metrics import mean_absolute_error
mae = mean_absolute_error(y_true, y_pred)
print(f"MAE: {mae}")
from sklearn.metrics import r2_score
r2 = r2_score(y_true, y_pred)
print(f"R² Score: {r2}")
交叉验证是一种评估模型泛化能力的方法,常用的有K折交叉验证。
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier()
scores = cross_val_score(clf, X, y, cv=5)
print(f"Scores: {scores}")
print(f"Mean Score: {np.mean(scores)}")
混淆矩阵可以帮助我们了解模型在不同类别上的表现。
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
y_true = [0, 1, 2, 3, 4]
y_pred = [0, 2, 2, 3, 4]
cm = confusion_matrix(y_true, y_pred)
sns.heatmap(cm, annot=True, fmt='d')
plt.xlabel('Predicted')
plt.ylabel('True')
plt.show()
通过这些指标和方法,你可以全面评估机器学习模型的性能,并根据需要进行调整和优化。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。