<jupyter_start><jupyter_text>Min-max scaling for normalization<jupyter_code># This cell works perfectly fine, we are simply going to experiment with unnormalizing the data
# in the next cell

# np.max(training_set[:,0])
# print(type(training_set))
# type(training_set[0,0])
## Normalize between -1.0 and +1.0
# training_set_scaled = np.empty(training_set.shape)
# test_set_scaled = np.empty(test_set.shape)
# for i in range(5):
#    training_set_scaled[:,i] = training_set[:,i]/abs(training_set[:,i]).max()
#    test_set_scaled[:,i] = test_set[:,i]/abs(test_set[:,i]).max()
# print(training_set[0:5,:])
# Feature Scaling
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range = (0, 1))
training_set_scaled = sc.fit_transform(training_set)
test_set_scaled = sc.fit_transform(test_set)
print(training_set_scaled[0:5, :])
print(training_set_scaled.shape)
import matplotlib.pyplot as plt

fig, axes = plt.subplots(1,2)

axes[0].scatter(training_set[:,0], training_set[:,1])
axes[0].set_title("Original data")

axes[1].scatter(training_set_scaled[:,0], training_set_scaled[:,1])
axes[1].set_title("MinMax scaled data")

plt.show()
# Feature Scaling
# sc = MinMaxScaler(feature_range = (0, 1))
# training_set_scaled = sc.fit_transform(training_set)

# Creating a data structure with 60 time-steps and 1 output
X_train = []
y_train = []
for i in range(60, 800):
    X_train.append(training_set_scaled[i-60:i, 0])
    y_train.append(training_set_scaled[i, 0])
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))
print(len(X_train))
print(len(y_train))
print(X_train.shape)
print(y_train.shape)
#(740, 60, 1)
StopWatch.start("Training time")
# try changing the number of epochs and timestep instead of 60, try 70 or 20 (in previous cell)
model = Sequential()

#Adding the first LSTM layer and some Dropout regularisation
model.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], 1)))
model.add(Dropout(0.2))

# Adding a second LSTM layer and some Dropout regularisation
model.add(LSTM(units = 50, return_sequences = True))
model.add(Dropout(0.2))

# Adding a third LSTM layer and some Dropout regularisation
model.add(LSTM(units = 50, return_sequences = True))
model.add(Dropout(0.2))

# Adding a fourth LSTM layer and some Dropout regularisation
model.add(LSTM(units = 50))
model.add(Dropout(0.2))

# Adding the output layer
model.add(Dense(units = 1))

# Compiling the RNN
model.compile(optimizer = 'adam', loss = 'mean_squared_error')

# Fitting the RNN to the Training set
model.fit(X_train, y_train, epochs = 100, batch_size = 32)

StopWatch.stop("Training time")
StopWatch.benchmark()
# Getting the predicted stock price of 2017
# dataset_train = df1.iloc[:1120, [1,2,3,5,6]]
# dataset_test = df1.iloc[1121:, [1,2,3,5,6]]

# dataset_total = pd.concat((dataset_train, dataset_test), axis = 0)
dataset_total = np.concatenate((training_set_scaled, test_set_scaled), axis=0)

inputs = dataset_total[0:len(dataset_total) - len(test_set_scaled) - 60,:]
len(inputs)
print(test_set_scaled.shape)
print(training_set_scaled.shape)
# inputs = inputs.reshape(-1,1)
# inputs = sc.transform(inputs)
X_test = []
y_test = []
for i in range(60, 343):
    X_test.append(test_set_scaled[i-60:i, 0])
    y_test.append(test_set_scaled[i, 0])
X_test, y_test = np.array(X_test), np.array(y_test)

# X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
# X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))

print(X_test.shape)
print(type(test_set_scaled))
# (459, 60, 1)
print(X_test.shape)
predicted_stock_price = model.predict(X_test)
predicted_stock_price = sc.inverse_transform(predicted_stock_price)
# resolve = np.append(X_test,predicted_stock_price,1)
print(resolve.shape)
print(X_test.shape)
print(predicted_stock_price.shape)
print(predicted_stock_price[1:5,:])
# Visualising the results
# plt.plot(df.loc[1170:,"Date"],y_test, color = 'red',
# label = 'Real EOS Price')
plt.plot(range(0,len(y_test)),y_test, color = 'red',
label = 'Real EOS Price')
# plt.plot(df.loc[1009:, "Date"],predicted_stock_price, color = 'blue',
# label = 'Predicted EOS Stock Price')
plt.plot(range(0,len(predicted_stock_price)),predicted_stock_price, color = 'blue',
label = 'Predicted EOS Stock Price')
plt.xticks(np.arange(0,459,50))
plt.title('EOS Price Prediction')
plt.xlabel('Time')
plt.ylabel('EOS Price')
plt.legend()
plt.tight_layout()
plt.savefig("C://Users//Sledgehammer//Desktop//eos_lstm.png", dpi=100, facecolor="#FFFFFF")
# print(len(test_set_scaled))
# print(len(test_set))
# print(len(predicted_stock_price))
# print(test_set_scaled.shape)
print(X_test)
print(y_test)
print(X_train.shape)
print(y_train.shape)
print(X_train[0,:])
print(y_train[0])
plt.plot(test_set_scaled, color = 'red', label = 'Real TESLA Stock Price')
plt.plot(predicted_stock_price, color = 'blue', label = 'Predicted TESLA Stock Price')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15190885468718768, 'fraction_numerical': 0.03058165100939436, 'mean_word_length': 6.8065522620904835, 'pattern_counts': {'":': 0, '<': 5, '<?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': '143046', 'license': 'Apache-2.0', 'n_tokens_mistral': 2020, 'n_tokens_neox': 1915, 'n_words': 378, 'path': 'docs/report/su21-reu-361/project/deprecated code/lstm.ipynb', 'repo_name': 'cybertraining-dsc/cybertraining-dsc.github.io', 'size': '5003'}