我正在尝试使这个脚本工作,但每当我在终端中运行它时,即使脚本仍在运行,它也不会呈现。
我使用
安装了Qt5Aggpip install Qt5Agg
我在Windows 10计算机上
我使用python 3.5
我在终端没有错误。
我已经获得了脚本所需的所有依赖项。
这是脚本:
import csv
import numpy as np
from sklearn.svm import SVR
import matplotlib.pyplot as plt
plt.switch_backend('Qt5Agg')
dates = []
prices = []
def get_data(filename):
with open(filename, 'r') as csvfile:
csvFileReader = csv.reader(csvfile)
next(csvFileReader) # skipping column names
for row in csvFileReader:
dates.append(int(row[0].split('-')[0]))
prices.append(float(row[1]))
return
def predict_price(dates, prices, x):
dates = np.reshape(dates,(len(dates), 1)) # converting to matrix of n X 1
svr_lin = SVR(kernel= 'linear', C= 1e3)
svr_poly = SVR(kernel= 'poly', C= 1e3, degree= 2)
svr_rbf = SVR(kernel= 'rbf', C= 1e3, gamma= 0.1) # defining the support vector regression models
svr_rbf.fit(dates, prices) # fitting the data points in the models
svr_lin.fit(dates, prices)
svr_poly.fit(dates, prices)
plt.scatter(dates, prices, color= 'black', label= 'Data') # plotting the initial datapoints
plt.plot(dates, svr_rbf.predict(dates), color= 'red', label= 'RBF model') # plotting the line made by the RBF kernel
plt.plot(dates,svr_lin.predict(dates), color= 'green', label= 'Linear model') # plotting the line made by linear kernel
plt.plot(dates,svr_poly.predict(dates), color= 'blue', label= 'Polynomial model') # plotting the line made by polynomial kernel
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('deutch.csv') # calling get_data method by passing the csv file to it
#print "Dates- ", dates
#print "Prices- ", prices
predicted_price = predict_price(dates, prices, 40)
print(predicted_price)
答案 0 :(得分:2)
首先,我假设您安装了PyQt5
,因为没有Qt5Agg
。
您不应该使用plt.switch_backend
,您可以快速查看此处的文档(http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.switch_backend)。
更改您的导入语句,因为在导入pyplot后无法更改后端:
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt