python - ValueError: Unknown label type: 'unknown' when plotting SVM classifiers in the iris dataset -
i tried plot svm classifiers in iris dataset the starting code can found here. extended pandas dataframe 4 column want plot in same manner.
i made 4 columns code :
iris = iris.assign(sepalratio = iris['sepallengthcm'] / iris['sepalwidthcm']).assign(petalratio = iris['petallengthcm'] / iris['petalwidthcm']).assign(sepalmultiplied = iris['sepallengthcm'] * iris['sepalwidthcm']).assign(petalmultiplied = iris['petallengthcm'] * iris['petalwidthcm'])
i made specieid colunm :
d = {"iris-setosa" : 0, "iris-versicolor": 1, "iris-virginica": 2} iris['specieid'] = iris['species'].map(d).fillna(-1)
then, extracted columns plot dataframe after plotting error :
--------------------------------------------------------------------------- valueerror traceback (most recent call last) <ipython-input-49-9724675f32fa> in <module>() 77 xx, yy = make_meshgrid(x0, x1) 78 ---> 79 clf, title, ax in zip(models, titles, sub.flatten()): 80 plot_contours(ax, clf, xx, yy, 81 cmap=plt.cm.coolwarm, alpha=0.8) <ipython-input-49-9724675f32fa> in <genexpr>(.0) 62 svm.svc(kernel='rbf', gamma=0.7, c=c), 63 svm.svc(kernel='poly', degree=3, c=c)) ---> 64 models = (clf.fit(x, y) clf in models) 65 66 # title plots c:\users\masc\appdata\local\continuum\anaconda3\lib\site-packages\sklearn\svm\base.py in fit(self, x, y, sample_weight) 150 151 x, y = check_x_y(x, y, dtype=np.float64, order='c', accept_sparse='csr') --> 152 y = self._validate_targets(y) 153 154 sample_weight = np.asarray([] c:\users\masc\appdata\local\continuum\anaconda3\lib\site-packages\sklearn\svm\base.py in _validate_targets(self, y) 518 def _validate_targets(self, y): 519 y_ = column_or_1d(y, warn=true) --> 520 check_classification_targets(y) 521 cls, y = np.unique(y_, return_inverse=true) 522 self.class_weight_ = compute_class_weight(self.class_weight, cls, y_) c:\users\masc\appdata\local\continuum\anaconda3\lib\site-packages\sklearn\utils\multiclass.py in check_classification_targets(y) 170 if y_type not in ['binary', 'multiclass', 'multiclass-multioutput', 171 'multilabel-indicator', 'multilabel-sequences']: --> 172 raise valueerror("unknown label type: %r" % y_type) 173 174 valueerror: unknown label type: 'unknown'
my modified code :
from sklearn import svm iris = pd.read_csv("iris.csv") # iris dataset pandas dataframe def make_meshgrid(x, y, h=.02): """create mesh of points plot in parameters ---------- x: data base x-axis meshgrid on y: data base y-axis meshgrid on h: stepsize meshgrid, optional returns ------- xx, yy : ndarray """ x_min, x_max = x.min() - 1, x.max() + 1 y_min, y_max = y.min() - 1, y.max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) return xx, yy def plot_contours(ax, clf, xx, yy, **params): """plot decision boundaries classifier. parameters ---------- ax: matplotlib axes object clf: classifier xx: meshgrid ndarray yy: meshgrid ndarray params: dictionary of params pass contourf, optional """ z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) z = z.reshape(xx.shape) out = ax.contourf(xx, yy, z, **params) return out # import data play #iris = datasets.load_iris() iris_numpy_array = iris.as_matrix(columns=none) print (iris_numpy_array) # take first 2 features. avoid using two-dim dataset x = iris_numpy_array[:, [1, 2]] print (x) y = iris_numpy_array[:, [10]] y = y.ravel() print (y) # create instance of svm , fit out data. not scale our # data since want plot support vectors c = 1.0 # svm regularization parameter models = (svm.svc(kernel='linear', c=c), svm.linearsvc(c=c), svm.svc(kernel='rbf', gamma=0.7, c=c), svm.svc(kernel='poly', degree=3, c=c)) models = (clf.fit(x, y) clf in models) # title plots titles = ('svc linear kernel', 'linearsvc (linear kernel)', 'svc rbf kernel', 'svc polynomial (degree 3) kernel') # set-up 2x2 grid plotting. fig, sub = plt.subplots(2, 2) plt.subplots_adjust(wspace=0.4, hspace=0.4) x0, x1 = x[:, 0], x[:, 1] xx, yy = make_meshgrid(x0, x1) clf, title, ax in zip(models, titles, sub.flatten()): plot_contours(ax, clf, xx, yy, cmap=plt.cm.coolwarm, alpha=0.8) ax.scatter(x0, x1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k') ax.set_xlim(xx.min(), xx.max()) ax.set_ylim(yy.min(), yy.max()) ax.set_xlabel('sepal length') ax.set_ylabel('sepal width') ax.set_xticks(()) ax.set_yticks(()) ax.set_title(title) plt.show()
the content of x , y same in code example in code difference the're extracted pandas dataframe.
the original code is:
print(__doc__) iris = pd.read_csv("iris.csv") # iris dataset pandas dataframe import numpy np import matplotlib.pyplot plt sklearn import svm, datasets def make_meshgrid(x, y, h=.02): """create mesh of points plot in parameters ---------- x: data base x-axis meshgrid on y: data base y-axis meshgrid on h: stepsize meshgrid, optional returns ------- xx, yy : ndarray """ x_min, x_max = x.min() - 1, x.max() + 1 y_min, y_max = y.min() - 1, y.max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) return xx, yy def plot_contours(ax, clf, xx, yy, **params): """plot decision boundaries classifier. parameters ---------- ax: matplotlib axes object clf: classifier xx: meshgrid ndarray yy: meshgrid ndarray params: dictionary of params pass contourf, optional """ z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) z = z.reshape(xx.shape) out = ax.contourf(xx, yy, z, **params) return out # import data play iris = datasets.load_iris() # take first 2 features. avoid using two-dim dataset x = iris.data[:, :2] y = iris.target # create instance of svm , fit out data. not scale our # data since want plot support vectors c = 1.0 # svm regularization parameter models = (svm.svc(kernel='linear', c=c), svm.linearsvc(c=c), svm.svc(kernel='rbf', gamma=0.7, c=c), svm.svc(kernel='poly', degree=3, c=c)) models = (clf.fit(x, y) clf in models) # title plots titles = ('svc linear kernel', 'linearsvc (linear kernel)', 'svc rbf kernel', 'svc polynomial (degree 3) kernel') # set-up 2x2 grid plotting. fig, sub = plt.subplots(2, 2) plt.subplots_adjust(wspace=0.4, hspace=0.4) x0, x1 = x[:, 0], x[:, 1] xx, yy = make_meshgrid(x0, x1) clf, title, ax in zip(models, titles, sub.flatten()): plot_contours(ax, clf, xx, yy, cmap=plt.cm.coolwarm, alpha=0.8) ax.scatter(x0, x1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k') ax.set_xlim(xx.min(), xx.max()) ax.set_ylim(yy.min(), yy.max()) ax.set_xlabel('sepal length') ax.set_ylabel('sepal width') ax.set_xticks(()) ax.set_yticks(()) ax.set_title(title) plt.show()
i solved problem using template makes svm plot :
from sklearn import svm mlxtend.plotting import plot_decision_regions x = iris[['sepallengthcm', 'sepalwidthcm']] y = iris['specieid'] clf = svm.svc(decision_function_shape = 'ovo') clf.fit(x.values, y.values) # plot decision region using mlxtend's awesome plotting function plot_decision_regions(x=x.values, y=y.values, clf=clf, legend=2) # update plot object x/y axis labels , figure title plt.xlabel(x.columns[0], size=14) plt.ylabel(x.columns[1], size=14) plt.title('svm decision region boundary', size=16)
Comments
Post a Comment