Train a SVM (Support Vector Machine) classifier with Scikit-learn

Home / Uncategorized / Train a SVM (Support Vector Machine) classifier with Scikit-learn

Question:
I want to train different classifier with using Scikit-learn with following code: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()]

for 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 properly but when I reach to the SVM classifier it throws following exception:Traceback (most recent call last):File "/Users/mac/PycharmProjects/GraphLstm/classifier.py", line 87, in
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 the reason and How can I fix that?


Answer:
The fit function of the knn classifier allows a matrix as y-value. For the svm this is not allowed. The error message tries to hint you on a disallowed y-shape
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *