我正试图熟悉python和机器学习,但我坚持以下几点:代码按预期工作,但无法在图形中显示。我正在使用Windows并从cmd运行。
为了更改后端,我尝试将其添加到我的代码中,但不起作用:
import matplotlib as mpl
mpl.use('Agg')
import numpy as numpy
from sklearn.svm import SVR
import matplotlib.pyplot as plt
#plt.switch_backend('new_backend')
dates = []
prices = []
def get_data(filename):
with open(filename, 'r') as csvfile:
csvFileReader = csv.reader(csvfile)
next(csvFileReader)
for row in csvFileReader:
dates.append(int(row[0].split('-')[0]))
prices.append(float(row[1]))
return
def predict_prices(dates, prices, x):
dates = np.reshape(dates,(len(dates), 1))
svr_len = SVR(kernel= 'linear', C=1e3)
svr_poly = SVR(kernel= 'poly', C=1e3, degree = 2)
svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
svr_lin.fit(dates, prices)
svr_poly.fit(dates, prices)
svr_rbf.fit(dates, prices)
plt.scatter(dates, prices, color='black', label='Data')
plt.plot(dates, svr_rbf.predict(dates),color='red', label='RBF model')
plt.plot(dates, svr_lin.predict(dates), color='green', label='Linear model')
plt.plot(dates, svr_poly.predict(dates), color='blue', label='Polynomial model')
plt.xlabel('Date')
plt.ylabel('Price')
plt.title('Support Vector Regression')
plt.legend()
plt.show()
return svr_rbf.predict(x)[0], svr_lin.predict(x)[0], svr_poly.predict(x)[0]
get_data('UBER.csv')
predicted_price = (dates, prices, 29)
print(predicted_price)
我现在希望获得图形结果,而只能在CMD行上获得期望的结果,有人可以建议我所缺少的内容吗?