该函数在python中工作不正常

该函数在python中工作不正常,python,Python,我试图预测未来的石油价格,但在此之前,我编写了一个简单的函数,使用matplotlib可视化查看日期与价格比较。然而,代码中有一些错误,我无法找到应该通过的内容。 代码如下: dates=[] prices=[] def getdata(filename): with open(filename,'r') as csvfile: csvFilereader=csv.reader(csvfile) next(csvFilereader) f

我试图预测未来的石油价格,但在此之前,我编写了一个简单的函数,使用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'
TypeError:回溯(最近一次调用)
在()
38 getdata('D:\\android\\trans1.csv')
39
--->40预测价格=预测价格([日期,10.2,10])
41打印(预计价格)
TypeError:predicted_price()缺少2个必需的位置参数:“prices”和“x”
以下是数据快照: 改变

predicted_prices=predicted_price([dates,10.2,10])

因为,
predicted\u price
需要三个参数,而您只给出了一个
list
[日期,10.2,10]
更改

predicted_prices=predicted_price([dates,10.2,10])


因为,
predicted_price
需要三个参数,而您只给出一个是
list
[dates,10.2,10]

predicted_price(*[dates,prices,10]),所以您需要扩展列表中提供的变量,方法是添加*将参数包装在列表中,但是它们必须是单参数:
predicted_prices=predicted_prices(dates,10.2,10)
BugHunter我尝试了你的方法,但后来出现了这个错误:预期2D数组,改为1D数组:数组=[19.19.19…22.20.23]。使用数组重塑数据。如果数据具有单个特征或数组,则重塑(-1,1)。如果数据包含单个样本,则重塑(1,-1)。预测价格(*[dates,prices,10]),您需要通过添加*扩展列表中提供的变量,将参数包装在列表中,但是它们必须是单参数:
predicted_prices=predicted_prices(dates,10.2,10)
BugHunter我尝试了你的方法,但后来出现了这个错误:预期2D数组,改为1D数组:数组=[19.19.19…22.20.23]。使用数组重塑数据。如果数据具有单个特征或数组,则重塑(-1,1)。如果数据包含单个样本,则重塑(1,-1)。我尝试过,但随后出现错误:预期为2D数组,改为1D数组。也许您的函数调用应该是
预测价格(日期,价格,10)
并根据
x[4]
在函数中,参数值
10
似乎也不正确。如果我只传递参数作为预测价格(日期、价格,10),则1D错误会发生。我找不到问题所在。我尝试了一下,但出现了错误:预期的2D数组,改为1D数组。也许您的函数调用应该是
预测价格(日期,价格,10)
并根据
x[4]
在函数中,参数值
10
似乎也不正确。如果我只传递参数作为预测价格(日期、价格,10),则1D错误会发生。我找不到问题所在。