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

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

How to fix TypeError: too many positional arguments when using make_pipeline

问题

由于某种原因,我在以下代码中收到了一个TypeError错误。

  1. from sklearn.preprocessing import PolynomialFeatures
  2. from sklearn.linear_model import LinearRegression
  3. from sklearn.pipeline import make_pipeline
  4. from sklearn.model_selection import validation_curve
  5. def PolynomialRegression(degree=2, **kwargs):
  6. return make_pipeline(PolynomialFeatures(degree), LinearRegression(**kwargs))
  7. train_score, val_score = validation_curve(PolynomialRegression(), X, y,
  8. 'polynomialfeatures__degree',
  9. degree, cv=7)

错误信息如下:

  1. File ~/opt/miniconda3/lib/python3.10/inspect.py:3108, in Signature._bind(self, args, kwargs, partial)
  2. 3104 else:
  3. 3105 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
  4. 3106 # Looks like we have no parameter for this positional
  5. 3107 # argument
  6. -> 3108 raise TypeError(
  7. 3109 'too many positional arguments') from None
  8. 3111 if param.kind == _VAR_POSITIONAL:
  9. 3112 # We have an '*args'-like argument, let's fill it with
  10. 3113 # all positional arguments we have left and move on to
  11. 3114 # the next phase
  12. 3115 values = [arg_val]
  13. TypeError: too many positional arguments
英文:

For some reason I'm getting a TypeError in the following code.

  1. from sklearn.preprocessing import PolynomialFeatures
  2. from sklearn.linear_model import LinearRegression
  3. from sklearn.pipeline import make_pipeline
  4. from sklearn.model_selection import validation_curve
  5. def PolynomialRegression(degree=2, **kwargs):
  6. return make_pipeline(PolynomialFeatures(degree), LinearRegression(**kwargs))
  7. train_score, val_score = validation_curve(PolynomialRegression(), X, y,
  8. 'polynomialfeatures__degree',
  9. 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参数应该作为一个范围或数值数组传递,而不是一个单一的数值。

  1. from sklearn.preprocessing import PolynomialFeatures
  2. from sklearn.linear_model import LinearRegression
  3. from sklearn.pipeline import make_pipeline
  4. from sklearn.model_selection import validation_curve
  5. def PolynomialRegression(degree=2, **kwargs):
  6. return make_pipeline(PolynomialFeatures(degree), LinearRegression(**kwargs))
  7. degrees = [1, 2, 3, 4, 5] # 用于验证曲线的示例度数
  8. train_score, val_score = validation_curve(
  9. PolynomialRegression(), X, y,
  10. param_name='polynomialfeatures__degree',
  11. param_range=degrees,
  12. cv=7
  13. )
英文:

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.

  1. from sklearn.preprocessing import PolynomialFeatures
  2. from sklearn.linear_model import LinearRegression
  3. from sklearn.pipeline import make_pipeline
  4. from sklearn.model_selection import validation_curve
  5. def PolynomialRegression(degree=2, **kwargs):
  6. return make_pipeline(PolynomialFeatures(degree), LinearRegression(**kwargs))
  7. degrees = [1, 2, 3, 4, 5] # Example degrees for the validation curve
  8. train_score, val_score = validation_curve(
  9. PolynomialRegression(), X, y,
  10. param_name='polynomialfeatures__degree',
  11. param_range=degrees,
  12. cv=7
  13. )

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:

确定