英文:
How to fix TypeError: too many positional arguments when using make_pipeline
问题
由于某种原因,我在以下代码中收到了一个TypeError错误。
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import validation_curve
def PolynomialRegression(degree=2, **kwargs):
return make_pipeline(PolynomialFeatures(degree), LinearRegression(**kwargs))
train_score, val_score = validation_curve(PolynomialRegression(), X, y,
'polynomialfeatures__degree',
degree, cv=7)
错误信息如下:
File ~/opt/miniconda3/lib/python3.10/inspect.py:3108, in Signature._bind(self, args, kwargs, partial)
3104 else:
3105 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
3106 # Looks like we have no parameter for this positional
3107 # argument
-> 3108 raise TypeError(
3109 'too many positional arguments') from None
3111 if param.kind == _VAR_POSITIONAL:
3112 # We have an '*args'-like argument, let's fill it with
3113 # all positional arguments we have left and move on to
3114 # the next phase
3115 values = [arg_val]
TypeError: too many positional arguments
英文:
For some reason I'm getting a TypeError in the following code.
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import validation_curve
def PolynomialRegression(degree=2, **kwargs):
return make_pipeline(PolynomialFeatures(degree), LinearRegression(**kwargs))
train_score, val_score = validation_curve(PolynomialRegression(), X, y,
'polynomialfeatures__degree',
degree, cv=7)
> File ~/opt/miniconda3/lib/python3.10/inspect.py:3108, in Signature._bind(self, args, kwargs, partial)
3104 else:
3105 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
3106 # Looks like we have no parameter for this positional
3107 # argument
-> 3108 raise TypeError(
3109 'too many positional arguments') from None
3111 if param.kind == _VAR_POSITIONAL:
3112 # We have an '*args'-like argument, let's fill it with
3113 # all positional arguments we have left and move on to
3114 # the next phase
3115 values = [arg_val]
TypeError: too many positional arguments
答案1
得分: 0
你分享的代码似乎有一个小错误。在validation_curve
函数中,degree
参数应该作为一个范围或数值数组传递,而不是一个单一的数值。
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import validation_curve
def PolynomialRegression(degree=2, **kwargs):
return make_pipeline(PolynomialFeatures(degree), LinearRegression(**kwargs))
degrees = [1, 2, 3, 4, 5] # 用于验证曲线的示例度数
train_score, val_score = validation_curve(
PolynomialRegression(), X, y,
param_name='polynomialfeatures__degree',
param_range=degrees,
cv=7
)
英文:
The code you shared seems to have a small error. The degree
parameter in the validation_curve
function should be passed as a range or array of values, not as a single value.
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import validation_curve
def PolynomialRegression(degree=2, **kwargs):
return make_pipeline(PolynomialFeatures(degree), LinearRegression(**kwargs))
degrees = [1, 2, 3, 4, 5] # Example degrees for the validation curve
train_score, val_score = validation_curve(
PolynomialRegression(), X, y,
param_name='polynomialfeatures__degree',
param_range=degrees,
cv=7
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论