Hello QC Support,

I am working on a Machine Learning Strategy for the Daily SPY ETF. Code below & attached. I am getting the following error:

Runtime Error: ValueError : Shape of passed values is (2, 1), indices imply (2, 2)
at construction_error
"Shape of passed values is {0} in managers.py:line 1717 
ValueError : Shape of passed values is (2, 1), indices imply (2, 2) (Open Stacktrace)

I seem to be passing an incorrectly shaped array to the Neural Network predict function. I'm using a rolling window for the daily Close prices and a Momentum indicator with the same lookback period. I hope I am initializing & passing these correctly to the alpha model. Does the Close rolling window & MOM indicator have a Date / Time index that I need to drop? As you can see I have tried all sorts of data manipulations to try to get this work. Can you have a look pls and see if you can help me out. 

Thanks / Sheikh



import numpy as np
import pandas as pd
from sklearn.neural_network import MLPClassifier

class MachineLearningSPY(QCAlgorithm):
    
    def Initialize(self):
        
        self.SetStartDate(2016, 5, 2)  
        self.SetEndDate(2021, 6, 22)  
        self.SetCash(100000)  
        self.AddEquity("SPY", Resolution.Daily)  
        self.SetBenchmark("SPY")
        self.SetBrokerageModel(BrokerageName.AlphaStreams)
        self.SetExecution(ImmediateExecutionModel())
        self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
        
        self.AddUniverseSelection(ManualUniverseSelectionModel("SPY"))
        
        self.lookback = 30
        self.spyClose = RollingWindow[float](self.lookback)
        self.spyMomentum = self.MOMP("SPY", self.lookback, Resolution.Daily)
        self.SetWarmup(self.lookback)
        self.AddAlpha(MachineLearningSPYAlphaModel(self.spyClose, self.spyMomentum))
        
class MachineLearningSPYAlphaModel:
    def __init__(self, close, spyMomentum):
        self.period = timedelta(30)
        self.spyClose = close
        self.spyMomentum = spyMomentum
    
    def GetMLModel(self):
        self.MLModel = 0
        self.MLModel = MLPClassifier(hidden_layer_sizes = (100, 100, 100, 100), max_iter = 1000)
        
    def Update(self, algorithm, data):
        insights = []
        
        if data.Bars.ContainsKey("SPY"):
            self.spyMomentum.Update(data["SPY"].EndTime, data["SPY"].Close)
            self.spyClose.Add(data["SPY"].Close)
            
            if self.spyMomentum.IsReady and self.spyClose.IsReady:
                
                # features array
                arr_features = np.array([self.spyMomentum, self.spyClose])
                
                # features dataframe
                self.df = pd.DataFrame(arr_features, columns=["MOM", "Close"]) # Does MOM and Close series have a date/time index?
                
                # calculate daily SPY forward returns to be used to set Target / Signal
                self.df['spyReturn'] = np.log(self.df['Close'].shift(-1)/self.df['Close']) 
                self.df = self.df.dropna()
                
                # set Signal / Target
                self.df['Signal'] = 0
                self.df.loc[self.df['spyReturn'] > 0, 'Signal'] = 1
                self.df.loc[self.df['spyReturn'] < 0, 'Signal'] = -1
                
                # set training data
                self.X = self.df.drop(['Close','Signal'], axis=1)
                self.Y = self.df['Signal']
        
                # align signals and features
                self.Y, self.X = self.Y.align(self.X, axis=0, join='inner')
                self.X_train = self.X[:-1]
                self.Y_train = self.Y[:-1]
                
                # fit / train ML model
                self.GetMLModel()
                self.MLModel.fit(self.X_train, self.Y_train)
                
                # predict next day signal using today's values of feature set
                self.X_today = self.X.iloc[-1]
                # self.X_today is Series, so convert to numpy array
                self.X_today = self.X_today.to_numpy()
                # reshape self.X_today because it only has 1 day's sample
                self.X_today = self.X_today.reshape(1,-1)
                
                # Y_predict will take predicted signal
                self.Y_predict = self.Y.iloc[-1]
                self.Y_predict = self.MLModel.predict(self.X_today)
                
                if self.Y_predict == 1:
                    insights.append(Insight("SPY", self.period, InsightType.Price, InsightDirection.Up, 1, None))
                elif self.Y_predict == -1:
                    insights.append(Insight("SPY", self.period, InsightType.Price, InsightDirection.Down, 1, None))
                    
        return insights
                
    def OnSecuritiesChanged(self, algorithm, changes):
        self.changes = changes