how to resolve this ValueError: only 2 non-keyword arguments accepted sklearn python

天涯浪子 提交于 2020-12-11 09:01:22

问题


hello i am new to sklearn in python and iam trying to learn it and use this module to predict some numbers based on two features here is the error i am getting:

ValueError: only 2 non-keyword arguments accepted

and here is my code:

    from sklearn.linear_model import LinearRegression
    import numpy as np

    trainingData = np.array([[861, 16012018], [860, 12012018], [859, 9012018], [858, 5012018], [857, 2012018], [856, 29122017], [855, 26122017], [854, 22122017], [853, 19122017]])
    trainingScores = np.array([11,18,23,33,34,6],[10,19,21,33,34,1], [14,18,22,23,31,6],[16,22,29,31,33,10],[21,24,27,30,31,6],[1,14,15,20,27,7],[1,9,10,11,15,8],[2,9,27,31,35,1],[7,13,18,22,33,2])

    clf = LinearRegression(fit_intercept=True)
    clf.fit(trainingScores,trainingData)

   predictionData = np.array([862, 19012018 ])
   x=clf.predict(predictionData)
   print(x)

回答1:


I am not sure what you are trying to do here, but change this line:

trainingScores = np.array([11,18,23,33,34,6],[10,19,21,33,34,1], [14,18,22,23,31,6],[16,22,29,31,33,10],[21,24,27,30,31,6],[1,14,15,20,27,7],[1,9,10,11,15,8],[2,9,27,31,35,1],[7,13,18,22,33,2])

to this (Notice the extra square brackets around your data):

trainingScores = np.array([[11,18,23,33,34,6],[10,19,21,33,34,1], [14,18,22,23,31,6],[16,22,29,31,33,10],[21,24,27,30,31,6],[1,14,15,20,27,7],[1,9,10,11,15,8],[2,9,27,31,35,1],[7,13,18,22,33,2]])

Then change the order of params in fit() like this:

clf.fit(trainingData,trainingScores)

And finally change prediction data like this (again look at the extra square brackets):

predictionData = np.array([[862, 19012018]])

After that your code will run.




回答2:


You are doing a linear regression code in ML and try to change this line with

trainingScores = np.array(
  [11,18,23,33,34,6],
  [10,19,21,33,34,1], 
  [14,18,22,23,31,6],
  [16,22,29,31,33,10],
  [21,24,27,30,31,6],
  [1,14,15,20,27,7],
  [1,9,10,11,15,8],
  [2,9,27,31,35,1],
  [7,13,18,22,33,2]
)


来源:https://stackoverflow.com/questions/48342143/how-to-resolve-this-valueerror-only-2-non-keyword-arguments-accepted-sklearn-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!