-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpredicción_acciones_tesla.py
89 lines (72 loc) · 2.87 KB
/
predicción_acciones_tesla.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Red Neuronal Recurrente
# Part 1 - Pre Procesamiento de Datos
# Importando Librerias
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importando Set de Entrenamiento
dataset_train = pd.read_csv('Tesla_Stock_Price_Train.csv')
training_set = dataset_train.iloc[:, 1:2].values
# Escalado de Categorias
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range = (0, 1))
training_set_scaled = sc.fit_transform(training_set)
# Creando Estructura de Datos con 60 Pasos y 1 output
X_train = []
y_train = []
for i in range(60, 1258):
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)
# Remodelacion
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
# Part 2 - Armando la Red Neuronal Recurrente
# Importando Librerias
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
# Iniciando RNR
regressor = Sequential()
# Primera capa LSTM y Regularizacion de Dropout
regressor.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], 1)))
regressor.add(Dropout(0.2))
# Segunda capa LSTM y Regularizacion de Dropout
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))
# Tercera capa LSTM y Regularizacion de Dropout
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))
# Cuarta capa LSTM y Regularizacion de Dropout
regressor.add(LSTM(units = 50))
regressor.add(Dropout(0.2))
# Capa Output / Salida
regressor.add(Dense(units = 1))
# Compilando RNR
regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')
# Encajando Red Neuronal En Set de Entrenamiento
regressor.fit(X_train, y_train, epochs = 100, batch_size = 32)
# Part 3 - Haciendo Predicciones y Visualizando Datos
# Obteniendo los Precios de la Bolsa de Valores del Stock por Analizar
dataset_test = pd.read_csv('Tesla_Stock_Price_Test.csv')
real_stock_price = dataset_test.iloc[:, 1:2].values
# Obteniendo el Precio Predicho
dataset_total = pd.concat((dataset_train['Open'], dataset_test['Open']), axis = 0)
inputs = dataset_total[len(dataset_total) - len(dataset_test) - 60:].values
inputs = inputs.reshape(-1,1)
inputs = sc.transform(inputs)
X_test = []
for i in range(60, 80):
X_test.append(inputs[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))
predicted_stock_price = regressor.predict(X_test)
predicted_stock_price = sc.inverse_transform(predicted_stock_price)
# Visualizando Resultados
plt.plot(real_stock_price, color = 'red', label = 'Real Tesla Stock Price')
plt.plot(predicted_stock_price, color = 'blue', label = 'Predicted Tesla Stock Price')
plt.title('Tesla Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('Tesla Stock Price')
plt.legend()
plt.show()