sklearn ShuffleSplit 出现“__init__() 参数 'n_splits' 的多个值”错误
Posted
技术标签:
【中文标题】sklearn ShuffleSplit 出现“__init__() 参数 \'n_splits\' 的多个值”错误【英文标题】:"__init__() got multiple values for argument 'n_splits'" error with sklearn ShuffleSplitsklearn ShuffleSplit 出现“__init__() 参数 'n_splits' 的多个值”错误 【发布时间】:2017-05-30 04:18:38 【问题描述】:我来了
init() 获得了参数“n_splits”的多个值
这一行的错误:
cv = ShuffleSplit(n_splits = 10, test_size = 0.2, random_state = 0)
在以下代码中:
import matplotlib.pyplot as pl
import numpy as np
import sklearn.model_selection as curves
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import ShuffleSplit, train_test_split, learning_curve
def ModelLearning(X, y):
""" Calculates the performance of several models with varying sizes of training data.
The learning and testing scores for each model are then plotted. """
# Create 10 cross-validation sets for training and testing
cv = ShuffleSplit(n_splits = 10, test_size = 0.2, random_state = 0)
# Generate the training set sizes increasing by 50
train_sizes = np.rint(np.linspace(1, X.shape[0]*0.8 - 1, 9)).astype(int)
# Create the figure window
fig = pl.figure(figsize=(10,7))
# Create three different models based on max_depth
for k, depth in enumerate([1,3,6,10]):
# Create a Decision tree regressor at max_depth = depth
regressor = DecisionTreeRegressor(max_depth = depth)
# Calculate the training and testing scores
sizes, train_scores, test_scores = learning_curve(regressor, X, y, \
train_sizes = train_sizes, cv = cv, scoring = 'r2')
# Find the mean and standard deviation for smoothing
train_std = np.std(train_scores, axis = 1)
train_mean = np.mean(train_scores, axis = 1)
test_std = np.std(test_scores, axis = 1)
test_mean = np.mean(test_scores, axis = 1)
# Subplot the learning curve
ax = fig.add_subplot(2, 2, k+1)
ax.plot(sizes, train_mean, 'o-', color = 'r', label = 'Training Score')
ax.plot(sizes, test_mean, 'o-', color = 'g', label = 'Testing Score')
ax.fill_between(sizes, train_mean - train_std, \
train_mean + train_std, alpha = 0.15, color = 'r')
ax.fill_between(sizes, test_mean - test_std, \
test_mean + test_std, alpha = 0.15, color = 'g')
# Labels
ax.set_title('max_depth = %s'%(depth))
ax.set_xlabel('Number of Training Points')
ax.set_ylabel('Score')
ax.set_xlim([0, X.shape[0]*0.8])
ax.set_ylim([-0.05, 1.05])
# Visual aesthetics
ax.legend(bbox_to_anchor=(1.05, 2.05), loc='lower left', borderaxespad = 0.)
fig.suptitle('Decision Tree Regressor Learning Performances', fontsize = 16, y = 1.03)
fig.tight_layout()
fig.show()
我知道这个错误通常表明参数顺序不正确,但这应该是正确的。这是 sklearn 文档中的示例:
rs = ShuffleSplit(n_splits=3, test_size=.25, random_state=0)
我也尝试删除 n_splits 参数,因为 10 是默认值:
cv = ShuffleSplit(test_size = 0.2, random_state = 0)
这会产生同样的错误。
我正在将代码从 python 2.7 转换为 3.5,并从早期版本的 sklearn 转换为 0.18.1,所以我可能遗漏了一些东西,但我不知道它可能是什么。调用 ShuffleSplit 的行中的参数似乎也是有序的:
大小,train_scores,test_scores = learning_curve(regressor, X, y, \ train_sizes = train_sizes, cv = cv, score = 'r2')
调用函数的 X 和 y 与 python 2.7 一起使用,所以它们也应该没问题。
追溯:
TypeError Traceback (most recent call last)
<ipython-input-33-191abc15bbd7> in <module>()
1 # Produce learning curves for varying training set sizes and maximum depths
----> 2 vs.ModelLearning(features, prices)
E:\Python\machine-learning-master\projects\boston_housing\visuals.py in ModelLearning(X, y)
21
22 # Create 10 cross-validation sets for training and testing
---> 23 cv = ShuffleSplit(n_splits = 10, test_size = 0.2, random_state = 0)
24
25 # Generate the training set sizes increasing by 50
TypeError: __init__() got multiple values for argument 'n_splits'
【问题讨论】:
请发布整个回溯消息。 添加了对原始帖子的追溯 fwiw,如果它对其他回答者有帮助,这里是库源:github.com/scikit-learn/scikit-learn/blob/… 乍一看,虽然我不熟悉 sklearn,但 OP 的代码看起来不错 你有哪个版本的sklearn?检查您的版本的文档字符串 - 它可能已过时并且您传递的参数不存在。在早期版本中,使用 n_iter 而不是 n_splits。 与 sklearn.model_selection.StratifiedKFold 有同样的问题,正如这里报告的那样:github.com/dmlc/xgboost/issues/2102,sklearn 版本为 0.18.1 【参考方案1】:代替:
from sklearn.model_selection import ShuffleSplit
用途:
from sklearn.cross_validation import ShuffleSplit
StratifiedShuffleSplit
会出现同样的错误,再次使用
cross_validation
不是 model_selection
。
【讨论】:
以上是关于sklearn ShuffleSplit 出现“__init__() 参数 'n_splits' 的多个值”错误的主要内容,如果未能解决你的问题,请参考以下文章
sklearn可视化不同数据划分方法的差异:KFold, ShuffleSplit,StratifiedKFold, GroupKFold, StratifiedShuffleSplit.......