import numpy as np
import sklearn.metrics as metrics
import sklearn.metrics as sm
from sklearn.metrics import mean_squared_error
from sklearn.metrics import accuracy_score, confusion_matrix
import pandas as pd
import plotly.graph_objs as go
from pandas_datareader import data, wb
from datetime import date
import cufflinks as cf
import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
import math
import numpy as np import sklearn.metrics as metrics import sklearn.metrics as sm from sklearn.metrics import mean_squared_error from sklearn.metrics import accuracy_score, confusion_matrix import pandas as pd import plotly.graph_objs as go from pandas_datareader import data, wb from datetime import date import cufflinks as cf import yfinance as yf import pandas as pd import numpy as np import matplotlib.pyplot as plt import tensorflow as tf from sklearn.preprocessing import MinMaxScaler from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM from keras.layers import Dropout import math
In[3]:
start=pd.to_datetime('2013-01-01') end=pd.to_datetime('2018-01-01')
In[4]:
TSLA = data.DataReader('TSLA', 'yahoo', start, end)
In[5]:
TSLA.info()
In[6]:
TSLA_processed=TSLA.iloc[:, 3:4].values
In[7]:
TSLA.head()
In[8]:
TSLA_processed
In[9]:
from sklearn.preprocessing import MinMaxScaler scaler = MinMaxScaler(feature_range = (0, 1)) TSLA_training_scaled = scaler.fit_transform(TSLA_processed)
In[10]:
x_train = [] y_train = [] for i in range(60, 1259): x_train.append(TSLA_training_scaled[i-60:i, 0]) y_train.append(TSLA_training_scaled[i, 0]) x_train, y_train = np.array(x_train), np.array(y_train)
In[11]:
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
In[12]:
model = Sequential() model.add(LSTM(units=200, return_sequences=True, activation='relu',input_shape=(x_train.shape[1], 1))) model.add(Dropout(0.2))
model.add(LSTM(units=200, return_sequences=True)) model.add(Dropout(0.2))
model.add(LSTM(units=200, return_sequences=True)) model.add(Dropout(0.2))
model.add(LSTM(units=200, return_sequences=False)) model.add(Dropout(0.2))
In[13]:
model.add(Dense(units = 1)) model.compile(optimizer = 'adam', loss = 'mean_squared_error')
In[14]:
model.fit(x_train, y_train, epochs = 100, batch_size = 32)
In[15]:
start=pd.to_datetime('2018-01-01') end=pd.to_datetime('2018-01-8') TSLA2 = data.DataReader('TSLA', 'yahoo', start, end)
In[16]:
TSLA2_processed=TSLA2.iloc[:, 3:4].values
In[17]:
len(TSLA2)
In[18]:
TSLA_total = pd.concat((TSLA['Close'], TSLA2['Close']), axis=0)
In[19]:
test_inputs = TSLA_total[len(TSLA_total) - len(TSLA2) - 60:].values
In[20]:
test_inputs = test_inputs.reshape(-1,1) test_inputs = scaler.transform(test_inputs)
In[21]:
test_features = [] for i in range(60, 65): test_features.append(test_inputs[i-60:i, 0])
In[22]:
test_features = np.array(test_features) test_features = np.reshape(test_features, (test_features.shape[0], test_features.shape[1], 1))
In[23]:
predictions = model.predict(test_features)
In[24]:
predictions = scaler.inverse_transform(predictions)
PREDICCION DE 7 DIAS
In[25]:
plt.figure(figsize=(10,6)) plt.plot(TSLA2_processed, color='blue', label='Actual Tesla Stock Price') plt.plot(predictions , color='red', label='Predicted Tesla Stock Price') plt.title('Tesla Stock Price Prediction') plt.xlabel('Date') plt.ylabel('Tesla Stock Price') plt.legend() plt.show()
In[26]:
print("Mean absolute error =", round(sm.mean_absolute_error(TSLA2_processed, predictions), 2)) print("Mean squared error =", round(sm.mean_squared_error(TSLA2_processed, predictions), 2)) print("Median absolute error =", round(sm.median_absolute_error(TSLA2_processed, predictions), 2)) print("Explain variance score =", round(sm.explained_variance_score(TSLA2_processed, predictions), 2)) print("R2 score =", round(sm.r2_score(TSLA2_processed, predictions), 2))
In[27]:
start=pd.to_datetime('2018-01-01') end=pd.to_datetime('2018-02-01') TSLA3 = data.DataReader('TSLA', 'yahoo', start, end)
In[28]:
TSLA3_processed=TSLA3.iloc[:, 3:4].values
In[29]:
len(TSLA3)
In[30]:
TSLA_total = pd.concat((TSLA['Close'], TSLA3['Close']), axis=0)
In[31]:
test_inputs = TSLA_total[len(TSLA_total) - len(TSLA3) - 60:].values
In[32]:
test_inputs
In[33]:
test_inputs = test_inputs.reshape(-1,1) test_inputs = scaler.transform(test_inputs)
In[34]:
test_features = [] for i in range(60, 82): test_features.append(test_inputs[i-60:i, 0])
In[35]:
test_features = np.array(test_features) test_features = np.reshape(test_features, (test_features.shape[0], test_features.shape[1], 1))
In[36]:
predictions = model.predict(test_features)
In[37]:
predictions = scaler.inverse_transform(predictions)
In[38]:
plt.figure(figsize=(10,6)) plt.plot(TSLA3_processed, color='blue', label='Actual Tesla Stock Price') plt.plot(predictions , color='red', label='Predicted Tesla Stock Price') plt.title('Tesla Stock Price Prediction') plt.xlabel('Date') plt.ylabel('Tesla Stock Price') plt.legend() plt.show()
In[39]:
print("Mean absolute error =", round(sm.mean_absolute_error(TSLA3_processed, predictions), 2)) print("Mean squared error =", round(sm.mean_squared_error(TSLA3_processed, predictions), 2)) print("Median absolute error =", round(sm.median_absolute_error(TSLA3_processed, predictions), 2)) print("Explain variance score =", round(sm.explained_variance_score(TSLA3_processed, predictions), 2)) print("R2 score =", round(sm.r2_score(TSLA3_processed, predictions), 2))
In[ ]:
In[ ]: