Kratos Kratos - 1 month ago 21
Python Question

Turning list to svm input

I am implementing an svm model with python and scikit learn. I have reached to a point where I have selected and transformed my features and merged them to a list that looks like this:

[[17, 14, 14, 7, 14, 14, 14, 7, 14, 14, 1],
[14, 14, 7, 14, 14, 14, 7, 14, 14, 7, 1],
[14, 7, 14, 14, 14, 7, 14, 14, 7, 14, 1],
[7, 14, 14, 14, 7, 14, 14, 7, 14, 7, 1],
[14, 14, 14, 7, 14, 14, 7, 14, 7, 14, 1],
[14, 14, 7, 14, 14, 7, 14, 7, 14, 7, 1],
[14, 7, 14, 14, 7, 14, 7, 14, 7, 13, 1],
[7, 14, 14, 7, 14, 7, 14, 7, 13, 7, 1],
[14, 14, 7, 14, 7, 14, 7, 13, 7, 14, 1],
[14, 7, 14, 7, 14, 7, 13, 7, 14, 10, 1],
[7, 14, 7, 14, 7, 13, 7, 14, 10, 4, 1],
[14, 7, 14, 7, 13, 7, 14, 10, 4, 13, 1],
[7, 14, 7, 13, 7, 14, 10, 4, 13, 13, 1],
[14, 7, 13, 7, 14, 10, 4, 13, 13, 7, 1],
[7, 13, 7, 14, 10, 4, 13, 13, 7, 13, 1],
[13, 7, 14, 10, 4, 13, 13, 7, 13, 3, 1],
[7, 14, 10, 4, 13, 13, 7, 13, 3, 13, 1],
[14, 10, 4, 13, 13, 7, 13, 3, 13, 13, 1],
[10, 4, 13, 13, 7, 13, 3, 13, 13, 3, 1],
[4, 13, 13, 7, 13, 3, 13, 13, 3, 13, 0],
[13, 13, 7, 13, 3, 13, 13, 3, 13, 13, 0],
[13, 7, 13, 3, 13, 13, 3, 13, 13, 14, 0]]


The last number in each tuple is the label. I am trying to find a way to create a dataset that could separate the data and the target in order to build a model. I couldn't find anything similar in the documentation. Will it be easier to turn it back to Dataframe?

Thank you!

Answer

Do you mean separate the features from the labels? If so, you can use numpy.

from sklearn import svm
import numpy as np
data = np.asarray(A)
X = data[:,:-1]
y = data[:,-1]
clf = svm.SVC()
clf.fit(X, y)

A is the original data list.

Comments