python - Train a SVM (Support Vector Machine) classifier with Scikit-learn -
i want train different classifier using scikit-learn
following code multi-label classification problem:
names = [ "nearest neighbors", "linear svm", "rbf svm", "gaussian process", "decision tree", "random forest", "neural net", "adaboost", "naive bayes", "qda"] classifiers = [ kneighborsclassifier(3), svc(c=0.025), svc(gamma=2, c=1), gaussianprocessclassifier(1.0 * rbf(1.0)), decisiontreeclassifier(max_depth=5), randomforestclassifier(max_depth=5), mlpclassifier(alpha=0.5), adaboostclassifier(), gaussiannb(), quadraticdiscriminantanalysis()] name, clf in izip(names, classifiers): clf.fit(x_train, y_train) score = clf.score(x_train, y_test) print name, score
kneighbors
classifier works when reach svm classifier throws following exception:
traceback (most recent call last): file "/users/mac/pycharmprojects/graphlstm/classifier.py", line 87, in <module> clf.fit(x_train, y_train) file "/library/python/2.7/site-packages/sklearn/svm/base.py", line 151, in fit x, y = check_x_y(x, y, dtype=np.float64, order='c', accept_sparse='csr') file "/library/python/2.7/site-packages/sklearn/utils/validation.py", line 526, in check_x_y y = column_or_1d(y, warn=true) file "/library/python/2.7/site-packages/sklearn/utils/validation.py", line 562, in column_or_1d raise valueerror("bad input shape {0}".format(shape)) valueerror: bad input shape (9280, 39)
what's reason , how can fix that?
edit: commented @vivek following classifier allowed multi-label classification:
sklearn.tree.decisiontreeclassifier sklearn.tree.extratreeclassifier sklearn.ensemble.extratreesclassifier sklearn.neighbors.kneighborsclassifier sklearn.neural_network.mlpclassifier sklearn.neighbors.radiusneighborsclassifier sklearn.ensemble.randomforestclassifier sklearn.linear_model.ridgeclassifiercv
since multi-label classification problem, not estimators in scikit able handle them inherently. documentation provides list of estimators support multi-label out of box various tree based estimators or others :
sklearn.tree.decisiontreeclassifier sklearn.tree.extratreeclassifier sklearn.ensemble.extratreesclassifier sklearn.neighbors.kneighborsclassifier ... ...
however there strategies one-vs-all can employed train required estimator (which doesn't support multilabel directly). sklearn estimator onevsrestclassifier made this.
see documentation here more details it.
Comments
Post a Comment