2021年3月5日星期五

How to run cross validation in a SVM model?

I tried to train a LinearSVC model and evaluate it with cross_val_score on a linearly separable dataset that I created. But I'm getting the following error message.

from sklearn.model_selection import cross_val_score  from sklearn.model_selection import train_test_split  from sklearn.pipeline import Pipeline  from sklearn.preprocessing import StandardScaler  from sklearn.svm import LinearSVC    import matplotlib.pyplot as plt  import numpy as np  import pandas as pd      # creating the dataset  x1          = 2 * np.random.rand(100, 1)  y1          = 5 + 3 * x1 + np.random.randn(100, 1)  lable1  = np.zeros((100, 1))    x2          = 2 * np.random.rand(100, 1)  y2          = 15 + 3 * x2 + np.random.randn(100, 1)  lable2  = np.ones((100, 1))    x       = np.concatenate((x1, x2))  y       = np.concatenate((y1, y2))  lable = np.concatenate((lable1, lable2))    x       = np.reshape(x, (len(x),))  y       = np.reshape(y, (len(y),))  lable = np.reshape(lable, (len(lable),))    d   = {'x':x, 'y':y, 'lable':lable}  df  = pd.DataFrame(data=d)  df.plot(kind="scatter", x="x", y="y")        # preparing data and model  train_set, test_set = train_test_split(df, test_size=0.2, random_state=42)  X = train_set.drop("lable", axis=1)  y = train_set["lable"].copy()    scaler = StandardScaler()  scaler.fit_transform(X)    linear_svc = LinearSVC(C=5, loss="hinge", random_state=42)  linear_svc.fit(X, y)        # evaluation  scores = cross_val_score(linear_svc, X, y, scoring="neg_mean_squared_error", cv=10)  rmse_scores = np.sqrt(-scores)  print("Mean:", rmse_scores.mean())  

enter image description here

Output:

Mean: 0.0

/usr/local/lib/python3.7/dist-packages/sklearn/svm/_base.py:947: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations. "the number of iterations.", ConvergenceWarning)

https://stackoverflow.com/questions/66501258/how-to-run-cross-validation-in-a-svm-model March 06, 2021 at 08:36AM

没有评论:

发表评论