【问题标题】:How do i get my support vector regression to work to plot my polynomial graph我如何让我的支持向量回归来绘制我的多项式图
【发布时间】:2016-09-09 12:15:33
【问题描述】:

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

import pandas as pd
import numpy as np
from sklearn.svm import SVR
from sklearn import cross_validation
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt



df = 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.T
np.all(x_train == c)

x_test = np.arange(6).reshape((-1,1))
x_test = x.reshape((-1,1))
c2 = x.T
np.all(x_test == c2)

y_test = np.arange(6).reshape((-1,1))
y_test = y.reshape((-1,1))
c2 = y.T
np.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

【问题讨论】:

  • 我没有发现这样的代码有什么问题。虽然有一个警告:为 x 和 y 分别提供一些长度为 26 的虚拟值,然后查看绘图是否显示。如果是这样,那么您就知道您的代码运行正常,否则您需要确定您的数据是否采用正确的格式。尝试排除各种可能性,你一定能找到解决办法。
  • 您能否提供一些数据以使此示例完全可重现。否则有点难以遵循。在第一次检查时,我觉得将 y_poly 分配线分成两部分可能会更好。即。 svr = svr_poly.fit(x_t,y_t) 和 y_poly = svr.predict(x_t)
  • @draco_alpine 我在我的问题中添加了一些示例数据。谢谢!
  • 我实际上只是在练习这个,并且对通过图表更感兴趣。并不真正担心预测,因为这里显然没有因果关系。

标签: python machine-learning scikit-learn svm


【解决方案1】:

试试下面的代码。支持向量机期望它们的输入具有零均值和单位方差。这不是情节,那是阻塞。这是给fit的电话。

from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

svr_poly = make_pipeline(StandardScaler(), SVR(kernel='poly', C=1e3, degree=2))
y_poly = svr_poly.fit(x_train,y_train).predict(x_train)

【讨论】:

  • 这样做会显示此错误 DataConversionWarning: 输入 dtype int64 的数据已被 StandardScaler 转换为 float64。 warnings.warn(msg, DataConversionWarning)
  • 它正在绘制 btw 但只是抛出此错误消息,我想知道为什么。
  • Beverage_Index 可能存储为 int64。要表示标准化,您需要能够划分为 float64 类型。您基本上可以忽略警告。
  • 好的,知道了。谢谢
【解决方案2】:

只是以马特的回答为基础。您的绘图没有任何错误。当您以“不合理”的大数字调用 svr_poly.fit 时,不会引发错误(但我仍然不得不杀死我的内核)。通过修改此代码中的指数值,我认为您可以在它中断之前达到 1e5,但不会更多。因此你的问题。正如 Matt 所说,应用 StandardScaler 将解决您的问题。

import pandas as pd
import numpy as np
from sklearn.svm import SVR
import matplotlib.pyplot as plt



x_train = np.random.rand(10,1)      # between 0 and 1
y_train = np.random.rand(10,)       # between 0 and 1
x_train = np.multiply(x_train,1e5)  #scaling up to 1e5
svr_poly = SVR(kernel='poly', C=1e3, degree=1)
svr_poly.fit(x_train,y_train)#.predict(x_train)
y_poly = svr_poly.predict(x_train)

plt.scatter(x_train, y_train, color='black')
plt.plot(x_train,  y_poly)

plt.show()

【讨论】:

  • 是的,我应该对我的数据进行预处理和规范化。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 2020-02-01
  • 2014-02-20
  • 2018-11-25
  • 2019-05-11
  • 2020-01-18
  • 1970-01-01
相关资源
最近更新 更多