[英]The function is not working properly in python
我试图预测石油的未来价格,但在此之前,我编写了一个简单的函数,使用matplotlib可视化工具查看日期与价格的比较。 但是,代码中有问题,我找不到应该通过的内容。 这是代码:
dates=[]
prices=[]
def getdata(filename):
with open(filename,'r') as csvfile:
csvFilereader=csv.reader(csvfile)
next(csvFilereader)
for row in csvFilereader:
dates.append(int(row[4].split('-')[0]))
prices.append(float(row[2]))
return
def predicted_price(dates, prices, x):
dates=np.reshape(dates,len(dates),1)
svr_linear= 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_linear.fit(dates,prices)
svr_ploy.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.poly.predict(dates), color='blue', label='Poly Model')
plt.plot(dates, svr.linear.predict(dates), color='green', label='Linera Model')
plt.xlabel('Dates')
plt.ylabel('Prices')
plt.title('Regression')
plt.legend()
plt.show()
return svr_rbf.predict(x[4]), svr_linear(x[4]), svr_poly(x[4])
getdata('D:\\android\\trans1.csv')
predicted_prices=predicted_price([dates,prices,10])
print(predicted_prices)
这是错误:
TypeError: Traceback (most recent call last)
<ipython-input-20-935270aaab8d> in <module>()
38 getdata('D:\\android\\trans1.csv')
39
---> 40 predicted_prices=predicted_price([dates,10.2,10])
41 print(predicted_prices)
TypeError: predicted_price() missing 2 required positional arguments: 'prices' and 'x'
这是数据快照: 在此处输入图像描述
更改
predicted_prices=predicted_price([dates,10.2,10])
至
predicted_prices=predicted_price(dates,10.2,10)
因为, predicted_price
期望得到三个论点,而您只给出了一个list
, [dates,10.2,10]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.