<jupyter_start><jupyter_text>Challenge - Stock Prediction ![](https://miro.medium.com/max/9216/1*NG0bzk0wtQcBdMYAnXKeBQ.jpeg) Background Information  Get data<jupyter_code>import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

from datetime import datetime

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
data = pd.read_csv('TSLA.csv')
data.shape
data.isna().sum()
data.describe()
#data['Date'] = pd.to_datetime(data['Date'])<jupyter_output><empty_output><jupyter_text>Preprocessing<jupyter_code>plt.plot(data['Close'])
plt.show()
data['Close']

from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range=(0, 1))
data['Close'] = scaler.fit_transform(data['Close'].values.reshape(-1,1))

data['Close']<jupyter_output><empty_output><jupyter_text>Splitting Data<jupyter_code>train,test = data[pd.to_datetime(data['Date'])<datetime(2016,1,1)]['Close'],data[pd.to_datetime(data['Date'])>=datetime(2016,1,1)]['Close']
len(train)
len(test)
train<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>from tensorflow.keras.layers import LSTM
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation, Dropout
import time

def create_dataset(dataset, look_back=1, forward_days=1):
    dataX, dataY = [], []
    for i in range(len(dataset)-look_back-1-forward_days):
        a = dataset.iloc[i:(i+look_back)]
        dataX.append(a)
        dataY.append(dataset.iloc[i + look_back:i + look_back + forward_days])
    return np.array(dataX), np.array(dataY)

look_back = 40
forward_days = 10
trainX, trainY = create_dataset(train, look_back, forward_days)
testX, testY = create_dataset(test, look_back, forward_days)

# The LSTM network expects the input data to be provided with a specific array structure in the form of: [samples, time steps, features].
trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
trainX
model = Sequential()

model.add(LSTM(128,
    return_sequences=True,
    activation='tanh'))
model.add(Dropout(0.2))

model.add(LSTM(
    64,
    return_sequences=False))
model.add(Dropout(0.2))

model.add(Dense(forward_days))
model.add(Activation('linear'))

start = time.time()
model.compile(loss='mse', optimizer='adam')
print ('compilation time : ', time.time() - start)<jupyter_output>compilation time :  0.015812158584594727<jupyter_text>Training and Predicting<jupyter_code>history = model.fit(
    trainX,
    trainY,
    batch_size=128,
    epochs=10,
    validation_split=0.05)
results = model.predict(testX[:4])
results
testY[:4]<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup