0

I am trying to predict the future prices of petroleum but before that I wrote a simple function to see the dates vs price comparison using matplotlib visualization. However there is something wrong in the code and I am not able to find what should I pass. Here is the code:

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) 

Here is the error:

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'

Here is the data snapshot: enter image description here

3
  • 1
    predicted_price(*[dates,prices,10]), you need to expand the variables provided in the list, by adding * Commented Nov 22, 2018 at 10:03
  • 1
    You wrapped the parameters in a list, but they have to be single arguments: predicted_prices = predicted_price(dates, 10.2, 10) Commented Nov 22, 2018 at 10:03
  • BugHunter I tried your approach but then this error comes:Expected 2D array, got 1D array instead: array=[19. 19. 19. ... 22. 20. 23.]. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample. Commented Nov 22, 2018 at 10:16

1 Answer 1

2

Change

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

to

predicted_prices=predicted_price(dates,10.2,10)

because, predicted_price is expecting three arguemnts, and you giving only one which is list, [dates,10.2,10].

Sign up to request clarification or add additional context in comments.

3 Comments

I tried that but then it gives error: Expected 2D array, got 1D array instead.
Maybe your function call should be predicted_price(dates, prices, 10) and according to x[4] in the function the parameter value 10 seems to be incorrext too.
if I pass only the parameter as predicted_price(dates, prices, 10), then the 1D error occours. I am not able to find what the probelm is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.