简体   繁体   中英

How to train SVM using python?

I'm trying to train SVM on dataset with only two column like:

  • 1 4.5456436
  • 0 2.4353453
  • 1 3.5435636
  • 1 5.4235354
  • 0 1.4235345

I have tried:

x = np.array([[1],[0],[1],[1]])
y = np.array([[4.5456436],[2.4353453],[3.5435636],[5.4235354]])

clf = svm.SVC()
clf.fit(y,x)

for these lines it works correctly, but the problem occurs when I import the array from dataset file, I got an error:

ValueError: The number of classes has to be greater than one; got 1

although the output and the type in the two cases are the same.

imported data from the dataset code is:

def read(dir):
    x = []
    y = []
    with open(dir) as f:
        lines = f.readlines()
    for i in range(len(lines)):
        x.append(lines[i][0]);y.append(lines[i][1:])
    x = np.array([[int(i)] for i in x])
    y = np.array([[float(i)] for i in y])

any suggestion, thanks in advance.

Posting the comment as answer to just close the question.

The error is that there is only one type of class (label) in target. See, in the example you posted above (x = np.array([[1],[0],[1],[1]])), there are two categories to classify (0 and 1).

But when you import the dataset from file, target has only type of category for all available samples. Please check the arrays loaded from your file.

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