<jupyter_start><jupyter_text><jupyter_code>!pip install yfinance
import pandas as pd
import numpy as np
import math
import pandas_datareader as pdd
from sklearn.preprocessing import MinMaxScaler
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
import matplotlib.pyplot as plt
from pandas_datareader import data as pdr
import yfinance as yfin
yfin.pdr_override()

df = pdr.get_data_yahoo("AAPL", start="2013-01-01", end="2019-12-30")
print(df)
plt.figure(figsize=(16,8))
plt.title('close price movement')
plt.plot(df['Close'])
plt.xlabel('Date', fontsize=18)
plt.ylabel('Close Price in $', fontsize=18)
plt.show
data = df.filter(['Close'])
dataset = data.values
len(dataset)
training_data_size = math.ceil(len(dataset)*.7)
training_data_size
scaler = MinMaxScaler(feature_range=(0,1))
scaled_data = scaler.fit_transform(dataset)
scaled_data

train_data = scaled_data[0:training_data_size, :]
x_train = []
y_train = []
for i in range(60, len(train_data)):
    x_train.append(train_data[i-60:i, 0])
    y_train.append(train_data[i,0])
    if i <=60:
        print(x_train)
        print(y_train)
x_train, y_train = np.array(x_train), np.array(y_train)
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
x_train.shape
model = Sequential()
model.add(LSTM(50, return_sequences=True, input_shape = (x_train.shape[1], 1)))
model.add(LSTM(50, return_sequences=False))
model.add(Dense(25))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mean_squared_error')

model.fit(x_train, y_train, batch_size=1, epochs=10)
test_data = scaled_data[training_data_size - 60: ,:]
x_test = []
y_test = dataset[training_data_size:, :]
for i in range(60, len(test_data)):
  x_test.append(test_data[i-60:i, 0])

x_test = np.array(x_test)
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1],1))
prediction = model.predict(x_test)
prediction = scaler.inverse_transform(prediction)
rmse = np.sqrt(np.mean(prediction- y_test)**2)
rmse
train = data[:training_data_size]
valid = data[training_data_size:]
valid['prediction'] = prediction
plt.figure(figsize=(16,8))
plt.title('Model LM')
plt.xlabel('Date', fontsize=18)
plt.ylabel('closeprice in $', fontsize=18)
plt.plot(train['Close'])
plt.plot(valid[['Close', 'prediction']])
plt.legend(['Train', 'val', 'predictions'], loc='lower right')
plt.show
valid<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.152317880794702, 'fraction_numerical': 0.028145695364238412, 'mean_word_length': 7.056666666666667, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '668554', 'license': 'MIT', 'n_tokens_mistral': 979, 'n_tokens_neox': 915, 'n_words': 174, 'path': 'stock_LSTM.ipynb', 'repo_name': 'purohitn/SNAS-Series', 'size': '2416'}