Make a stock market prediction model using below code as referance the model must be…
- User
Make a stock market prediction model using below code as referance the model must be relable use NIFTY dataset from yfinance:
from sklearn.preprocessing import scale import numpy as np from abc import abstractmethod
low = -0.01 high = 0.01
def to_category(label): label = np.where(label < low, -1, label) label = np.where(label > high, 1, label) label = np.where(abs(label) != 1, 0, label) label += 1 return label
def standardise(data): data = np.nan_to_num(data) return data
def print_summary(label): mean = np.mean(label) std = np.std(label) print('MARKET SUMMARY:\n', 'mean=\t', mean, '\tstd=\t', std) print('min=\t', np.min(label), '\tmax=\t', np.max(label)) print('left-outlier=\t', np.sum(label < mean - 3 * std), '\tright-outlier=\t', np.sum(label > mean + 3 * std))
class StrategyBase(object): def init(self, train_data, train_label, fit_data, target_filter, train_filter, month, split=None, kernel=None, method='category', pre_acc=1): self.pre_acc = pre_acc self.month = month self.model = None self.predict = None self.method = method self.kernel = kernel self.target_filter = target_filter self.train_filter = train_filter self.num_classes = 3 self.fit_data = standardise(fit_data[target_filter]) self.train_data = standardise(train_data[train_filter]) self.train_label = np.ravel(train_label)[train_filter] # ----------------------------SUMMARY----------------------- print_summary(self.train_label) print('training size(samples\tdata_dim\ttime_steps):') # ----------------------into MULTI-LABEL-------------------- if self.method == 'category': self.train_label = to_category(self.train_label) self.loss = 'categorical_crossentropy' else: self.loss = 'mse' self.train_data = self.train_data.swapaxes(1, 2) self.fit_data = self.fit_data.swapaxes(1, 2) self.samples, self.time_steps, self.data_dim = self.train_data.shape print(self.train_data.shape) print(self.fit_data.shape) if split is not None: self.data_dim1 = split + 1 self.data_dim2 = self.data_dim - self.data_dim1 self.split_data()
@abstractmethod def get_model(self, class_weight=None): '''model construct''' @abstractmethod def fit_model(self, class_weight=None): '''model fit''' def get_predict(self): self.predict = self.model.predict(self.fit_data) def fit_predict(self, class_weight=None): if self.month > 1: self.load_models() # NEED OVERLOAD else: self.get_model(class_weight) # NEED IMPLEMENT self.fit_model(class_weight) # NEED IMPLEMENT self.get_predict() # NEED OVERLOAD self.predict_transform() self.save_models() # NEED OVERLOAD def predict_transform(self): if self.method == 'category': if self.predict.ndim > 1: # pre_sum = np.sum(self.predict, axis=1) # self.predict = np.matmul(self.predict, np.array([-1, 0, 1]).T) / pre_sum self.predict = np.argmax(self.predict, axis=1) - 1 # self.predict = to_target(self.predict) else: self.predict -= 1 self.predict = np.ravel(self.predict) predict = np.full(len(self.target_filter), fill_value=-1, dtype=float) predict[self.target_filter] = self.predict self.predict = predict def save_models(self): pass def load_models(self): pass def split_data(self): self.train_data = [ self.train_data[:, :, :self.data_dim1], scale( self.train_data[:, 0, -self.data_dim2:].reshape( [self.train_data.shape[0], -1] ) ) ] self.fit_data = [ self.fit_data[:, :, :self.data_dim1], scale( self.fit_data[:, 0, -self.data_dim2:].reshape( [self.fit_data.shape[0], -1] ) ) ] - Assistant
First, let's install yfinance and get the historical NIFTY dataset:
!pip install yfinance import yfinance as yf # Get NIFTY historical data from Yahoo Finance symbol = "^NSEI" nifty = yf.Ticker(symbol) nifty_data = nifty.history(period="max")Then, preprocess the NIFTY dataset (OHLC tables) to be in a suitable format for our model:
import pandas as pd # We can use only the closing prices for our model nifty_close_prices = nifty_data["Close"] # We will calculate daily returns percentage nifty_returns = nifty_close_prices.pct_change().dropna() # We will use a window of 50 trading days for our model window = 50 # Create training data and labels train_data = [] train_label = [] for i in range(len(nifty_returns) - window): train_data.append(nifty_returns.iloc[i:i+window].values) train_label.append(nifty_returns.iloc[i+window]) train_data = np.array(train_data) train_label = np.array(train_label) # Determine the filters for training and target data num_train = int(0.8 * len(train_data)) # 80% training data train_filter = np.ones(len(train_data), dtype=bool) train_filter[num_train:] = False target_filter = ~train_filterFinally, create a class that derives from
StrategyBaseand override theget_model,fit_model,save_models, andload_modelsmethods as required for your specific model. In the example below, I will use a simple LSTM model using Keras:from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense, Dropout from tensorflow.keras.optimizers import Adam from tensorflow.keras.callbacks import ModelCheckpoint class LstmStrategy(StrategyBase): def get_model(self, class_weight=None): self.model = Sequential() self.model.add(LSTM(50, input_shape=(self.time_steps, self.data_dim), return_sequences=True)) self.model.add(Dropout(0.2)) self.model.add(LSTM(50, return_sequences=False)) self.model.add(Dropout(0.2)) self.model.add(Dense(self.num_classes, activation="softmax")) optimizer = Adam(lr=0.001) self.model.compile(optimizer=optimizer, loss=self.loss, metrics=["accuracy"]) def fit_model(self, class_weight=None): checkpointer = ModelCheckpoint(filepath="best_weights.hdf5", verbose=0, save_best_only=True) self.model.fit( self.train_data, pd.get_dummies(self.train_label).values, epochs=100, batch_size=64, verbose=1, validation_split=0.1, callbacks=[checkpointer], class_weight=class_weight) def save_models(self): self.model.save("final_model.hdf5") def load_models(self): if self.month > 1: self.model.load_weights("final_model.hdf5") else: self.model.load_weights("best_weights.hdf5")Now, you can create an instance of
LstmStrategyand callfit_predictto train the model and make your predictions:strategy = LstmStrategy(train_data, train_label, train_data, target_filter, train_filter, month=1) strategy.fit_predict() predictions = strategy.predictPlease note that the provided code and this example model were kept relatively simple for demonstration purposes. You may need to optimize and adjust the model according to your requirements. The final model performance must be evaluated on out-of-sample data to understand the reliability of the prediction model.
- User
NameError Traceback (most recent call last) in <cell line: 1>() ----> 1 class LstmStrategy(StrategyBase): 2 def get_model(self, class_weight=None): 3 self.model = Sequential() 4 self.model.add(LSTM(50, input_shape=(self.time_steps, self.data_dim), return_sequences=True)) 5 self.model.add(Dropout(0.2))
NameError: name 'StrategyBase' is not defined
- Assistant
Apologies for the confusion. It seems that the code cells have not been properly marked in my previous response. Please make sure you execute the cells in the correct order and pay attention to the naming.
- First, install yfinance and import required libraries:
!pip install yfinance import yfinance as yf import numpy as np import pandas as pd from sklearn.preprocessing import scale from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense, Dropout from tensorflow.keras.optimizers import Adam from tensorflow.keras.callbacks import ModelCheckpoint- Next, load and preprocess the NIFTY dataset as explained before:
# Get NIFTY historical data from Yahoo Finance symbol = "^NSEI" nifty = yf.Ticker(symbol) nifty_data = nifty.history(period="max") # We can use only the closing prices for our model nifty_close_prices = nifty_data["Close"] # We will calculate daily returns percentage nifty_returns = nifty_close_prices.pct_change().dropna() # We will use a window of 50 trading days for our model window = 50 # Create training data and labels train_data = [] train_label = [] for i in range(len(nifty_returns) - window): train_data.append(nifty_returns.iloc[i:i+window].values) train_label.append(nifty_returns.iloc[i+window]) train_data = np.array(train_data) train_label = np.array(train_label) # Determine the filters for training and target data num_train = int(0.8 * len(train_data)) # 80% training data train_filter = np.ones(len(train_data), dtype=bool) train_filter[num_train:] = False target_filter = ~train_filter-
Execute the code cell containing the
StrategyBaseclass provided in the original response. -
Finally, define and use the
LstmStrategyclass:
class LstmStrategy(StrategyBase): def get_model(self, class_weight=None): self.model = Sequential() self.model.add(LSTM(50, input_shape=(self.time_steps, self.data_dim), return_sequences=True)) self.model.add(Dropout(0.2)) self.model.add(LSTM(50, return_sequences=False)) self.model.add(Dropout(0.2)) self.model.add(Dense(self.num_classes, activation="softmax")) optimizer = Adam(lr=0.001) self.model.compile(optimizer=optimizer, loss=self.loss, metrics=["accuracy"]) def fit_model(self, class_weight=None): checkpointer = ModelCheckpoint(filepath="best_weights.hdf5", verbose=0, save_best_only=True) self.model.fit( self.train_data, pd.get_dummies(self.train_label).values, epochs=100, batch_size=64, verbose=1, validation_split=0.1, callbacks=[checkpointer], class_weight=class_weight) def save_models(self): self.model.save("final_model.hdf5") def load_models(self): if self.month > 1: self.model.load_weights("final_model.hdf5") else: self.model.load_weights("best_weights.hdf5") strategy = LstmStrategy(train_data, train_label, train_data, target_filter, train_filter, month=1) strategy.fit_predict() predictions = strategy.predictPlease make sure to execute the cells in the correct order, and you should not encounter the
NameErrorissue.