如何让我的支持向量回归工作以绘制我的多项式图

我已经编译了用于绘制多项式图的代码,但它没有进行绘图。我使用了来自scikit-learn的SVR(支持向量回归),我的代码如下。它没有显示任何错误消息,只是显示了我的数据。我不知道发生了什么。有人知道吗?甚至在变量控制台上也没有显示任何关于我的数据的描述。

import pandas as pdimport numpy as npfrom sklearn.svm import SVRfrom sklearn import cross_validationfrom sklearn.linear_model import LinearRegressionimport matplotlib.pyplot as pltdf = pd.read_csv('coffee.csv')print(df)df = df[['Date','Amount_prod','Beverage_index']]x = np.array(df.Amount_prod)y = np.array(df.Beverage_index)x_train, x_test, y_train, y_test = cross_validation.train_test_split(x, y, test_size=0.2)x_train = np.pad(x, [(0,0)], mode='constant')x_train.reshape((26,1))y_train = np.pad(y, [(0,0)], mode='constant')y_train.reshape((26,1))x_train = np.arange(26).reshape((26, 1))x_train = x.reshape((26, 1))c = x.Tnp.all(x_train == c)x_test = np.arange(6).reshape((-1,1))x_test = x.reshape((-1,1))c2 = x.Tnp.all(x_test == c2)y_test = np.arange(6).reshape((-1,1))y_test = y.reshape((-1,1))c2 = y.Tnp.all(y_test ==c2)svr_poly = SVR(kernel='poly', C=1e3, degree=2)y_poly = svr_poly.fit(x_train,y_train).predict(x_train)plt.scatter(x_train, y_train, color='black')plt.plot(x_train,  y_poly)plt.show()

数据样本:

 Date   Amount_prod Beverage_index    1990    83000         78    1991    102000        78    1992    94567         86    1993    101340        88    1994    96909         123    1995    92987         101    1996    103489        99    1997    99650         109    1998    107849        110    1999    123467        90    2000    112586        67    2001    113485        67    2002    108765        90

回答:

尝试下面的代码。支持向量机期望其输入具有零均值和单位方差。阻塞的不是绘图,而是对fit的调用。

from sklearn.pipeline import make_pipelinefrom sklearn.preprocessing import StandardScalersvr_poly = make_pipeline(StandardScaler(), SVR(kernel='poly', C=1e3, degree=2))y_poly = svr_poly.fit(x_train,y_train).predict(x_train)

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

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