我正在尝试绘制csv的日期和价格。但是dates.append()
函数抛出一个错误。我该怎么解决这个问题?
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('-')))
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.fir(dates,prices)
svr_ploy(dates,prices)
svr_rbf(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_linerar(x[4]), svr_poly(x[4])
getdata('D:\\android\\trans1.csv')
predicted_prices=predicted_price(dates,price,30)
print(predicted_prices)
这是错误消息。
TypeError Traceback (most recent call last)
<ipython-input-4-63df0e521768> in <module>()
35 return svr_rbf.predict(x[4]), svr_linerar(x[4]), svr_poly(x[4])
36
---> 37 getdata('D:\\android\\trans1.csv')
38
39 predicted_prices=predicted_price(dates,price,30)
<ipython-input-4-63df0e521768> in getdata(filename)
7 next(csvFilereader)
8 for row in csvFilereader:
----> 9 dates.append(int(row[4].split('-')))
10 prices.append(float(row[2]))
11 return
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list
答案 0 :(得分:2)
一旦split
一个字符串,您将返回一个列表,错误发生在这里:
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('-'))) <------------ here
prices.append(float(row[2]))
因此,不是将字符串项转换为int
,而是将列表的.split()
的结果转换为int
,这是不可能的。
如果要从列表中选择目标结果,可以尝试从结果中选择特定索引以对其进行测试:
dates.append(int(row[4].split('-')[0]))