Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Error When Using sklearn Predict Function firebird Programmer named Tim Posts: 16 Threads: 10 Joined: Jun 2019 Reputation: 0 Likes received: 0 #1 Mar-21-2020, 04:34 PM (This post was last modified: Mar-21-2020, 04:35 PM by firebird. Edited 1 time in total.) Hello everyone! Needing your help to figure out on how to resolve the error message i get after running code below (credit: https://github.com/SravB/Algorithmic-Trading): ```#Algorithmic Trading with Machine Learning #imports from time import * from sklearn import tree import datetime as dt from pandas.plotting import register_matplotlib_converters register_matplotlib_converters() import matplotlib.pyplot as plt from matplotlib import style import pandas as pd import pandas_datareader.data as web import time start_time = time.time() import warnings warnings.filterwarnings("ignore",category=DeprecationWarning) #trading algorithm def algo(t): features = [] labels = [] for i in range(len(t) - acc + 1): features.append(t[-1*acc:-1]) #1 means price went up if t[-1] > t[-2]: labels.append(1) else: labels.append(0) clf = tree.DecisionTreeClassifier() clf.fit(features, labels) #if clf.predict(t[-1*acc+1:]) == 1: if clf.predict(t[-1*acc+1:]) == 1: return 1 else: return 0 #fields acc = 10 Points = [] dates = [] CashRecords = [] Cash = 100 Bought = False days = 0 decision = 0 stockSymbol = 'AAPL' style.use('ggplot') start = dt.datetime(2015,1,1) end = dt.datetime(2016,12,31) #importing data df = web.DataReader(stockSymbol,'yahoo',start,end) df.to_csv('data.csv') df = pd.read_csv('data.csv', parse_dates = True) for i in df[['Close']]: count = 0 for j in df[i]: Points.append(round(j,2)) for i in df[['Date']]: count = 0 for j in df[i]: dates.append(dt.datetime.strptime(j, "%Y-%m-%d")) #graph labels plt.figure(num = stockSymbol) plt.title(stockSymbol + " Stock Algorithmic Trading Analysis") plt.xlabel('Date') plt.ylabel('Stock Price / Cash') while days <= len(df[['Close']]) - 1: #stock info days += 1 StockPrice = Points[days - 1] if days == 1: initP = StockPrice initC = Cash #your money if Bought == True: Cash = round(Cash*StockPrice/Points[days-2],2) c = "green" else: c = "red" CashRecords.append(Cash) if days > acc: decision = algo(Points[:days]) if Bought == True: if decision == 0: Bought = False else: if decision == 1: Bought = True plt.plot(dates[days - 2:days], Points[days - 2:days], color=c) print("Ending Cash: " + str(CashRecords[-1])) print("Expected Cash: " + str(round(CashRecords * Points[-1] / Points,2))) print("Performance: " + str(round(100 * CashRecords[-1] * Points / (Points[-1] * CashRecords),2)) + "%") plt.plot(dates, CashRecords, color='blue') plt.show() ```The error message: ``````Error: Traceback (most recent call last): File "C:/Users/.../Desktop/algoscore_lab.py", line 100, in decision = algo(Points[:days]) File "C:/Users/.../Desktop/algoscore_lab.py", line 38, in algo if clf.predict(t[-1*acc+1:]) == 1: File "C:\Users\...\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\tree\_classes.py", line 419, in predict X = self._validate_X_predict(X, check_input) File "C:\Users\...\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\tree\_classes.py", line 380, in _validate_X_predict X = check_array(X, dtype=DTYPE, accept_sparse="csr") File "C:\Users\...\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\utils\validation.py", line 552, in check_array raise ValueError( ValueError: Expected 2D array, got 1D array instead: array=[106.26 107.75 111.89 112.01 109.25 110.22 109.8 106.82 105.99]. 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. `````` « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Keras.Predict into Dataframe Finpyth 12 275 Mar-15-2020, 06:22 PM Last Post: Finpyth Outputing LogisticRegression Coefficients (sklearn) RawlinsCross 6 333 Feb-27-2020, 02:47 PM Last Post: RawlinsCross Predict Longitude and Latitude Using Python vibeandvisualize 1 227 Dec-27-2019, 12:10 PM Last Post: Larz60+ How to predict with date as input for DecisionTreeRegressor sandeep_ganga 0 169 Dec-12-2019, 03:29 AM Last Post: sandeep_ganga Predicting an output variable with sklearn Ccross1 1 479 Jun-04-2019, 03:11 PM Last Post: michalmonday sklearn regression to excel punksnotdead 1 541 Apr-14-2019, 12:32 PM Last Post: punksnotdead Can someone explain how does svr_rbf.predict(dates) work? j2ee 0 1,347 Feb-22-2018, 06:50 PM Last Post: j2ee Model.predict() always returning the same value of 1 for opencv nastyheatnor 0 2,415 Dec-14-2017, 08:20 AM Last Post: nastyheatnor how to predict next value taking in account several variables? drogontargaryen 2 1,589 Aug-23-2017, 02:16 AM Last Post: ichabod801 sklearn imported but not recognized kerberg 6 8,572 Jun-18-2017, 12:32 PM Last Post: snippsat

Forum Jump:

Users browsing this thread: 1 Guest(s)