ValueError:输入形状错误(100,752)

时间:2016-04-29 21:46:18

标签: python python-2.7

我试图使用scikit运行朴素的贝叶斯模型,但它给了我这个错误,我似乎无法弄清楚问题是什么?

# Gaussian Naive Bayes
from sklearn import datasets
from sklearn import metrics
from sklearn.naive_bayes import GaussianNB
# load the iris datasets
#dataset = datasets.load_iris()
# fit a Naive Bayes model to the data
model = GaussianNB()
model.fit(training_set, testing_set)
print(model)
# make predictions
expected = testing_set
predicted = model.predict(training_set)
# summarize the fit of the model
print(metrics.classification_report(expected, predicted))
print(metrics.confusion_matrix(expected, predicted))

我认为问题出在training_set和testing_set中,但我不确定并且不知道如何解决它,我已将数据拆分为培训和测试

training_set=newdf.sample(frac=0.7,random_state=200)
testing_set=newdf.drop(train.index)

如果我使用虹膜数据

,代码可以正常工作
# Gaussian Naive Bayes
from sklearn import datasets
from sklearn import metrics
from sklearn.naive_bayes import GaussianNB
# load the iris datasets
dataset = datasets.load_iris()
# fit a Naive Bayes model to the data
model = GaussianNB()
model.fit(dataset.data, dataset.target)
print(model)
# make predictions
expected = dataset.target
predicted = model.predict(dataset.data)
# summarize the fit of the model
print(metrics.classification_report(expected, predicted))
print(metrics.confusion_matrix(expected, predicted))

如何删除此错误?

Traceback

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-77-072d1f083190> in <module>()
      7 # fit a Naive Bayes model to the data
      8 model = GaussianNB()
----> 9 model.fit(training_set, testing_set)
     10 print(model)
     11 # make predictions

D:\python\lib\site-packages\sklearn\naive_bayes.py in fit(self, X, y, sample_weight)
    171             Returns self.
    172         """
--> 173         X, y = check_X_y(X, y)
    174         return self._partial_fit(X, y, np.unique(y), _refit=True,
    175                                  sample_weight=sample_weight)

D:\python\lib\site-packages\sklearn\utils\validation.pyc in check_X_y(X, y, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
    513                         dtype=None)
    514     else:
--> 515         y = column_or_1d(y, warn=True)
    516         _assert_all_finite(y)
    517     if y_numeric and y.dtype.kind == 'O':

D:\python\lib\site-packages\sklearn\utils\validation.pyc in column_or_1d(y, warn)
    549         return np.ravel(y)
    550 
--> 551     raise ValueError("bad input shape {0}".format(shape))
    552 
    553 

ValueError: bad input shape (100, 752)

0 个答案:

没有答案
相关问题