"__init __()对于参数'n_splits'具有多个值"sklearn ShuffleSplit错误 [英] "__init__() got multiple values for argument 'n_splits'" error with sklearn ShuffleSplit

查看:44
本文介绍了"__init __()对于参数'n_splits'具有多个值"sklearn ShuffleSplit错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到

init ()对于参数'n_splits'具有多个值

init() got multiple values for argument 'n_splits'

此行的错误:

cv = ShuffleSplit(n_splits = 10,test_size = 0.2,random_state = 0)

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文档中的示例:

I am aware this error usually indicates incorrect parameter order, however this should be the correct one. This is the example in the sklearn documentation:

rs = ShuffleSplit(n_splits = 3,test_size = .25,random_state = 0)

rs = ShuffleSplit(n_splits=3, test_size=.25, random_state=0)

我也尝试删除n_splits参数,因为无论如何默认值为10:

I also tried removing the n_splits parameter since 10 is the default value anyway:

cv = ShuffleSplit(test_size = 0.2,random_state = 0)

cv = ShuffleSplit(test_size = 0.2, random_state = 0)

这将产生相同的错误.

我正在将代码从python 2.7转换为3.5,并将代码从较早版本的sklearn转换为0.18.1,所以我可能错过了一些东西,但我不知道它可能是什么.调用ShuffleSplit的行中的参数似乎也井井有条:

I am converting the code from python 2.7 to 3.5 and from an earlier version of sklearn to 0.18.1 so I might have missed something, but I have no idea what it could be. The parameters in the line that calls the ShuffleSplit seem to be in order too:

大小,train_scores,test_scores = learning_curve(regressor,X,y,\train_sizes = train_sizes,cv = cv,得分='r2')

sizes, train_scores, test_scores = learning_curve(regressor, X, y, \ train_sizes = train_sizes, cv = cv, scoring = 'r2')

调用该函数的X和y与python 2.7一起使用,因此它们也应该没问题.

The X and y that the function is called with worked with python 2.7 so they should be fine too.

跟踪:

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'

推荐答案

而不是:

from sklearn.model_selection import ShuffleSplit

使用:

from sklearn.cross_validation import ShuffleSplit

对于 StratifiedShuffleSplit ,您可以得到相同的错误,再次使用 cross_validation 而不是 model_selection .

You can get the same error for the StratifiedShuffleSplit, again use cross_validation not model_selection.

这篇关于"__init __()对于参数'n_splits'具有多个值"sklearn ShuffleSplit错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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