ValueError: (‘Unexpected parameters in params‘, dict_keys([‘cv‘]))

Posted Data+Science+Insight

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ValueError: (‘Unexpected parameters in params‘, dict_keys([‘cv‘]))相关的知识,希望对你有一定的参考价值。

ValueError: ('Unexpected parameters in params', dict_keys(['cv']))

目录

ValueError: ('Unexpected parameters in params', dict_keys(['cv']))

问题:

解决:

完整错误:


问题:

构建lassocv模型并使用path函数构建系数衰减曲线(系数衰减图);

发生了错误。

# alpha spaces
# However, if we increase α to a much higher value, we see that literally none of the features are being used
# ie. all features weight will be 0
alphas = np.logspace(-10, 10, 100, base = 10)


# Create lasso regression with three alpha values
regr_cv = LassoCV(alphas=alphas, cv = 5, max_iter = 1e4,selection = 'random',random_state=42)
#regr_cv = LassoCV(alphas=alphas, cv = 5, max_iter = 1e6)

# Fit the linear regression
reg_scaler = RobustScaler()
# reg_scaler = MinMaxScaler()

regr_cv.fit(reg_scaler.fit_transform(X), y)
# regr_cv.fit(X, y)




def regressor_coef(regr_cv):
    
    coefs = regr_cv.path(X, y, alphas=alphas,cv = 3, max_iter = 1e4,selection = 'random')[1].T
    plt.figure()
    plt.semilogx(regr_cv.alphas_,coefs, '-')
    plt.axvline(regr_cv.alpha_,color = 'black',ls="--")
    plt.xlabel('Lambda')
    plt.ylabel('Coefficients')
    plt.savefig("lasso_coefficient.jpeg",bbox_inches='tight')
    plt.show()


regressor_coef(regr_cv)

解决:

删掉path函数中的cv参数就OK了。

sklearn版本升级后path函数的参数有变动。

# alpha spaces
# However, if we increase α to a much higher value, we see that literally none of the features are being used
# ie. all features weight will be 0
alphas = np.logspace(-10, 10, 100, base = 10)


# Create lasso regression with three alpha values
regr_cv = LassoCV(alphas=alphas, cv = 5, max_iter = 1e4,selection = 'random',random_state=42)
#regr_cv = LassoCV(alphas=alphas, cv = 5, max_iter = 1e6)

# Fit the linear regression
reg_scaler = RobustScaler()
# reg_scaler = MinMaxScaler()

regr_cv.fit(reg_scaler.fit_transform(X), y)
# regr_cv.fit(X, y)




def regressor_coef(regr_cv):
    
    coefs = regr_cv.path(X, y, alphas=alphas, max_iter = 1e4,selection = 'random')[1].T
    plt.figure()
    plt.semilogx(regr_cv.alphas_,coefs, '-')
    plt.axvline(regr_cv.alpha_,color = 'black',ls="--")
    plt.xlabel('Lambda')
    plt.ylabel('Coefficients')
    plt.savefig("lasso_coefficient.jpeg",bbox_inches='tight')
    plt.show()


regressor_coef(regr_cv)

 

完整错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-101-4bf43fcef4cb> in <module>
----> 1 regressor_coef(regr_cv)

<ipython-input-100-9c6531f705fe> in regressor_coef(regr_cv)
      1 def regressor_coef(regr_cv):
      2 
----> 3     coefs = regr_cv.path(X, y, alphas=alphas,cv = 3, max_iter = 1e4,selection = 'random')[1].T
      4     plt.figure()
      5     plt.semilogx(regr_cv.alphas_,coefs, '-')

D:\\anaconda\\lib\\site-packages\\sklearn\\linear_model\\_coordinate_descent.py in lasso_path(X, y, eps, n_alphas, alphas, precompute, Xy, copy_X, coef_init, verbose, return_n_iter, positive, **params)
    361         positive=positive,
    362         return_n_iter=return_n_iter,
--> 363         **params,
    364     )
    365 

D:\\anaconda\\lib\\site-packages\\sklearn\\linear_model\\_coordinate_descent.py in enet_path(X, y, l1_ratio, eps, n_alphas, alphas, precompute, Xy, copy_X, coef_init, verbose, return_n_iter, positive, check_input, **params)
    507 
    508     if len(params) > 0:
--> 509         raise ValueError("Unexpected parameters in params", params.keys())
    510 
    511     # We expect X and y to be already Fortran ordered when bypassing

ValueError: ('Unexpected parameters in params', dict_keys(['cv']))

Scikit-learn项目始于scikits.learn,这是David Cournapeau的Google Summer of Code项目。它的名称源于它是“ SciKit”(SciPy工具包)的概念,它是SciPy的独立开发和分布式第三方扩展。原始代码库后来被其他开发人员重写。2010年费边Pedregosa,盖尔Varoquaux,亚历山大Gramfort和Vincent米歇尔,全部由法国国家信息与自动化研究所的罗屈昂库尔,法国,把该项目的领导和做出的首次公开发行在二月一日2010在各种scikits中,scikit-learn以及scikit-image在2012年11月被描述为“维护良好且受欢迎” 。Scikit-learn是GitHub上最受欢迎的机器学习库之一。

Scikit-learn主要是用Python编写的,并且广泛使用numpy进行高性能的线性代数和数组运算。此外,用Cython编写了一些核心算法来提高性能。支持向量机由围绕LIBSVM的Cython包装器实现 ; 逻辑回归和线性支持向量机的相似包装围绕LIBLINEAR。在这种情况下,可能无法使用Python扩展这些方法。
Scikit-learn与许多其他Python库很好地集成在一起,例如matplotlib和plotly用于绘图,numpy用于数组矢量化,pandas数据帧,scipy等。
 

参考:sklearn

参考:

以上是关于ValueError: (‘Unexpected parameters in params‘, dict_keys([‘cv‘]))的主要内容,如果未能解决你的问题,请参考以下文章

Unexpected end of input 和 Unexpected token var 和 Unexpected token ;

Import Unexpected identifier + SyntaxError: Unexpected string

python IndentationError: unexpected indent

“set_unexpected”在 VC2010 中不起作用?

com.fasterxml.jackson.core.JsonParseException: Unexpected character

VMWare vCenter Converter 迁移错误Unexpected Exception