欢迎访问 生活随笔!

凯发ag旗舰厅登录网址下载

当前位置: 凯发ag旗舰厅登录网址下载 > 编程资源 > 编程问答 >内容正文

编程问答

sklearn模型评选择与评估 -凯发ag旗舰厅登录网址下载

发布时间:2025/1/21 编程问答 17 豆豆
凯发ag旗舰厅登录网址下载 收集整理的这篇文章主要介绍了 sklearn模型评选择与评估 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

sklearn模型评选择与评估

1. 数据集划分

1.1 k折交叉验证

1.1.1 k折交叉验证算法原理
a. 将全部训练及s分成k个不相交的子集,假设s中样本个数为m,那么,每一个子集的训练样本个数为m/k相应的子集称做{s1,s2,...,sk}\{s_{1},s_{2},...,s_{k}\}{s1,s2,...,sk}
b. 每次从分好的训练只集中拿出一个测试集。其他k-1一个作为训练集。
c. 在k-1一个训练集上训练出学习器模型,把这个模型放到测试集上,得到分类率.
d. 计算k求得分类率的平均值。作为该模型的真实分类率。

1.1.2 stratifiedkfold划分后,训练集、测试集的比例与原始数据集一致。
1.1.3优缺点
优点:充分利用了所有样本
缺点:计算繁琐。计算k次,测试k次
1.1.4代码

返回的是每一折的索引

def kfold_practice():x = np.arange(12)x = x.reshape(6, 2)y = np.arange(15, 21)kf = model_selection.kfold(n_splits=3) # 定义一个k折分割器 还有参数shuffle,random_state'kfold.split(x)返回的是索引,训练样本和测试样本是什么要将索引带到数据集中去'for train_index, test_index in kf.split(x):print("train index:", train_index,"test index:", test_index)x_train, x_test = x[train_index], x[test_index]y_train, y_test = y[train_index], y[test_index]print("train subset:\n", x_train)print("test subset:\n", x_test) kfold_practice() train index: [2 3 4 5] test index: [0 1] train subset:[[ 4 5][ 6 7][ 8 9][10 11]] test subset:[[0 1][2 3]] train index: [0 1 4 5] test index: [2 3] train subset:[[ 0 1][ 2 3][ 8 9][10 11]] test subset:[[4 5][6 7]] train index: [0 1 2 3] test index: [4 5] train subset:[[0 1][2 3][4 5][6 7]] test subset:[[ 8 9]]

1.2 留一法(loo)

1.2.1 留一法
假设在n个样本中,将每一个样本作为测试集,剩下的n-1个作为作为训练样本,这样得到n个分类器、n个测试结果,n个结果平均来衡量模型的性能。
loo与kfold相比,当k< 1.2.2 留p法
假设在n个样本中,将p个样本作为测试集,剩下的n-p个作为作为训练样本,这样得到cnpc_{n}^{p}cnp个训练-测试集对(train-test pairs),当p>1是,测试集将发生重叠。当p=1时,变成了留一法。
1.2.3 代码
与kfold类似,划分后返回的也是索引

def leave_one_out():x = np.arange(5,13)x = x.reshape(4, 2)y = np.arange(15, 21)loo = model_selection.leaveoneout()for train_index,test_index in loo.split(x):print("train index:", train_index, "test index:", test_index)x_train, x_test = x[train_index], x[test_index]# y_train, y_test = y[train_index], y[test_index]print("train x:", x_train, "test x:", x_test)

1.3 随机划分法

产生指定数量的训练/测试数据集划分。指定训练集和测试集的比例或者数量。
首先对样本随机打乱,然后划分train/test对。
可以通过random_state控制随机序列发生器,使得运算结果可重现。
stratifiedshufflesplit是shufflesplit的变体,返回分层划分。划分后每一个类的比例与原始数据集比例一致。

2. 超参数优化方法

2.1 网格搜索穷举式超参数优化方法

gridsearchcv(eatimator,param_grid=param_grid)
参数
eatimator:分类器
param_grid:参数网格

