<jupyter_start><jupyter_text>reading dataset<jupyter_code>df = pd.read_csv("apple.csv")
df.head()
df.isnull().sum()<jupyter_output><empty_output><jupyter_text>preprocessing----------------------<jupyter_code>import matplotlib.pyplot as plt
plt.plot(df['close'])
from sklearn.preprocessing import MinMaxScaler
import numpy as np
scaler=MinMaxScaler(feature_range=(0,1))
data=scaler.fit_transform(np.array(df['close']).reshape(-1,1))
data.shape, data[:5], data[5:10]<jupyter_output><empty_output><jupyter_text>train test split<jupyter_code>train_data,test_data=data[0:int(len(data)*0.75)],data[int(len(data)*0.75):]
train_data.shape, test_data.shape, train_data[:5], test_data[:5]
def create_dataset(dataset, time_step=1):
    dataX, dataY = [], []
    for i in range(len(dataset)-time_step):
        dataX.append(dataset[i:i+time_step])
        dataY.append(dataset[i+time_step])
    return np.array(dataX), np.array(dataY)
X_train, Y_train = create_dataset(train_data, time_step=64)
X_test, Y_test = create_dataset(test_data, time_step=64)
X_train.shape, Y_train.shape, X_test.shape, Y_test.shape<jupyter_output><empty_output><jupyter_text>creating LSTM model--------------------------<jupyter_code>from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
model=Sequential()
model.add(LSTM(50,return_sequences=True, input_shape=(64,1)))
model.add(LSTM(50,return_sequences=True))
model.add(LSTM(50))
model.add(Dense(1))
model.compile(loss='mean_squared_error',optimizer='adam')
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm (LSTM)                  (None, 64, 50)            10400     
_________________________________________________________________
lstm_1 (LSTM)                (None, 64, 50)            20200     
_________________________________________________________________
lstm_2 (LSTM)                (None, 50)                20200     
_________________________________________________________________
dense (Dense)                (None, 1)                 51        
=================================================================
Total params: 50,851
Trainable params: 50,851
Non-trainable params: 0
_________________________________________________________________<jupyter_text>training------------------<jupyter_code>model.fit(X_train,Y_train,validation_data=(X_test,Y_test),epochs=10,batch_size=128,verbose=1)
train_predict = model.predict(X_train)
test_predict = model.predict(X_test)<jupyter_output><empty_output><jupyter_text>forecast next several days values------------><jupyter_code>forecast_dat = []
forecast_inp = np.append(X_test[-1][1:], test_predict[-1].reshape(1,1),axis=0) #last input from X_test - first element + last element of test_predict 
i=0
while (i<100):
    forecast_pred = model.predict(forecast_inp[i:].reshape(1,forecast_inp.shape[0]-i,1))
    forecast_dat.append(forecast_pred)
    forecast_inp = np.append(forecast_inp, forecast_pred,axis=0)
    i=i+1<jupyter_output><empty_output><jupyter_text>plotting all the data------previous, train predict, test predict, FORECAST OF COMING DAYS-----------------<jupyter_code># shift train predictions for plotting
look_back=64
trainPredictPlot = np.empty_like(np.append(df['close'],np.array(forecast_dat).reshape(-1,),axis=0))
trainPredictPlot[:] = np.nan
trainPredictPlot = trainPredictPlot.reshape(-1,1)
trainPredictPlot[look_back:look_back+len(train_predict)] = train_predict

# shift test predictions for plotting
testPredictPlot = np.empty_like(np.append(df['close'],np.array(forecast_dat).reshape(-1,),axis=0))
testPredictPlot[:] = np.nan
testPredictPlot = testPredictPlot.reshape(-1,1)
testPredictPlot[len(train_predict)+look_back+look_back:len(df['close'])] = test_predict

# shift new forecast for plotting
forecastPlot = np.empty_like(np.append(df['close'],np.array(forecast_dat).reshape(-1,),axis=0))
forecastPlot[:] = np.nan
forecastPlot = forecastPlot.reshape(-1,1)
forecastPlot[len(df['close']):] = np.array(forecast_dat).reshape(len(forecast_dat),1)

# plot baseline and predictions
plt.plot(np.append(df['close'].values.reshape(-1,1),scaler.inverse_transform(np.array(forecast_dat).reshape(-1,1)),axis=0))
plt.plot(scaler.inverse_transform(trainPredictPlot))
plt.plot(scaler.inverse_transform(testPredictPlot))
plt.plot(scaler.inverse_transform(forecastPlot))
plt.show()
plt.plot(scaler.inverse_transform(forecastPlot)[len(df['close']):])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17497850386930353, 'fraction_numerical': 0.024505588993981083, 'mean_word_length': 6.071428571428571, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '560274', 'license': 'MIT', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1506, 'n_words': 205, 'path': 'Stock_Forecasting (LSTM).ipynb', 'repo_name': 'ravis2114/Text-Numeric-Data-Generation', 'size': '4652'}