In [3]:
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn import datasets
from sklearn import svm
from sklearn.model_selection import cross_val_score

In [2]:
iris = datasets.load_iris()
iris.data.shape, iris.target.shape
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.4, random_state=0)

print X_train.shape, y_train.shape
print X_test.shape, y_test.shape


(90, 4) (90,)
(60, 4) (60,)

In [19]:
clf = svm.SVC(kernel='linear', C=1)
scores = cross_val_score(clf, iris.data, iris.target, cv=5)
print scores 
print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))


[ 0.96666667  1.          0.96666667  0.96666667  1.        ]
Accuracy: 0.98 (+/- 0.03)

In [8]:
print clf.predict(iris.data)


---------------------------------------------------------------------------
NotFittedError                            Traceback (most recent call last)
<ipython-input-8-4edd7facdf96> in <module>()
----> 1 print clf.predict(iris.data)

/home/ilmira/.conda/envs/readmision/lib/python2.7/site-packages/sklearn/svm/base.pyc in predict(self, X)
    546             Class labels for samples in X.
    547         """
--> 548         y = super(BaseSVC, self).predict(X)
    549         return self.classes_.take(np.asarray(y, dtype=np.intp))
    550 

/home/ilmira/.conda/envs/readmision/lib/python2.7/site-packages/sklearn/svm/base.pyc in predict(self, X)
    306         y_pred : array, shape (n_samples,)
    307         """
--> 308         X = self._validate_for_predict(X)
    309         predict = self._sparse_predict if self._sparse else self._dense_predict
    310         return predict(X)

/home/ilmira/.conda/envs/readmision/lib/python2.7/site-packages/sklearn/svm/base.pyc in _validate_for_predict(self, X)
    435 
    436     def _validate_for_predict(self, X):
--> 437         check_is_fitted(self, 'support_')
    438 
    439         X = check_array(X, accept_sparse='csr', dtype=np.float64, order="C")

/home/ilmira/.conda/envs/readmision/lib/python2.7/site-packages/sklearn/utils/validation.pyc in check_is_fitted(estimator, attributes, msg, all_or_any)
    766 
    767     if not all_or_any([hasattr(estimator, attr) for attr in attributes]):
--> 768         raise NotFittedError(msg % {'name': type(estimator).__name__})
    769 
    770 

NotFittedError: This SVC instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.

In [ ]: