简体   繁体   English

在python中使用SVM进行分类

[英]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. 我有一个包含2列的csv数据集,我想训练SVM分类器,然后在给出X值时预测Y值。

My Dataset format 我的数据集格式

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

Y array is (1,2,3,4) only Y数组仅(1,2,3,4)

Python code is : Python代码是:

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] ). 如警告所述,您传入的是列向量( [(1,), (2,), (3,)] )而不是行向量( [1, 2, 3] )。 Do numpy.array(x).ravel() instead of numpy.array(x) to flatten it. 执行numpy.array(x).ravel()而不是numpy.array(x)来展平它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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