gpt4 book ai didi

python - Scikit 的支持向量回归 - 从他们的网站模拟代码时出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 02:05:27 24 4
gpt4 key购买 nike

如标题所示,我正在尝试从 Scikit Learns 网站(使用我自己的数据)模拟几个回归函数,但在绘制结果时遇到了一些问题。

证券代码:https://scikit-learn.org/stable/auto_examples/svm/plot_svm_regression.html

我的问题:我的图形绘制了多条回归线(见下图)。

In:  #select features
feature_cols = ['avg_r']

#x def
X= df_s[feature_cols]
X= df_s[['avg_r']]
print("X type", type(X))
print("X shape", X.shape)

#y def
y = df_s['sales']
y = df_s.sales
print("y type", type(y))
print("y shape",y.shape)
Out: X type <class 'pandas.core.frame.DataFrame'>
X shape (1105, 1)
y type <class 'pandas.core.series.Series'>
y shape (1105,)
In: X.head()
Out:     avg_r
0 470.500000
1 717.750000
2 603.416667
3 566.416667
4 778.333333
In: y.head()
Out: 0    2412
1 1308
2 2037
3 2052
4 1553
Name: sales, dtype: int64
In: #split data into training and test subsets
X_train, X_test, y_train, y_test = train_test_split(X,y)

print("X_train", X_train.shape)
print("X_test", X_test.shape)
print("y_train", y_train.shape)
print("y_train", y_test.shape)
Out: X_train (828, 1)
X_test (277, 1)
y_train (828,)
y_train (277,)
In: #fit regression models

svr_rbf = SVR(kernel='rbf', C=1e4, gamma=0.0025)
svr_lin = SVR(kernel='linear', C=1e3)
#svr_poly = SVR(kernel='poly', C=1e3, degree=2)

y_rbf = svr_rbf.fit(X_train, y_train).predict(X_test)
y_lin = svr_lin.fit(X_train, y_train).predict(X_test)
#y_poly = svr_poly.fit(X_train, y_train).predict(X_test)
Out: blank
In: # Look at the results
lw = 2
plt.scatter(X_train, y_train, color='darkorange', label='sales')
plt.plot(X_test, y_rbf, color='navy', lw=lw, label='RBF model')
#plt.plot(X_test, y_lin, color='c', lw=lw, label='Linear model')
#plt.plot(X_test.avg_r, y_poly, color='cornflowerblue', lw=lw,
label='Polynomial model')
plt.xlabel('rank')
plt.ylabel('sales')
plt.title('Support Vector Regression')
plt.legend()
plt.show()
Out: 

enter image description here

如果我改为将回归绘制为散点图,我会得到:

In: # Look at the results
lw = 2
plt.scatter(X_train, y_train, color='darkorange', label='sales')
plt.scatter(X_test, y_rbf, color='navy', lw=lw, label='RBF model')
#plt.plot(X_test, y_lin, color='c', lw=lw, label='Linear model')
#plt.plot(X_test.avg_r, y_poly, color='cornflowerblue', lw=lw,
label='Polynomial model')
plt.xlabel('rank')
plt.ylabel('sales')
plt.title('Support Vector Regression')
plt.legend()
plt.show()
Out:

enter image description here

最佳答案

您的代码不会产生“多重回归线”。

plt.plot(X,y) 方法按照您传递它们的顺序在数据的每个点之间绘制线条。这是一个说明正在发生的事情的示例:

X = np.array([1,2,4,3])
y = np.array([0,0.5,2,1.5])

plt.plot(X,y)

你会得到

unsorted plot

但是如果你这样做

X1 = X[np.argsort(X)]
y1 = y[np.argsort(X)]

plt.plot(X1,y1)

这次你会得到

sorted plot

所以您需要做的就是对您的代码进行相同的更正:

X_test, y_rbf = X_test[np.argsort(X_test)], y_rbf[np.argsort(X_test)]

关于python - Scikit 的支持向量回归 - 从他们的网站模拟代码时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54856330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com