简体   繁体   English

类型错误:fit() 缺少 1 个必需的位置参数:'y'(使用 sklearn - ExtraTreesRegressor)

[英]TypeError: fit() missing 1 required positional argument: 'y' (using sklearn - ExtraTreesRegressor)

Just trying out the Sklearn python library and I re-purposed some code I was using for Linear regression to fit a regression tree model as an example I saw (here's the example code):只是试用了 Sklearn python 库,我重新调整了一些用于线性回归的代码,以适应回归树 model 作为我看到的示例(这里是示例代码):

def fit(self, X, y):
        """
        Fit a Random Forest model to data `X` and targets `y`.

        Parameters
        ----------
        X : array-like
            Input values.
        y: array-like
            Target values.
        """
        self.X = X
        self.y = y
        self.n = self.X.shape[0]
        self.model = ExtraTreesRegressor(**self.params)
        self.model.fit(X, y)

Here's the code I've written/repurposed这是我编写/重新调整用途的代码

data = pd.read_csv("rmsearch.csv", sep=",")
data = data[["price", "type", "number_bedrooms"]]
predict = "price"

X = np.array(data.drop([predict], 1))
y = np.array(data[predict])
x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(X, y, test_size=0.2)

etr = ensemble.ExtraTreesRegressor
etr.fit(x_train, y_train)
acc = etr.score(x_test, y_test)
print("Accuracy; ", acc)

and I am getting this error:我收到此错误:

etr.fit(x_train, y_train)
TypeError: fit() missing 1 required positional argument: 'y'

I know fit() takes 'X', 'y', and 'sample_weight' as input.我知道 fit() 将“X”、“y”和“sample_weight”作为输入。 but, sample_weight defaults to none.但是,sample_weight 默认为无。 the other examples haven't helped me much but it could also be that I'm fairly new to python and not able to spot a simple coding error.其他示例对我帮助不大,但也可能是我对 python 还很陌生,无法发现简单的编码错误。

fit() documentation:适合()文档:

https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.ExtraTreesRegressor.html#sklearn.ensemble.ExtraTreesRegressor.fit https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.ExtraTreesRegressor.html#sklearn.ensemble.ExtraTreesRegressor.fit

Thanks for your help in advance.提前感谢您的帮助。

The problem is here问题就在这里

etr = ensemble.ExtraTreesRegressor
etr.fit(x_train, y_train)

You need to instantiate ensemble.ExtraTreesRegressor before calling fit on it.在调用fit之前,您需要实例化ensemble.ExtraTreesRegressor Change this code to将此代码更改为

etr = ensemble.ExtraTreesRegressor()
etr.fit(x_train, y_train)

You get the seemingly strange error that y is missing because .fit is an instance method, so the first argument to this function is actually self .你会得到一个看似奇怪的错误,即y丢失了,因为.fit是一个实例方法,所以这个 function 的第一个参数实际上是self When you call .fit on an instance, self is passed automatically.当您在实例上调用.fit时,会自动传递self If you call .fit on the class (as opposed to the instance), you would have to supply self .如果您在 class(而不是实例)上调用.fit ,则必须提供self So your code is equivalent to ensemble.ExtraTreesRegressor.fit(self=x_train, x=y_train) .所以你的代码相当于ensemble.ExtraTreesRegressor.fit(self=x_train, x=y_train)

For an example of the difference, please see the example below.有关差异的示例,请参见下面的示例。 The two forms are functionally equivalent, but you can see that the first form is clunky.两个 forms 在功能上是等价的,但是你可以看到第一个形式很笨拙。

from sklearn import ensemble

# Synthetic data.
x = [[0]]
y = [1]

myinstance = ensemble.ExtraTreesRegressor()
ensemble.ExtraTreesRegressor.fit(myinstance, x, y)

etr = ensemble.ExtraTreesRegressor()
etr.fit(x, y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sklearn:TypeError:fit() 缺少 1 个必需的位置参数:“x” - sklearn: TypeError: fit() missing 1 required positional argument: 'x" TypeError: fit() 缺少 1 个必需的位置参数:'y' while GridSearching CNN - TypeError: fit() missing 1 required positional argument: 'y' while GridSearching CNN 实现逻辑回归“TypeError: fit() missing 1 required positional argument: 'y'” - Implementing Logistic Regression “TypeError: fit() missing 1 required positional argument: 'y'” 逻辑回归类型错误:fit() 缺少 1 个必需的位置参数:'y' - Logistic Regression TypeError: fit() missing 1 required positional argument: 'y' scikit-learn-TypeError:fit()缺少1个必需的位置参数:“ y” - scikit-learn - TypeError: fit() missing 1 required positional argument: 'y' Python 出错:TypeError:fit() 缺少 1 个必需的位置参数:'y' - Error with Python: TypeError: fit() missing 1 required positional argument: 'y' TypeError:fit()缺少1个必需的位置参数:'y' - TypeError: fit() missing 1 required positional argument: 'y' please fit() 缺少 1 个必需的位置参数:'y' - fit() missing 1 required positional argument: 'y' 类型错误:x 缺少 1 个必需的位置参数:y - TypeError: x missing 1 required positional argument: y TypeError:scatter()缺少1个必需的位置参数:“ y” - TypeError: scatter() missing 1 required positional argument: 'y'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM