西安推广网站,wordpress teamtalk,潍坊网站制作厂家,WORDPRESS商家确认收货系统模型评估与验证是机器学习流程中的关键步骤#xff0c;它帮助我们了解模型在未见过的数据上的泛化能力。交叉验证#xff08;Cross-Validation, CV#xff09;是一种常用的技术#xff0c;通过将数据集划分为多个子集并进行多次训练和测试来估计模型的性能。此外#xff0…模型评估与验证是机器学习流程中的关键步骤它帮助我们了解模型在未见过的数据上的泛化能力。交叉验证Cross-Validation, CV是一种常用的技术通过将数据集划分为多个子集并进行多次训练和测试来估计模型的性能。此外选择合适的评价指标对于不同类型的任务至关重要。
交叉验证
交叉验证的主要目的是减少由于数据划分带来的偏差并提供更可靠的性能估计。常见的交叉验证方法包括K折交叉验证K-Fold Cross-Validation和留一法交叉验证Leave-One-Out Cross-Validation。
示例使用K折交叉验证评估分类模型
假设二分类问题将使用K折交叉验证来评估一个随机森林分类器的性能。
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split, cross_val_score, KFold
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix# 加载数据
data pd.read_csv(binary_classification_data.csv)
X data.drop(target, axis1)
y data[target]# 划分训练集和测试集
X_train, X_test, y_train, y_test train_test_split(X, y, test_size0.2, random_state42)# 定义分类器
classifier RandomForestClassifier(random_state42)# 使用K折交叉验证评估模型
kfold KFold(n_splits5, shuffleTrue, random_state42)
cv_scores cross_val_score(classifier, X_train, y_train, cvkfold, scoringaccuracy)print(Cross-Validation Accuracy Scores:, cv_scores)
print(Mean CV Accuracy:, np.mean(cv_scores))# 训练最终模型
classifier.fit(X_train, y_train)# 在测试集上评估
y_pred classifier.predict(X_test)# 计算各种评价指标
accuracy accuracy_score(y_test, y_pred)
precision precision_score(y_test, y_pred)
recall recall_score(y_test, y_pred)
f1 f1_score(y_test, y_pred)
conf_matrix confusion_matrix(y_test, y_pred)print(fTest Set Accuracy: {accuracy:.4f})
print(fTest Set Precision: {precision:.4f})
print(fTest Set Recall: {recall:.4f})
print(fTest Set F1 Score: {f1:.4f})
print(Confusion Matrix:\n, conf_matrix) 数据加载 使用pandas读取CSV文件并分离特征和标签。 数据划分 使用train_test_split将数据划分为训练集和测试集。 定义分类器 创建一个随机森林分类器实例。 K折交叉验证 使用KFold创建一个5折交叉验证对象。使用cross_val_score对训练集进行交叉验证并计算准确率。 训练最终模型 使用整个训练集训练最终的分类器。 测试集评估 在测试集上进行预测。计算并打印多种评价指标包括准确率、精确度、召回率、F1分数和混淆矩阵。
回归任务的评估
对于回归任务常用的评价指标包括均方误差MSE、平均绝对误差MAE和决定系数R²等。
示例使用K折交叉验证评估回归模型
假设房价预测问题使用K折交叉验证来评估一个线性回归模型的性能。
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split, cross_val_score, KFold
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score# 加载数据
data pd.read_csv(house_prices.csv)
X data.drop(price, axis1)
y data[price]# 划分训练集和测试集
X_train, X_test, y_train, y_test train_test_split(X, y, test_size0.2, random_state42)# 定义回归器
regressor LinearRegression()# 使用K折交叉验证评估模型
kfold KFold(n_splits5, shuffleTrue, random_state42)
cv_scores cross_val_score(regressor, X_train, y_train, cvkfold, scoringneg_mean_squared_error)print(Cross-Validation MSE Scores (negative values):, cv_scores)
print(Mean CV MSE (positive value):, -np.mean(cv_scores))# 训练最终模型
regressor.fit(X_train, y_train)# 在测试集上评估
y_pred regressor.predict(X_test)# 计算各种评价指标
mse mean_squared_error(y_test, y_pred)
mae mean_absolute_error(y_test, y_pred)
r2 r2_score(y_test, y_pred)print(fTest Set MSE: {mse:.4f})
print(fTest Set MAE: {mae:.4f})
print(fTest Set R^2: {r2:.4f}) 数据加载 使用pandas读取CSV文件并分离特征和标签。 数据划分 使用train_test_split将数据划分为训练集和测试集。 定义回归器 创建一个线性回归模型实例。 K折交叉验证 使用KFold创建一个5折交叉验证对象。使用cross_val_score对训练集进行交叉验证并计算负均方误差因为cross_val_score默认返回的是负值以方便排序。 训练最终模型 使用整个训练集训练最终的回归模型。 测试集评估 在测试集上进行预测。计算并打印多种评价指标包括均方误差MSE、平均绝对误差MAE和决定系数R²。