Search code examples
pythonpandasdataframesvmsklearn-pandas

Fit SVM on a pandas data frame in Python


I have a pandas data frame named dataset and I exported the first two columns of it as X and the last column which is named "Class" as y:

X = dataset.drop('Class', axis=1)
y = dataset['Class']

Then using the following lines of code, I tried to fit SVM using these data:

from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20)

After that, using the two lines below, I tried to train the SVM but I got the following error.

SupportVectorClassModel = SVC()
SupportVectorClassModel.fit(X_train,y_train)

The SVM Error

How can I fix this issue?


Solution

  • try to convert your dataframe into the list. then split and fit into model

    after you split the dataframe into X and Y, convert it into list by using values function or tolist function. i.e

    X = dataset.drop('Class', axis=1)
    y = dataset['Class']
    # assuming X has multiple columns and y only one column
    X = X.values
    y = y['Class'].tolist()
    
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20)