【发布时间】:2015-11-21 01:52:32
【问题描述】:
我试图通过一个玩具示例来感受 SVM 回归。我生成了 1 到 100 之间的随机数作为预测变量,然后获取它们的对数并添加高斯噪声来创建目标变量。将这些数据弹出到 sklearn 的 SVR 模块中会生成一个外观合理的模型:
但是,当我通过加入原始预测变量的平方来增加训练数据时,一切都变得混乱:
我知道 RBF 内核的作用类似于获取原始特征的权力,因此加入第二个特征大部分是多余的。但是,SVM 在处理功能冗余方面真的如此糟糕吗?还是我做错了什么?
这是我用来生成这些图表的代码:
from sklearn.svm import SVR
import numpy as np
import matplotlib.pyplot as plt
# change to highest_power=2 to get the bad model
def create_design_matrix(x_array, highest_power=1):
return np.array([[x**k for k in range(1, highest_power + 1)] for x in x_array])
N = 1000
x_array = np.random.uniform(1, 100, N)
y_array = np.log(x_array) + np.random.normal(0,0.2,N)
model = SVR(C=1.0, epsilon=0.1)
print model
X = create_design_matrix(x_array)
#print X
#print y_array
model = model.fit(X, y_array)
test_x = np.linspace(1.0, 100.0, num=10000)
test_y = model.predict(create_design_matrix(test_x))
plt.plot(x_array, y_array, 'ro')
plt.plot(test_x, test_y)
plt.show()
感谢您对这个谜团的任何帮助!
【问题讨论】:
-
你有没有搞过 C 语言?
-
是的,我为 C 尝试了从 0.01 到 1000 的值(并且也使用了 epsilon),但无法获得合理的曲线。
标签: python machine-learning scikit-learn regression svm