40

I am trying to predict economic cycles using Gaussian Naive Bayes "Classifier".

data (input 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) :

 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 ?

3
  • 26
    model = GaussianNB() Commented Mar 14, 2016 at 20:09
  • Thanks.. however, I got another error now. Any idea how to resolve it? /Users/d/anaconda/lib/python3.4/site-packages/sklearn/utils/validation.py:515: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel(). y = column_or_1d(y, warn=True) Commented Mar 14, 2016 at 20:33
  • 3
    I changed line 5 to Y = target.values.ravel() and it worked. Commented Mar 14, 2016 at 20:43

5 Answers 5

97

You forgot the parenthesis "()" in:

model = GaussianNB() 
Sign up to request clarification or add additional context in comments.

Comments

12

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.

obj_model = GaussianNB() 

So simply we do create an object with empty parenthesis which simply means default constructor.

Comments

9

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. This applies also to other classifiers in other frameworks, e.g. PySpark.

E.g. this won't work:

model = LogisticRegression.fit(data) 

But this will:

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

Comments

2

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..

Comments

-3
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

1 Comment

still missing the parenthesis and I think it's off topic

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.