HoldOut Cross-validation(Train-Test Split)
1. 简单高效
scikit - learn
库中,使用 train_test_split
函数可以轻松完成划分,代码简洁易懂。2. 模拟真实应用场景
3. 灵活性高
1. 评估结果不稳定
2. 数据利用不充分
3. 难以选择最优模型和参数
(K-fold Cross Validation,记为K-CV或K-fold)
K-Fold交叉验证技术中,整个数据集被划分为K个大小相同的部分。每个分区被称为 一个”Fold”。所以我们有K个部分,我们称之为K-Fold。一个Fold被用作验证集,其余的K-1个Fold被用作训练集。
该技术重复K次,直到每个Fold都被用作验证集,其余的作为训练集。
模型的最终准确度是通过取k个模型验证数据的平均准确度来计算。
更准确的模型评估
有效利用数据
适用于模型选择和参数调优
计算成本高
对 K 值选择敏感
数据分布问题
Stratified k-fold cross validation
分层 k - 折交叉验证(Stratified k - fold cross validation)是对普通 k - 折交叉验证的改进,在划分数据集时会考虑样本的类别分布,确保每个折(子集)中各类别样本的比例与原始数据集中的比例一致。
1. 更准确的模型评估
2. 提高模型泛化能力评估的可靠性
3. 适用于多种分类问题
1. 计算成本较高
2. 对数据特征的依赖
3. 对异常值和噪声的敏感性
留一交叉验证是一种特殊的 k 折交叉验证,其中 k 等于样本数量 。具体做法是每次只留一个样本作为验证集,其余 个样本作为训练集,重复 次,最终得到 个模型评估结果,取平均值作为最终的评估指标。
蒙特卡罗交叉验证也称为随机子抽样验证,它随机地将数据集划分为训练集和验证集,重复多次(例如 次),每次划分的比例可以自定义。每次划分后训练模型并评估,最终得到 个评估结果,取平均值作为最终的评估指标。
考虑到时间序列数据的顺序性和相关性,时间序列交叉验证按照时间顺序依次划分训练集和验证集。例如,前 个时间点的数据作为训练集,接下来的 个时间点的数据作为验证集,逐步向前移动,直到覆盖整个时间序列。
from sklearn.datasets import load_iris
from sklearn.model_selection import StratifiedKFold
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
# 加载数据
iris = load_iris()
X = iris.data
y = iris.target
# 初始化分层k-折交叉验证器
#n_splits划分为几个折叠
#shuffle是否在拆分之前被打乱(随机化),False则按照顺序拆分
#random_state随机因子
strat_k_fold = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
# 创建一个K近邻分类器实例
knn = KNeighborsClassifier(n_neighbors=7)
# 进行交叉验证
accuracies = []
for train_index, test_index in strat_k_fold.split(X, y):
print(train_index, test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
# 数据预处理(标准化)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# 使用K近邻分类器进行训练
knn.fit(X_train_scaled, y_train)
# 输出每次折叠的准确性得分
score = knn.score(X_test,y_test)
print(score)
accuracies.append(score)#把分数添加到外面列表中
print(sum(accuracies)/len(accuracies))#平均得分
#使用StratifiedKFold来创建5个折叠,每个折叠中鸢尾花数据集的类别分布与整体数据集的分布一致。然后我们对每个折叠进行了训练和测试,计算了分类器的准确性
超参数搜索也叫网格搜索(Grid Search)
比如在KNN算法中,k是一个可以人为设置的参数,所以就是一个超参数。网格搜索能自动的帮助我们找到最好的超参数值。
class sklearn.model_selection.GridSearchCV(estimator, param_grid)
说明:
同时进行交叉验证(CV)、和网格搜索(GridSearch),GridSearchCV实际上也是一个估计器(estimator),同时它有几个重要属性:
best_params_ 最佳参数
best_score_ 在训练集中的准确率
best_estimator_ 最佳估计器
cv_results_ 交叉验证过程描述
best_index_最佳k在列表中的下标
参数:
estimator: scikit-learn估计器实例
param_grid:以参数名称(str)作为键,将参数设置列表尝试作为值的字典
示例: {"n_neighbors": [1, 3, 5, 7, 9, 11]}
cv: 确定交叉验证切分策略,值为:
(1)None 默认5折
(2)integer 设置多少折
如果估计器是分类器,使用"分层k-折交叉验证(StratifiedKFold)"。在所有其他情况下,使用KFold。
示例-鸢尾花分类
用KNN算法对鸢尾花进行分类,添加网格搜索和交叉验证
# 用KNN算法对鸢尾花进行分类,添加网格搜索和交叉验证
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
def knn_iris_gscv():
# 1)获取数据
iris = load_iris()
# 2)划分数据集
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=22)
# 3)特征工程:标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
# 4)KNN算法预估器, 这里就不传参数n_neighbors了,交给GridSearchCV来传递
estimator = KNeighborsClassifier()
# 加入网格搜索与交叉验证, GridSearchCV会让k分别等于1,2,5,7,9,11进行网格搜索偿试。cv=10表示进行10次交叉验证
estimator = GridSearchCV(estimator, param_grid={"n_neighbors": [1, 3, 5, 7, 9, 11]}, cv=10)
estimator.fit(x_train, y_train)
# 5)模型评估
# 方法1:直接比对真实值和预测值
y_predict = estimator.predict(x_test)
print("y_predict:\n", y_predict)
print("直接比对真实值和预测值:\n", y_test == y_predict)
# 方法2:计算准确率
score = estimator.score(x_test, y_test)
print("在测试集中的准确率为:\n", score) #0.9736842105263158
# 最佳参数:best_params_
print("最佳参数:\n", estimator.best_params_) #{'n_neighbors': 3}, 说明k=3时最好
# 最佳结果:best_score_
print("在训练集中的准确率:\n", estimator.best_score_) #0.9553030303030303
# 最佳估计器:best_estimator_
print("最佳估计器:\n", estimator.best_estimator_) # KNeighborsClassifier(n_neighbors=3)
# 交叉验证结果:cv_results_
print("交叉验证过程描述:\n", estimator.cv_results_)
#最佳参数组合的索引:最佳k在列表中的下标
print("最佳参数组合的索引:\n",estimator.best_index_)
#通常情况下,直接使用best_params_更为方便
return None
knn_iris_gscv()