使用SVM模型进行交叉验证时出现ConvergenceWarning

我尝试训练一个LinearSVC模型,并用我创建的线性可分数据集通过cross_val_score进行评估,但出现了错误。

以下是一个可复现的例子:

from sklearn.model_selection import cross_val_score, train_test_splitfrom sklearn.preprocessing import StandardScalerfrom sklearn.svm import LinearSVCimport matplotlib.pyplot as pltimport numpy as npimport pandas as pd# creating the datasetx1          = 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 modeltrain_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)# evaluationscores = 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

输出结果:

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)


回答:

这不是一个错误,而是一个警告,并且它已经包含了一些建议:

增加迭代次数

默认的迭代次数是1000(文档)。

此外,LinearSVC是一个分类器,因此在cross_val_score中使用scoring="neg_mean_squared_error"(即回归指标)是没有意义的;请查看文档以了解每种问题类型相关指标的大致列表。

因此,通过以下更改:

linear_svc = LinearSVC(C=5, loss="hinge", random_state=42, max_iter=100000)scores = cross_val_score(linear_svc, X, y, scoring="accuracy", cv=10)

你的代码可以正常运行,没有任何错误或警告。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注