i am getting errors while creating SVM model

Viewed 23

The following is the code for SVM Classifier. But I am getting the following error while creating the SVM Model, I am passing x_train and y_train as parameters to the fit method the data type of both x_train and y_train is a dataframe object. I tried all the suggestion from the output but none of those suggestion worked. I have this problem with KNN, Random Forest classifier models but not with Linear Regression or Decision Tree models. What is it I am doing wrong and suggestion followed by explanation will not only help me with solving the issue but also understand the problem better.

```
def svmClassifier(featuresDF, targetDF, trainPercent, testPercent):
    from sklearn.svm import LinearSVC

    `
    print(type(y_train))
    print(y_train.columns)
    # Create a Random Forest Classifier Model
    svmModel = svc = LinearSVC(dual=False)

   `
   `
   #svmModel.fit(x_train, y_train)
   #svmModel.fit(x_train,y_train.values.ravel())
   svmModel.fit(x_train,np.ravel(y_train,order='C'))
   `
   `
   #svmModel.fit(x_train, y_train)
   #svmModel.fit(x_train,y_train.values.ravel())
   svmModel.fit(x_train,np.ravel(y_train,order='C'))
   `
   `   
   # Perform cross validation of the model
   crossValidation(svmModel, featuresDF, targetDF, 5, "kf") 
   ` 
   `
   # Prediction
   y_pred = svmModel.predict(x_test)
   `
   `
   #confusion_matrix(y_test, y_pred)

   #accuracy_score(y_test,y_pred)
   `
"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()."
I did try everything suggested in the message but the warning seem to persist. This happens with KNN, Random Forest Classifier and SVM models but it seems to work fine for Linear Regression and Decision Tree Classifier Models.  Here is the code for SVM classifier. Any suggestions and code changes along with explanation for my understanding is much appreciated. The targetDF is DataFrame instance 

**def svmClassifier(featuresDF, targetDF, trainPercent, testPercent): from sklearn.svm import LinearSVC # Split the data into training and testing data x_train,x_test,y_train, y_test = splitData(featuresDF, targetDF, trainPercent, testPercent, 2) print(type(y_train)) print(y_train.columns) # Create a Random Forest Classifier Model svmModel = svc = LinearSVC(dual=False) #svmModel.fit(x_train, y_train) #svmModel.fit(x_train,y_train.values.ravel()) svmModel.fit(x_train,np.ravel(y_train,order='C')) # Perform cross validation of the model crossValidation(svmModel, featuresDF, targetDF, 5, "kf") # Prediction y_pred = svmModel.predict(x_test) #confusion_matrix(y_test, y_pred) #accuracy_score(y_test,y_pred)**
------------------------------------------------------------------------

***Output***
<class 'pandas.core.frame.DataFrame'>
Index(['diagnosis'], dtype='object')
[0.40350877 0.43859649 0.35087719 0.92982456 0.23893805]
C:\Users\ramno\anaconda3\lib\site-packages\sklearn\utils\validation.py:1111: 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().
  y = column_or_1d(y, warn=True)
C:\Users\ramno\anaconda3\lib\site-packages\sklearn\utils\validation.py:1111: 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().
  y = column_or_1d(y, warn=True)
C:\Users\ramno\anaconda3\lib\site-packages\sklearn\utils\validation.py:1111: 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().
  y = column_or_1d(y, warn=True)
C:\Users\ramno\anaconda3\lib\site-packages\sklearn\utils\validation.py:1111: 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().
  y = column_or_1d(y, warn=True)
C:\Users\ramno\anaconda3\lib\site-packages\sklearn\utils\validation.py:1111: 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().
  y = column_or_1d(y, warn=True)
1 Answers

Try this to convert y into 1D array:

y = y.to_numpy().reshape(-1)
Related