-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearRegression.py
More file actions
77 lines (51 loc) · 1.46 KB
/
LinearRegression.py
File metadata and controls
77 lines (51 loc) · 1.46 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import numpy as np
import pandas as pd
df= pd.read_csv("https://raw.githubusercontent.com/GerhardSpross/clasificacion-prueba/refs/heads/main/day.csv")
display(df.info())
df.head(5)
col_x_features = [
'yr',
'holiday',
'atemp',
'hum',
'windspeed'
]
col_y_objetivo = [
'cnt'
]
X_df = df[col_x_features]
Y_df = df[col_y_objetivo]
x_mins = X_df.min()
x_maxs = X_df.max()
y_min = Y_df.min()
y_max = Y_df.max()
X_normalizada_df = (X_df - x_mins) / (x_maxs - x_mins)
Y_normalizada_df= (Y_df - y_min) / (y_max - y_min)
if (y_max == y_min).all():
Y_normalizada_df[:] = 0
m = len(Y_normalizada_df)
k = len(col_x_features)
X_norm = X_normalizada_df.values
Y_norm = Y_normalizada_df.values.reshape(m, 1)
X_b = np.c_[np.ones((m, 1)), X_norm]
alpha = 0.01
n_iteraciones = 2000
n_pesos = k + 1
w = np.zeros((n_pesos, 1))
for i in range(n_iteraciones):
y_pred = X_b.dot(w)
error = y_pred - Y_norm
gradiente = (1/m) * X_b.T.dot(error)
w = w - alpha * gradiente
print ('Mostrando resultados del entrenamiento:')
y_resultado_final = X_b.dot(w)
error_final = y_resultado_final - Y_norm
E_MSE = np.mean(error_final**2)
print("Error cuadratico medio obtenido:", E_MSE)
print(f"w0 (sesgo) = {w[0][0]:.8f}")
for j in range(k):
print(f"w{j+1} ({col_x_features[j]}): {w[j+1][0]:.8f}")
pesos_ident = [f"{peso:.8f}" for peso in w.flatten()]
pesos_str = " ".join(pesos_ident)
print(f"{pesos_str} {E_MSE: .8f}")
print ('Salida peso de W0, w1, w2, w3, w4, w5:', E_MSE)