如何修复使用`make_pipeline`时出现的`TypeError: too many positional arguments`错误。

huangapple go评论65阅读模式
英文:

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
)

huangapple
  • 本文由 发表于 2023年7月6日 13:32:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定