类型错误:fit() 缺少 1 个必需的位置参数:'y'(使用 sklearn - ExtraTreesRegressor) [英] TypeError: fit() missing 1 required positional argument: 'y' (using sklearn - ExtraTreesRegressor)

查看:128
本文介绍了类型错误:fit() 缺少 1 个必需的位置参数:'y'(使用 sklearn - ExtraTreesRegressor)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是尝试了 Sklearn python 库,我重新利用了一些用于线性回归的代码来拟合回归树模型作为我看到的示例(这是示例代码):

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):

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)

我收到此错误:

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

我知道 fit() 将 'X'、'y' 和 'sample_weight' 作为输入.但是,sample_weight 默认为 none.其他示例对我没有多大帮助,但也可能是我对 Python 相当陌生,无法发现简单的编码错误.

I know fit() takes 'X', 'y', and 'sample_weight' as input. but, sample_weight defaults to none. 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.

fit() 文档:

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

提前感谢您的帮助.

推荐答案

问题来了

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

在调用 fit 之前,您需要实例化 ensemble.ExtraTreesRegressor.将此代码更改为

You need to instantiate ensemble.ExtraTreesRegressor before calling fit on it. Change this code to

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

你会得到一个看似奇怪的错误,y 丢失了,因为 .fit 是一个实例方法,所以这个函数的第一个参数实际上是 self.当您在实例上调用 .fit 时,会自动传递 self.如果您在类(而不是实例)上调用 .fit,则必须提供 self.所以你的代码相当于 ensemble.ExtraTreesRegressor.fit(self=x_train, x=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. When you call .fit on an instance, self is passed automatically. If you call .fit on the class (as opposed to the instance), you would have to supply self. So your code is equivalent to 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.

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)

这篇关于类型错误:fit() 缺少 1 个必需的位置参数:'y'(使用 sklearn - ExtraTreesRegressor)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