I'm trying to run a Logistic Regression via sklearn:
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
import datetime as dt
import pandas as pd
import numpy as np
import talib
import matplotlib.pyplot as plt
import seaborn as sns
col_names = ['dates','prices']
# load dataset
df = pd.read_csv("DJI2.csv", header=None, names=col_names)
df.drop('dates', axis=1, inplace=True)
print(df.shape)
df['3day MA'] = df['prices'].shift(1).rolling(window = 3).mean()
df['10day MA'] = df['prices'].shift(1).rolling(window = 10).mean()
df['30day MA'] = df['prices'].shift(1).rolling(window = 30).mean()
df['Std_dev']= df['prices'].rolling(5).std()
df['RSI'] = talib.RSI(df['prices'].values, timeperiod = 9)
df['Price_Rise'] = np.where(df['prices'].shift(-1) > df['prices'], 1, 0)
df = df.dropna()
xCols = ['3day MA', '10day MA', '30day MA', 'Std_dev', 'RSI', 'prices']
X = df[xCols]
X = X.astype('int')
Y = df['Price_Rise']
Y = Y.astype('int')
logreg = LogisticRegression()
for i in range(len(X)):
#Without this case below I get: ValueError: Found array with 0 sample(s) (shape=(0, 6)) while a minimum of 1 is required.
if(i == 0):
continue
logreg.fit(X[:i], Y[:i])
However, when i try to run this code I get the following error:
ValueError:
This solver needs samples of at least 2 classes in the data, but the data contains only one class: 58
The shape of my X data is: (27779, 6)
The shape of my Y data is: (27779,)
Here is a df.head(3)
example to see what my data looks like:
prices 3day MA 10day MA 30day MA Std_dev RSI Price_Rise
30 58.11 57.973333 57.277 55.602333 0.247123 81.932338 1
31 58.42 58.043333 57.480 55.718667 0.213542 84.279674 1
32 58.51 58.216667 57.667 55.774000 0.249139 84.919586 0
I've tried searching for where I am getting this issue from myself, but I've only managed to find these two answers, both of which discuss the issue as a bug in sklearn, however they are both approx. two years old so I do not think that I am having the same issue.