from sklearn.datasets import load_digits from sklearn.ensemble import forest from scipy import stats import numpy as np import pandas as pddata = load_digits() print(data.keys()) x,y = data['data'],data['target'] x_train, x_test, y_train, y_test = train_test_split(x,y,train_size=0.7,test_size=0.3)model = forest.randomforestclassifier(n_estimators=10) # 待优化的超参数 print(model.get_params().keys())# 获取模型的参数名 def gridsearchcv(model):param_grid = {'criterion': ['gini', 'entropy'],'max_depth': [3, none],'min_samples_split': np.arange(2,11),'min_samples_leaf': np.arange(1,11),}start = time.time()clf = gridsearchcv(model,param_grid=param_grid)clf.fit(x_train, y_train)duration = time.time() - startprint('*best_params_*:\n', clf.best_params_)# print('*best_estimator_*:\n',clf.best_estimator_)print('模型在测试集的得分', clf.score(x_test, y_test))print('总共耗时:', duration)cv_result = pd.dataframe.from_dict(clf.cv_results_)print(type(clf.cv_results_))print(clf.cv_results_.keys())# with open('gridsearchcv_result.csv', 'w') as f: cv_result.to_csv(f)

参数网格如下。一共有2*2*9*10=360种组合方式。gridsearchcv在每种参数组合下计算在n折交叉验证的每一折中计算训练集和测试集上的得分。

clf.cv_results_是一个字典类型的数据,结果如下。rank_test_score对应的是某中参数组合下,模型在测试集中结果的排名。

2.2 随机采样式超参数优化方法

randomizedsearchcv(eatimator, param_distributions=param_distribution, n_iter=10,cv=3)
参数
eatimator:分类器
param_distributions:参数分布。参数值按参数分布选取
n_iter:迭代次数,选多少组参数组合

#模型与gridsearchcv相同 def randomizedcv(model):param_distribution = {'criterion': ['gini', 'entropy'],'max_depth': [3, none],'min_samples_split': stats.randint(2, 11),'min_samples_leaf': stats.randint(1, 11),}start = time.time()clf = randomizedsearchcv(model, param_distributions=param_distribution, n_iter=10,cv=3)clf.fit(x_train, y_train)duration = time.time() - startprint('*best_params_*:\n', clf.best_params_)# print('*best_estimator_*:\n',clf.best_estimator_)print(type(clf.cv_results_))top = 3# clf.cv_results_ 是选择参数的日志信息# cv_result = pd.dataframe.from_dict(clf.cv_results_)# with open('cv_result.csv', 'w') as f: cv_result.to_csv(f)# report(clf.cv_results_,top)# # print('模型在测试集的得分', clf.score(x_test, y_test))# print('总共耗时:', duration)


3. 模型验证方法

3.1 通过交叉验证计算得分

model_selection.cross_val_score(eatimator,x,y,cv=cv,n_jobs=4)
参数
eatimator:实现fit()的学习器
x:array_like,需要学习的数据,可以是列表或2d数组
y:array_like,可选的。监督学习中样本的真实目标值
scoring:string,callable or none,可选的,默认是none
   none - 调用eatimator的score函数
   string or callable需实现scorer(estimator,x,y)
cv: int,交叉验证生成器对象或者一个迭代器,可选的,默认none
   决定交叉验证划分策略,cv的可选项:
a. none:使用默认的3折交叉验证
b.interger:k折stratifiedkfold的k
c.交叉验证生成器对象
d.一个能产生train/test划分的迭代器对象。
返回值
scores:浮点数组,shape=len(list(cv)) 在测试集上的得分
每一次交叉验证的得分形成一个数组(默认是3折交叉验证,生成3个数组)

