简体   繁体   中英

Classification using SVM in python

I have a csv dataset with 2 columns I want to train SVM classifier and then predict Y value when X value is given.

My Dataset format

    X,Y
    1.84166666681401,2
    ....
    1.283333,4

Y array is (1,2,3,4) only

Python code is :

import numpy
from sklearn import svm
import pandas as pd

x = pd.read_csv('train.csv', usecols=['1.84166666681401'])
y = pd.read_csv('train.csv', usecols=['2'])

x=numpy.array(x)
y=numpy.array(y)
clf = svm.SVC(C=1,kernel="linear")
clf.fit(x,y)
print(clf.predict(0.7882234))

But I have error when run the code

    Warning (from warnings module):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/utils/validation.py", line 578
    y = column_or_1d(y, warn=True)
DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().

How to solve this ?

As the warning says, you passed in a column vector ( [(1,), (2,), (3,)] ) rather than a row vector ( [1, 2, 3] ). Do numpy.array(x).ravel() instead of numpy.array(x) to flatten it.

x=numpy.array(x) y = y.reshape(len(dataset),1)之前添加这段代码

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM