<jupyter_start><jupyter_text>Stock Prediction by Keras- tf.keras 를 이용- pandas package 사용- RNN, LSTM cell 사용- S&P 500 DATA 사용<jupyter_code>import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
import urllib<jupyter_output>/Users/chjeong/.pyenv/versions/3.6.3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6
  return f(*args, **kwds)<jupyter_text>Download S&P 500 Stock data and read<jupyter_code># urllib.request.urlretrieve('https://github.com/CNuge/kaggle-code/raw/master/stock_data/all_stocks_5yr.csv'+'/?accessType=download', './data/all_stocks_5yr.csv')
data = pd.read_csv('./data/all_stocks_5yr.csv')<jupyter_output><empty_output><jupyter_text>Set data<jupyter_code>cl = data[data['Name']=='MMM']
cl = cl[['close', 'low', 'high']].values
def set_data(cl):
    scl = MinMaxScaler()
    cl = scl.fit_transform(cl)
    return cl, scl
cl, scl = set_data(cl)
def processData(data,lb):
    X,Y = [],[]
    for i in range(len(data)-lb-1):
        X.append(data[i:(i+lb)])
        Y.append(data[(i+lb),0])
    return np.array(X),np.array(Y)
X, y = processData(cl,7)
print(X.shape)
print(y.shape)
X_train,X_test = X[:int(X.shape[0]*0.80)],X[int(X.shape[0]*0.80):]
y_train,y_test = y[:int(y.shape[0]*0.80)],y[int(y.shape[0]*0.80):]
print(X_train.shape[0])
print(X_test.shape[0])
print(y_train.shape[0])
print(y_test.shape[0])<jupyter_output>1000
251
1000
251<jupyter_text>Build the model<jupyter_code>model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(256,input_shape=(7,3)))
model.add(tf.keras.layers.Dense(1))
model.compile(optimizer='adam',loss='mse')
#Reshape data for (Sample,Timestep,Features) 
X_train = X_train.reshape((X_train.shape[0],X_train.shape[1],3))
X_test = X_test.reshape((X_test.shape[0],X_test.shape[1],3))
#Fit model with history to check for overfitting
history = model.fit(X_train,y_train,epochs=10,validation_data=(X_test,y_test),shuffle=False)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
X_test[0]
y_test.reshape(-1,1).shape
Xt = model.predict(X_test)
plt.plot(y_test.reshape(-1,1))
plt.plot(Xt)
act = []
pred = []
#for i in range(250):
i=249
Xt = model.predict(X_test[i].reshape(1,7,3))
print('predicted:{0}, actual:{1}'.format(Xt,y_test[i].reshape(-1,1)))
pred.append(Xt)
act.append(y_test[i].reshape(-1,1))
result_df = pd.DataFrame({'pred':list(np.reshape(pred, (-1))),'act':list(np.reshape(act, (-1)))})
Xt = model.predict(X_test)
plt.plot(y_test.reshape(-1,1))
plt.plot(Xt)
X_test[249]
X_test[248]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1855822550831793, 'fraction_numerical': 0.03770794824399261, 'mean_word_length': 6.7094017094017095, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '617765', 'license': 'Apache-2.0', 'n_tokens_mistral': 1193, 'n_tokens_neox': 1145, 'n_words': 165, 'path': 'tfmodule/ipynb_files/Stock_Predict_keras_3.ipynb', 'repo_name': 'jwkanggist/tf-keras-stock-pred', 'size': '2705'}