-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinealModel.py
More file actions
54 lines (40 loc) · 2.07 KB
/
Copy pathLinealModel.py
File metadata and controls
54 lines (40 loc) · 2.07 KB
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
import pandas as pd
import numpy as np
from sklearn import linear_model
from sklearn.metrics import mean_squared_error, r2_score
excelData = pd.read_excel('data/Ibex35Data.xlsx', sheet_name=None)
[companie.drop(['Unnamed: 0'], axis='columns', inplace=True) for companie in excelData.values()]
data = dict(excelData)
def ModelCompanie(companieName):
print("-----------------------------------------------")
print("Companie: ", companieName)
data[companieName] = data[companieName].apply(lambda x: x.str.replace(',','.'))
data[companieName]['Volumen(€)'] = data[companieName]['Volumen(€)'].apply(lambda x: x.replace('.',''))
X = data[companieName][['Var.(€)', 'Var.(%)', 'Máx', 'Mín', 'Volumen(€)']]
Y = data[companieName][['Cierre']]
X_train = np.array(X[1:int(0.7*len(X))])
Y_train = np.array(Y[1:int(0.7*len(Y))])
X_test = np.array(X[int(0.7*len(X)):])
Y_test = np.array(Y[int(0.7*len(Y)):])
regr=linear_model.LinearRegression()
regr.fit(X_train,Y_train)
y_pred = regr.predict(X_train)
t1=regr.coef_
print('Pendiente: \n', t1)
t0=regr.intercept_
print('Término independiente: \n', t0)
print('El modelo de regresión es: y = %f + %f * X1 + %f * X2 + %f * X3 + %f * X4 '%(t0,t1[0][0],t1[0][1],t1[0][2], t1[0][3]))
print("ECM : %.2f" % mean_squared_error(Y_train, y_pred))
# Puntaje de Varianza. El mejor puntaje es un 1.0
print('Coeficiente Correlacción: %.2f' % r2_score(Y_train, y_pred))
y_pred_test = regr.predict(X_test)
# Error Cuadrático Medio (Mean Square Error)
print("ECM : %.2f" % mean_squared_error(Y_test, y_pred_test))
# Puntaje de Varianza. El mejor puntaje es un 1.0
print('Coeficiente Correlacción: %.2f' % r2_score(Y_test, y_pred_test))
if companieName == 'ACCIONA':
y_pred2 = regr.predict([[-1.30,-1.39,94.70,92.45,59273]])
print('La predicción de cierre es: ',y_pred2)
print("-----------------------------------------------")
#[ModelCompanie(companie) for companie in data.keys()]
ModelCompanie('ACCIONA')