0

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 ?

user31562
  • 97
  • 1
  • 11
  • Possible duplicate of [A column-vector y was passed when a 1d array was expected](https://stackoverflow.com/questions/34165731/a-column-vector-y-was-passed-when-a-1d-array-was-expected) – prabhat mishra Jan 20 '18 at 18:59

2 Answers2

0

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.

a spaghetto
  • 1,072
  • 2
  • 14
  • 22
0

Add this piece of code before x=numpy.array(x) y = y.reshape(len(dataset),1)

prabhat mishra
  • 202
  • 2
  • 14