TypeError: predict() takes 2 positional arguments but 3 were given

TypeError: predict() takes 2 positional arguments but 3 were given

我在 Whosebug 上查找了这个错误并找到了几个帖子,但没有人解决这个具体情况。

我有以下数据框:

输入变量和输出变量在这段代码中定义:

xcol=["h","o","p","d","ddlt","devdlt","sl","lt"]
ycol=["Q","r"]
x=df[xcol].values
y=df[ycol].values

我的目标是根据输入 (x) 猜测输出值 Q 和 r。 我尝试了两种方法,但都失败了。对于第一个,我尝试了一个多输出回归器。

我首先将测试数据和训练数据分开:

import numpy as np
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
y_train = y_train.ravel()
y_test = y_test.ravel()

然后导入函数:

from sklearn.multioutput import MultiOutputRegressor

然后尝试预测 Q & r:

reg= MultiOutputRegressor(estimator=100, n_jobs=None)
reg=reg.predict(X_train, y_train)

这给了我错误:

TypeError: predict() takes 2 positional arguments but 3 were given

我做错了什么,我该如何解决?

接下来我尝试的是神经网络。分配 x 和 y 列后,我制作了神经网络:

# neural network class definition
class neuralNetwork:

#Step 1: 
def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
    #set number of nodes in each input, hidden, output layer
    self.inodes = inputnodes
    self.hnodes = hiddennodes
    self.onodes = outputnodes

    #link weight matrices, wih and who (weights in hidden en output layers), 
    # we are going to create matrices for the multiplication of it to get an 
    # output
    # weights inside the arrays (matrices) are w_i_j, where link is from node 
    # i to node j in the next layer
    #w11 w21
    #w12 w22 etc
    self.wih = numpy.random.normal(0.0,pow(self.inodes,-0.5),( self.hnodes, 
    self.inodes))
    self.who = numpy.random.normal(0.0,pow(self.hnodes,-0.5),( self.onodes, 
    self.hnodes))

    # setting the learning rate
    self.lr = learningrate

    # activation function is the sigmoid function
    self.activation_function = lambda x: scipy.special.expit(x)

    pass

    #Step 2:
def train(self, inputs_list, targets_list):
    #convert input lists to 2d array (matrice)
    inputs = numpy.array(inputs_list, ndmin=2).T
    targets = numpy.array(targets_list, ndmin=2).T

    #calculate signals into hidden layer
    hidden_inputs = numpy.dot(self.wih, inputs)
    #calculate signals emerging from hidden layer
    hidden_outputs = self.activation_function(hidden_inputs)

    #calculate signals into final output layer
    final_inputs = numpy.dot(self.who, hidden_outputs)
    #calculate signals emerging from final output layer
    final_outputs = self.activation_function(final_inputs)
    # output layer error is the (target-actual)
    output_errors = targets -final_outputs
    #hidden layer error is the output_errors, split by weights, recombined 
    at hidden nodes
    hidden_errors = numpy.dot(self.who.T, output_errors)

    #update the weights for the links between the hidden and output layers
    self.who += self.lr * numpy.dot((output_errors*final_outputs * (1.0- 
    final_outputs)),numpy.transpose(hidden_outputs))

    # update the weights for the links between the input and hidden layers
    self.wih += self.lr*numpy.dot((hidden_errors*hidden_outputs*(1.0- 
    hidden_outputs)),numpy.transpose(inputs))

    pass

    #Step 3
def query(self, inputs_list):
    #convert input lists to 2d array (matrice)
    inputs = numpy.array(inputs_list, ndmin=2).T

    #calculate signals into hidden layer
    hidden_inputs = numpy.dot(self.wih, inputs)
    #calculate signals emerging from hidden layer
    hidden_outputs = self.activation_function(hidden_inputs)

    #calculate signals into final output layer
    final_inputs = numpy.dot(self.who, hidden_outputs)
    #calculate signals emerging from final output layer
    final_outputs = self.activation_function(final_inputs)

    return final_outputs

然后我创建了一个神经网络实例:

   #Creating instance of neural network 

   #number of input, hidden and output nodes
   input_nodes = 8
   hidden_nodes = 100
   output_nodes = 2

   #learning rate is 0.8
   learning_rate = 0.8

   #create instance of neural network
   n = neuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)

我有 8 个输入和 2 个输出需要预测。

然后我训练了神经网络:

# train the neural network
# go through all records in the training data set 
for record in df:
    #scale and shift te inputs
    inputs = x
    #create the target output values 
    targets = y
    n.train(inputs, targets)
    pass

然后我想查询猜测的输出,现在出错了:

所以我想用 Q (Q*) & r (r*) 的猜测在数据框中制作 2 个额外的列:

df["Q*","r*"] = n.query(x)

我真的不知道如何正确地做到这一点。上面的代码给我错误:

ValueError: Length of values does not match length of index

感谢任何帮助。

史蒂文

关于您问题的第一部分 (MultiOutputRegressor),您的代码存在几个问题...

首先,MultiOutputRegressorestimator 参数不应该是一个数字,但是,正如 docs 所说:

estimator : estimator object

An estimator object implementing fit and predict.

因此,对于 example,要使用具有默认参数的随机森林,您应该使用

reg = MultiOutputRegressor(RandomForestRegressor()) 

(有关更多示例,请参阅

其次,在你的代码中你永远不会适合你的回归量;你应该添加

reg.fit(X_train, y_train)

定义后

第三,predict不以ground truth值(此处为y_train)为参数,仅以特征(X_train)为参数;再次来自 docs

predict(X)

Predict multi-output variable using a model trained for each target variable.

Parameters: X : (sparse) array-like, shape (n_samples, n_features)

Data.

Returns: y : (sparse) array-like, shape (n_samples, n_outputs)

Multi-output targets predicted across multiple predictors. Note: Separate models are generated for each predictor.

由于您还在代码中传递了 y_train,因此您会收到一个参数过多的预期错误;只需将其更改为 reg.predict(X_train),就可以了。