#coding:utf-8 from sklearn.datasets import load_digits from sklearn.model_selection import cross_val_score,shufflesplit from sklearn.svm import svc import numpy as np from matplotlib import pyplot as plt data = load_digits() # print(data.keys()) x,y = data['data'],data['target']# data['data'] 一维数组, data['images'] 二维数组(8x8) # print(len(x)) model = svc(gamma=0.001) cv = shufflesplit(n_splits=10,test_size=0.2) c_s = np.logspace(-6,0,6) print(c_s) result = [] for c in c_s:model.c = ct = cross_val_score(model,x,y,cv=cv,n_jobs=4)print(np.mean(t))result.append(np.mean(t)) # plt.plot(c_s,result,'o') plt.plot(c_s,result) plt.xscale('log')# 对数坐标 plt.xlabel('c') plt.ylabel('accuracy') plt.rcparams['font.sans-serif']=['simhei'] #显示中文 plt.title(r'参数c对svm的影响') plt.show()

3.2 对每个输入数据点产生交叉验证估计

model_selection .cross_val_predict(estimator,x,y=none,cv=none,n_jobs=1,method='predict')
参数

  • method:string,optional,default:predict. invokes the passed method of the passed estimator.
    默认使用传入模型的预测方法。

3.3 计算并绘制模型的学习率曲线

learning_curve(estimator, x, y, groups=none, train_sizes=np.linspace(0.1, 1.0, 5), cv='warn', scoring=none, exploit_incremental_learning=false, n_jobs=none, pre_dispatch="all", verbose=0, shuffle=false, random_state=none, error_score='raise-deprecating')
计算指定学习器在不同大小的训练集上经过交叉验证的训练得分和测试得分。
算法过程
首先用一个交叉验证生成器将整体数据集划分成k折,每一折划分成训练集和测试集。从每一折的训练集中取出一定比例(比如0.1)作为训练集,然后不断增加子集的比例,在这些自己上训练模型。然后计算在对应自己下的模型在整体训练集和测试集上的得分。

参数

  • train_sizes :array_like,shape(n_tricks,),dtype:float or int.
    用于指定训练样本子集的相对数量或者绝对数量。
    如果是浮点数,则是训练集中样本数量的百分比,(0,1]之间。
    如果是整型数,则指的是训练子集的样本数量,这个数量不能超过训练样本总体数量。默认值np.linspace(0.1,1.0,5)

返回值

  • train_sizes_abs:array,shape=(n_unique_tricks,),dtype:int。返回train_sizes去掉重复值后的结果。
  • train_scores:array,shape=(n_unique_tricks,n_cv_folds),scores on training sets.
  • test_scores:array,shape=(n_unique_tricks,n_cv_folds),scores on testsets.
from matplotlib import pyplot as plt import sklearn from sklearn.datasets import load_digits from sklearn.model_selection import learning_curve from sklearn.model_selection import train_test_split,shufflesplit,randomizedsearchcv from sklearn.ensemble import forestimport numpy as np data = load_digits() print(data.keys()) x,y = data['data'],data['target']# data['data'] 一维数组, data['images'] 二维数组(8x8) print(len(x)) # model = svc(gamma=0.001) model = forest.randomforestclassifier(n_estimators=10)cv = shufflesplit(n_splits=10,test_size=0.2) clf = model t,train_scores,test_scores = learning_curve(clf,x,y,cv=cv,n_jobs=4) # print(t) print('*'*20) print('train_scores:\n',train_scores) print('*'*20) print('test_scores:\n',test_scores) test_score = np.mean(test_scores,axis = 1) print(test_score) plt.plot(test_score) plt.show()

3.4 计算并绘制模型的验证曲线

model_selection.validation_curve(estimator, x, y, param_name, param_range, groups=none, cv='warn', scoring=none, n_jobs=none, pre_dispatch="all", verbose=0, error_score='raise-deprecating')
当某个参数变化时,在该参数每一个取值下计算出模型在训练集和测试集上的得分。(与gridsearchcv类似,但是只可以计算在测试集上的得分,且只能有一个参数)
参数

  • param_name:string,参数名
  • param_range:array_like,shape(n_values,),参数的取值范围
    返回值
  • train_scoresarray_like,shape(n_trick,n_cv_folds),在训练集上的得分。
  • test_scoresarray_like,shape(n_trick,n_cv_folds),在测试集上的得分。

总结

以上是凯发ag旗舰厅登录网址下载为你收集整理的sklearn模型评选择与评估的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。

网站地图