Sklearn 0.20+ 的交叉验证? [英] Cross-validation for Sklearn 0.20+?

查看:23
本文介绍了Sklearn 0.20+ 的交叉验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行交叉验证,但遇到了一个错误:发现样本数量不一致的输入变量:[18, 1]"

I am trying to do cross validation and I am running into an error that says: 'Found input variables with inconsistent numbers of samples: [18, 1]'

我在 Pandas 数据框 (df) 中使用不同的列作为特征,最后一列作为标签.这源自加州大学欧文分校的机器学习存储库.在导入我过去使用过的交叉验证包时,我收到一个错误,提示它可能已贬值.我将运行决策树、SVM 和 K-NN.

I am using different columns in a pandas data frame (df) as the features, with the last column as the label. This is derived from the machine learning repository for UC Irvine. When importing the cross-validation package that I have used in the past, I am getting an error that it may have depreciated. I am going to be running a decision tree, SVM, and K-NN.

我的代码是这样的:

feature = [df['age'], df['job'], df['marital'], df['education'], df['default'], df['housing'], df['loan'], df['contact'],
       df['month'], df['day_of_week'], df['campaign'], df['pdays'], df['previous'], df['emp.var.rate'], df['cons.price.idx'],
       df['cons.conf.idx'], df['euribor3m'], df['nr.employed']]
label = [df['y']]

from sklearn.cross_validation import train_test_split
from sklearn.model_selection import cross_val_score
# Model Training 
x = feature[:]
y = label
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.5)

任何帮助都会很棒!

推荐答案

cross_validation 模块已弃用.新模块 model_selection 已取而代之.所以你用 cross_validation 所做的一切.现在在 model_selection 中可用.然后你上面的代码变成:

cross_validation module is deprecated. The new module model_selection has taken its place. So everything you did with cross_validation. is now available in model_selection. Then your above code becomes:

feature = [df['age'], df['job'], df['marital'], df['education'], df['default'], df['housing'], df['loan'], df['contact'],
       df['month'], df['day_of_week'], df['campaign'], df['pdays'], df['previous'], df['emp.var.rate'], df['cons.price.idx'],
       df['cons.conf.idx'], df['euribor3m'], df['nr.employed']]
label = [df['y']]

from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score

现在就声明 X 和 y 而言,为什么要将它们包装在列表中.像这样使用它们:

Now as far as declaring the X and y is concerned, why are you wrapping them in a list. Just use them like this:

feature = df[['age', 'job', 'marital', 'education', 'default', 'housing', 
              'loan', 'contact', 'month', 'day_of_week', 'campaign', 
              'pdays', 'previous', 'emp.var.rate', 'cons.price.idx', 
              'cons.conf.idx', 'euribor3m', 'nr.employed']]
label = df['y']

然后您可以简单地使用您的代码,而无需更改任何内容.

And then you can simply use your code, without changing anything.

# Model Training 
x = feature[:]
y = label
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.5)

对于您关于交叉验证中折叠的最后一个问题,sklearn 中有多个类可以执行此操作(取决于任务).请看:

And for your last question about folds in cross-validation, there are multiple classes in sklearn which does this (depending upon task). Please have a look at:

其中包含折叠迭代器.请记住,所有这些都存在于 model_selection 包中.

Which contains fold iterators. And remember, all this is present in model_selection package.

这篇关于Sklearn 0.20+ 的交叉验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