# --------------------------------
# ----- Variables del modelo -----
# --------------------------------
ACCION = 'AAPL'
FECHA_INICIO = '2019-01-01'
FECHA_FINAL = '2019-01-30'
# Importaciones
import numpy as np
from sklearn.svm import SVR
import matplotlib.pyplot as plt
import pandas as pd
from pandas_datareader.data import DataReader
%matplotlib inline
# Cargamos el dataset
dataframe = DataReader(
ACCION,
data_source='yahoo',
start=FECHA_INICIO,
end=FECHA_FINAL
)
dataframe["Date"] = dataframe.index
dataframe.head()
Highfloat64
Lowfloat64
2019-01-02T00:00:00.000000
39.71250153
38.55749893
2019-01-03T00:00:00.000000
36.43000031
35.5
2019-01-04T00:00:00.000000
37.13750076
35.95000076
2019-01-07T00:00:00.000000
37.20750046
36.47499847
2019-01-08T00:00:00.000000
37.95500183
37.13000107
# Formateamos los datos
df = pd.DataFrame({
"open": dataframe['Open'],
"volume": dataframe['Volume'],
"high": dataframe['High'],
"low": dataframe['Low'],
"close": dataframe['Close'],
"date": dataframe['Date'].astype(str),
"adjclose": dataframe['Adj Close'],
})
df.head()
openfloat64
volumeint64
2019-01-02T00:00:00.000000
38.72249985
148158800
2019-01-03T00:00:00.000000
35.99499893
365248800
2019-01-04T00:00:00.000000
36.13249969
234428400
2019-01-07T00:00:00.000000
37.17499924
219111200
2019-01-08T00:00:00.000000
37.38999939
164101200
def get_data(df):
data = df.copy()
data['date'] = data['date'].str.split('-').str[2]
data['date'] = pd.to_numeric(data['date'])
return [ data['date'].tolist(), data['close'].tolist() ] # Convertimos la serie a una lista
dates, prices = get_data(df)
print(dates, prices)
[2, 3, 4, 7, 8, 9, 10, 11, 14, 15, 16, 17, 18, 22, 23, 24, 25, 28, 29, 30] [39.47999954223633, 35.54750061035156, 37.064998626708984, 36.98249816894531, 37.6875, 38.32749938964844, 38.45000076293945, 38.0724983215332, 37.5, 38.26750183105469, 38.73500061035156, 38.96500015258789, 39.20500183105469, 38.32500076293945, 38.47999954223633, 38.17499923706055, 39.439998626708984, 39.07500076293945, 38.66999816894531, 41.3125]
def predict_prices(dates, prices, x):
dates = np.reshape(dates,(len(dates), 1)) # Convertimos a 1 dimensión
x = np.reshape(x,(len(x), 1))
svr_lin = SVR(kernel='linear', C=1e3)
svr_poly = SVR(kernel='poly', C=1e3, degree=2)
svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
# Llenamos con los datos los modelos de regresión
svr_lin .fit(dates, prices)
svr_poly.fit(dates, prices)
svr_rbf.fit(dates, prices)
plt.scatter(dates, prices, c='k', label='Data')
plt.plot(dates, svr_lin.predict(dates), c='g', label='Linear model')
plt.plot(dates, svr_rbf.predict(dates), c='r', label='RBF model')
plt.plot(dates, svr_poly.predict(dates), c='b', label='Polynomial model')
plt.xlabel('Date')
plt.ylabel('Price')
plt.title('Support Vector Regression')
plt.legend()
plt.show()
return svr_rbf.predict(x)[0], svr_lin.predict(x)[0], svr_poly.predict(x)[0]
predicted_price = predict_prices(dates, prices, [31])
predicted_price
# -------------------------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------------------------