python - 用于 SVR 回归的 Scikit Learn 包存在问题

标签 python scikit-learn regression svm libsvm

我正在尝试使用 Scikit Learn Package 拟合 SVM 回归模型,但它没有像我预期的那样工作。

您能帮我找出错误吗?我想使用的代码是:

from sklearn.svm import SVR
import numpy as np


X = []
x = np.arange(0, 20)
y = [3, 4, 8, 4, 6, 9, 8, 12, 15, 26, 35, 40, 45, 54, 49, 59, 60, 62, 63, 68]
X.append(x)

clf = SVR(verbose=1)
clf.fit(np.transpose(X), y)

print("Expecting Result:")
print(y)
print("Predicted Result:")
print(clf.predict(np.transpose(X)))

我的输出是:

[LibSVM]*
optimization finished, #iter = 10
obj = -421.488272, rho = -30.500000
nSV = 20, nBSV = 20
Expecting Result:
[3, 4, 8, 4, 6, 9, 8, 12, 15, 26, 35, 40, 45, 54, 49, 59, 60, 62, 63, 68]
Predicted Result:
[ 29.1136814   28.74580196  28.72748632  28.72736291  28.7273628
  28.7273628   28.72736302  28.72760984  28.76424112  29.5         31.5
  32.23575888  32.27239016  32.27263698  32.2726372   32.2726372
  32.27263709  32.27251368  32.25419804  31.8863186 ]

我们可以看到预测结果与训练数据差距很大。 如何改善拟合?

谢谢

大卫

最佳答案

这是一种边缘情况,RBF(scikit-learn 上 SVM 的默认值)内核不能很好地工作。

将 SVR 行更改为: clf = SVR(verbose=1, kernel='linear'),您将看到更合理的结果。

[LibSVM]预期结果: [3, 4, 8, 4, 6, 9, 8, 12, 15, 26, 35, 40, 45, 54, 49, 59, 60, 62, 63, 68] 预测结果: [ -6.9 -2.9 1.1 5.1 9.1 13.1 17.1 21.1 25.1 29.1 33.1 37.1 41.1 45.1 49.1 53.1 57.1 61.1 65.1 69.1]

我知道您只是想了解 SVM 的工作原理。看看this有关 RBF 内核如何工作的博客文章。

关于python - 用于 SVR 回归的 Scikit Learn 包存在问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36990288/

相关文章:

python - 如何将架构组合传递给 MLPClassifier?

python - 随机森林回归 - 如何分析其性能? - python ,sklearn

r - 在ggplot2中仅绘制stat_smooth的边界

python - 如何使用 Distutils 让 Python 脚本作为简单的 linux 命令运行?

python - Django中如何导入其他项目中的模型

python - Altair 条形图 - 标签放置和格式

scikit-learn - 带加权 AUC 的网格搜索

python - 线性回归 : How to find the distance between the points and the prediction line?

machine-learning - WEKA回归模型

python - (python) [Errno 11001] getaddrinfo 失败