简体   繁体   English

TypeError: fit() 缺少 1 个必需的位置参数:'y'

[英]TypeError: fit() missing 1 required positional argument: 'y'

I am trying to predict economic cycles using Gaussian Naive Bayes "Classifier".我正在尝试使用高斯朴素贝叶斯“分类器”来预测经济周期。

data (input X):数据(输入 X):

             SPY    Interest Rate    Unemployment   Employment  CPI
Date                    
1997-01-02   56.05     7.82            9.7           3399.9     159.100
1997-02-03   56.58     7.65            9.8           3402.8     159.600
1997-03-03   54.09     7.90            9.9           3414.7     160.000

target (output Y):目标(输出 Y):

    Economy
0   Expansion
1   Expansion
2   Expansion
3   Expansion

Below is my code:下面是我的代码:

from sklearn.naive_bayes import GaussianNB
from sklearn import metrics
from sklearn.cross_validation import train_test_split
X = data
Y = target
model = GaussianNB
X_train, X_test, Y_train, Y_test = train_test_split(X,Y)
model.fit(X_train, Y_train)

Below is Error:以下是错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-132-b0975752a19f> in <module>()
  6 model = GaussianNB
  7 X_train, X_test, Y_train, Y_test = train_test_split(X,Y)
  ----> 8 model.fit(X_train, Y_train)

  TypeError: fit() missing 1 required positional argument: 'y'

What am I doing wrong?我究竟做错了什么? How can I resolve this issue /error?我该如何解决这个问题/错误?

你忘记了括号“()”:

model = GaussianNB()

Whenever you try to initialize/ define an object of a class you must call its own constructor to create one object for you.每当您尝试初始化/定义类的对象时,您必须调用它自己的构造函数来为您创建一个对象。 The constructor may have parameters or none.构造函数可能有参数,也可能没有。 In your case GaussianNB is a class from sklearn which has a non-parametric constructor by default.在您的情况下, GaussianNB是来自 sklearn 的类,默认情况下具有非参数构造函数

obj_model =  GaussianNB()

So simply we do create an object with empty parenthesis which simply means default constructor .因此,我们确实创建了一个带有空括号的对象,这仅表示默认构造函数

You forgot to put () after your model, gaussianNB .你忘了在你的模型gaussianNB后面加上() Try doing this in line 6:尝试在第 6 行执行此操作:

model = GaussianNB()

I am pretty sure it will solve the problem.我很确定它会解决问题。

Just in case someone else stumbles over this, suffering from the same root cause as I did: This error can also occur when you are trying to call the method "fit" as a static method (classmethod) on the class instead of calling it on an instantiated object of the class.以防万一其他人偶然发现这一点,遭受与我相同的根本原因:当您尝试将方法“fit”作为类上的静态方法(classmethod)而不是调用它时,也会发生此错误类的实例化对象。 This applies also to other classifiers in other frameworks, eg PySpark.这也适用于其他框架中的其他分类器,例如 PySpark。

Eg this won't work:例如,这将不起作用:

model = LogisticRegression.fit(data)

But this will:但这将:

log_reg = LogisticRegression()
model = log_reg.fit(data)

You just need to add () for the model.您只需要为模型添加 ()。

from sklearn.naive_bayes import GaussianNB
from sklearn import metrics
from sklearn.cross_validation import train_test_split
X = data
Y = target
model = GaussianNB()
X_train, X_test, Y_train, Y_test = train_test_split(X,Y)
model.fit(X_train, Y_train)

This works..这工作..

您只需要为模型添加()

model = GaussianNB()

You forgot the parentheses sign in front of GaussianNB,你忘记了 GaussianNB 前面的括号符号,

correct line is Model = GaussianNB()<\/strong>正确的线是 Model = GaussianNB()<\/strong>

"

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.35, `
`random_state=100)
from sklearn.linear_model import LinearRegression 
lm = LinearRegression
lm.fit(X_test,y_test)

Good Luck祝你好运

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

相关问题 逻辑回归类型错误:fit() 缺少 1 个必需的位置参数:'y' - Logistic Regression TypeError: fit() missing 1 required positional argument: 'y' 程序不起作用“ TypeError:fit()缺少1个必需的位置参数:&#39;y&#39;” - program is not working “TypeError: fit() missing 1 required positional argument: 'y'” “fit() 缺少 1 个必需的位置参数:'y'”错误 - “fit() missing 1 required positional argument: 'y'” error TypeError:fit()缺少1个必需的位置参数:“ X” - TypeError: fit() missing 1 required positional argument: 'X' 获取 TypeError fit() 缺少 1 个必需的位置参数:'self' - Getting TypeError fit() missing 1 required positional argument: 'self' 类型错误:fit_transform() 缺少 1 个必需的位置参数:&#39;X&#39; - TypeError: fit_transform() missing 1 required positional argument: 'X' 类型错误:flag1() 缺少 1 个必需的位置参数:&#39;y&#39; - TypeError: flag1() missing 1 required positional argument: 'y' 缺少 1 个必需的位置参数:'y' - Missing 1 required positional argument: 'y' TypeError:缺少1个必需的位置参数 - TypeError: missing 1 required positional argument 类型错误:add() 缺少 1 个必需的位置参数:“实例” - TypeError: add() missing 1 required positional argument: 'instance'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM