python - 支持向量-/Logistic-回归 : do you have benchmark results for the boston housing data?

标签 python machine-learning regression scikit-learn

我打算通过在 sklearn (sklearn.datasets.load_boston) 附带的波士顿房价数据集上运行它来测试 sklearn 支持向量回归包的实现。

在试用了一段时间(尝试不同的正则化和管参数、案例随机化和交叉验证)并始终如一地预测平坦线之后,我现在对我失败的地方感到茫然。更引人注目的是,当我使用 sklearn.datasets 包 (load_diabetes) 附带的糖尿病数据集时,我得到了更好的预测。

复制代码如下:

import numpy as np
from sklearn.svm import SVR
from matplotlib import pyplot as plt
from sklearn.datasets import  load_boston
from sklearn.datasets import load_diabetes
from sklearn.linear_model import LinearRegression

# data = load_diabetes()
data = load_boston()
X = data.data
y = data.target

# prepare the training and testing data for the model
nCases = len(y)
nTrain = np.floor(nCases / 2)
trainX = X[:nTrain]
trainY = y[:nTrain]
testX  = X[nTrain:]
testY = y[nTrain:]

svr = SVR(kernel='rbf', C=1000)
log = LinearRegression()

# train both models
svr.fit(trainX, trainY)
log.fit(trainX, trainY)

# predict test labels from both models
predLog = log.predict(testX)
predSvr = svr.predict(testX)

# show it on the plot
plt.plot(testY, testY, label='true data')
plt.plot(testY, predSvr, 'co', label='SVR')
plt.plot(testY, predLog, 'mo', label='LogReg')
plt.legend()
plt.show()

现在我的问题是:你们中有没有人成功地将这个数据集与支持向量回归模型一起使用,或者知道我做错了什么?我非常感谢你的建议! p>

下面是上面脚本的结果: results of running on the load_boston dataset

最佳答案

rbf 更改内核至 linear将解决问题。如果你想使用 rbf , 尝试一些不同的参数,尤其是 gamma .默认 gamma ( 1/# features ) 对于您的情况来说太大了。

enter image description here

这是我用于线性内核的参数 SVR :

svr = SVR(kernel='linear', C=1.0, epsilon=0.2)

我绘制了训练数据标签和测试数据标签。您可能会注意到训练数据的分布不均匀。这使得模型在 5 < y < 15 时缺少训练数据。 .所以我对数据进行了一些改组,并将训练数据设置为使用 66% 的数据。

nTrain = np.floor(nCases *2.0 / 3.0)
import random
ids = range(nCases)
random.shuffle(ids)

trainX,trainY,testX,testY = [],[],[],[]
for i, idx in enumerate(ids):
    if i < nTrain:
        trainX.append(X[idx])
        trainY.append(y[idx])
    else:
        testX.append(X[idx])
        testY.append(y[idx])

这是我得到的:

enter image description here

从视觉上看,就预测误差而言,两个回归变量看起来都更好。

这是 rbf 的一个工作示例内核 SVR :

svr = SVR(kernel='rbf',  C=1.0, epsilon=0.2, gamma=.0001)

结果如下:

enter image description here

关于python - 支持向量-/Logistic-回归 : do you have benchmark results for the boston housing data?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14590879/

相关文章:

Python 将临时文件写入 S3

python - Pandas 日期时间 : Aggregate based on time lag

algorithm - 非线性 SVM 的原始权重

python - 跨文件组织的 Python 类的可能性?

python - 在 Ubuntu 中找不到(但已安装)模块

python - 在 Tensorflow 2 中的每个纪元之后计算每个类的召回率

python - Keras TimeDistributed - 权重共享吗?

python - 线性回归不对称系数 - python 中的双 beta

python - 所有 Tensorflow 输出均为 nan

r - 无法绘制 rlm 对象。非 NA 剩余长度与拟合中使用的情况不匹配