I have compiled my code for a polynomial graph, but it is not plotting. I am using SVR(support vector regression) from scikit learn and my code is below. It is not showing any error message, and it is just showing my data. I don't know what is going on. Does anyone? It is not even showing anything on the variable console describing my data.
我已经编译了一个多项式图的代码,但它不是绘图。我正在使用scikit学习的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()
Data sample:
数据样本:
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
2 个解决方案
#1
2
Try the code below. Support Vector Machines expect their input to have zero mean and unit variance. It's not the plot, that's blocking. It's the call to 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)
#2
0
Just to build on Matt's answer a little. Nothing about your plotting is in error. When you call to svr_poly.fit with 'unreasonably' large numbers no error is thrown (but I still had to kill my kernel). By tinkering the exponent value in this code I reckoned that you could get up to 1e5 before it breaks, but not more. Hence your problem. As Matt says, applying the StandardScaler will solve your problems.
我们来看看马特的回答。你的绘图没有任何错误。当你调用svr_poly。与“不合理”的大数字相匹配,没有错误(但我仍然要杀死我的内核)。通过修改这段代码中的指数值,我认为在它崩溃之前,您可以达到1e5,但不是更多。因此你的问题。就像Matt说的,应用标准定标器会解决你的问题。
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()
#1
2
Try the code below. Support Vector Machines expect their input to have zero mean and unit variance. It's not the plot, that's blocking. It's the call to 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)
#2
0
Just to build on Matt's answer a little. Nothing about your plotting is in error. When you call to svr_poly.fit with 'unreasonably' large numbers no error is thrown (but I still had to kill my kernel). By tinkering the exponent value in this code I reckoned that you could get up to 1e5 before it breaks, but not more. Hence your problem. As Matt says, applying the StandardScaler will solve your problems.
我们来看看马特的回答。你的绘图没有任何错误。当你调用svr_poly。与“不合理”的大数字相匹配,没有错误(但我仍然要杀死我的内核)。通过修改这段代码中的指数值,我认为在它崩溃之前,您可以达到1e5,但不是更多。因此你的问题。就像Matt说的,应用标准定标器会解决你的问题。
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()