Python Forum
Error When Using sklearn Predict Function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error When Using sklearn Predict Function
#1
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:])[0] == 1:

    if clf.predict(t[-1*acc+1:])[0] == 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[0] * Points[-1] / Points[0],2)))
print("Performance: " + str(round(100 * CashRecords[-1] * Points[0] / (Points[-1] * CashRecords[0]),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 <module> decision = algo(Points[:days]) File "C:/Users/.../Desktop/algoscore_lab.py", line 38, in algo if clf.predict(t[-1*acc+1:])[0] == 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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Column Transformer with Mixed Types - sklearn aaldb 0 243 Feb-22-2024, 03:27 PM
Last Post: aaldb
  How to use .predict() method Jack90_ 4 698 Nov-03-2023, 08:21 PM
Last Post: Larz60+
  How to predict Total Hours needed with List as Input? caninan 0 761 Jan-27-2023, 04:20 PM
Last Post: caninan
  Which network architecture should be chosen to predict disease severity? AlekseyPython 2 1,956 Aug-21-2021, 07:12 PM
Last Post: jefsummers
  How to predict output in a regression problem? Rezaafz 1 2,469 Apr-03-2021, 04:16 PM
Last Post: jefsummers
  sklearn.neural_network MLPClassifier forecast variances CK1960 1 1,779 Oct-29-2020, 10:13 AM
Last Post: CK1960
  Keras.Predict into Dataframe Finpyth 13 9,564 Aug-31-2020, 07:22 AM
Last Post: hussainmujtaba
  Analyze, predict the next step in a sequence. Antonio0608 0 1,971 Jul-23-2020, 05:53 PM
Last Post: Antonio0608
  Customizing an sklearn submodule with cython JHogg11 0 1,930 May-27-2020, 05:39 PM
Last Post: JHogg11
  sklearn and train_test_split nsadams87xx 1 1,793 Apr-23-2020, 05:32 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020