In this code no matter how much low learning rate i do my accuracy saturates at 0.4340 I…
- User
In this code no matter how much low learning rate i do my accuracy saturates at 0.4340 I tried lr=0.001 and 0.00001: Epoch 1/200 79/79 [==============================] - 13s 130ms/step - loss: 1.0979 - accuracy: 0.3753 Epoch 2/200 79/79 [==============================] - 11s 135ms/step - loss: 1.0956 - accuracy: 0.4281 Epoch 3/200 79/79 [==============================] - 9s 113ms/step - loss: 1.0930 - accuracy: 0.4332 Epoch 4/200 79/79 [==============================] - 10s 131ms/step - loss: 1.0906 - accuracy: 0.4340 Epoch 5/200 79/79 [==============================] - 10s 132ms/step - loss: 1.0880 - accuracy: 0.4340 Epoch 6/200 79/79 [==============================] - 10s 129ms/step - loss: 1.0853 - accuracy: 0.4340
Code: import yfinance as yf import pandas as pd import numpy as np
nifty_df = yf.download('^NSEI', start='2009-01-01', end='2021-12-31')
Clean the data
nifty_df = nifty_df.dropna()
Calculate various percent differences
nifty_df['PctOpenClose'] = (nifty_df['Close'] - nifty_df['Open']) / nifty_df['Open'] * 100 nifty_df['PctHighLow'] = (nifty_df['High'] - nifty_df['Low']) / nifty_df['Low'] * 100 nifty_df['PctChange'] = nifty_df['Close'].pct_change() * 100
Remove the first row with NaN in 'PctChange'
nifty_df = nifty_df.iloc[1:]
Export cleaned dataset
nifty_df.to_csv('nifty_cleaned.csv')
def create_labels(df, col_name, threshold=0.5): labels = [] for index, row in df.iterrows(): future_price = row[col_name] if future_price >= threshold: labels.append(2) # Buy elif future_price <= -threshold: labels.append(0) # Sell else: labels.append(1) # Hold
return labelsnifty_df['Labels'] = create_labels(nifty_df, 'PctChange')
import matplotlib.pyplot as plt import seaborn as sns
sns.countplot(nifty_df['Labels'])
plt.title('Count of Buy, Hold, and Sell') plt.xlabel('Trade Decisions') plt.ylabel('Count') plt.show()
from sklearn.ensemble import ExtraTreesClassifier
X = nifty_df.drop(['Labels'], axis=1) y = nifty_df['Labels']
Fit the ExtraTreesClassifier
et_clf = ExtraTreesClassifier() et_clf.fit(X, y)
Visualize feature importances
feature_importances = pd.Series(et_clf.feature_importances_, index=X.columns) feature_importances.nlargest(10).plot(kind='barh') plt.show()
important_features = feature_importances.nlargest(8).index.tolist() X_filtered = X[important_features] X_filtered
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler() scaled_X = scaler.fit_transform(X_filtered)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(scaled_X, y, test_size=0.2, random_state=42)
- Assistant
The accuracy saturation at 0.4340 may result from several factors. Possible adjustments to improve the model's performance include:
-
Data Preprocessing and Feature Selection: In your code, you use the top 8 important features. You might try selecting different features or change the threshold when creating labels to see if it affects the model's performance.
-
Model Architecture: You can try adjusting the architecture of your LSTM model, including the number of hidden layers, the number of units in each layer, and the activation functions used.
-
Regularization: You can add regularization techniques like L1 or L2 to prevent overfitting.
-
More Data: You might consider using a more extensive dataset for training the model. A larger training dataset can improve the model's generalization capacity.
-
Optimizer and Loss Function: Try experimenting with different optimizers and loss functions to fine-tune the model's performance.
-