Scikit learn clf.fit/ model evaluation

I will build a model I will clfsay

clf = MultinomialNB()
clf.fit(x_train, y_train)

then I want to see my model accuracy using estimates

clf.score(x_train, y_train)

the result was 0.92

My goal is to test the test, so I use

clf.score(x_test, y_test)

This one I got 0.77, so I thought it would give me the result the same as this code below

clf.fit(X_train, y_train).score(X_test, y_test)

I got it 0.54. Can someone help me understand why 0.77 > 0.54?

+4
source share
1 answer

, x_train, y_train, x_test y_test . iris, , .

>>> from sklearn.naive_bayes import MultinomialNB
>>> from sklearn.cross_validation import train_test_split
>>> from sklearn.datasets import load_iris
>>> from copy import copy
# prepare dataset
>>> iris = load_iris()
>>> X = iris.data[:, :2]
>>> y = iris.target
>>> X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# model
>>> clf1 = MultinomialNB()
>>> clf2 = MultinomialNB()
>>> print id(clf1), id(clf2) # two different instances
 4337289232 4337289296
>>> clf1.fit(X_train, y_train)
>>> print clf1.score(X_test, y_test)
 0.633333333333
>>> print clf2.fit(X_train, y_train).score(X_test, y_test)
 0.633333333333
+6

All Articles