scikit学习clf.fit /得分模型的准确性

时间:2013-10-16 16:43:19

标签: python machine-learning scikit-learn classification

我正在构建一个模型clf

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

然后我想用分数

来查看我的模型准确度
clf.score(x_train, y_train)

结果为0.92

我的目标是测试测试,所以我使用

clf.score(x_test, y_test)

我得到了0.77,所以我认为它会给我与下面这段代码相同的结果

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

我得到0.54。有人可以帮我理解为什么0.77 > 0.54

1 个答案:

答案 0 :(得分:6)

如果x_trainy_trainx_testy_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
相关问题