-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlin_reg.py
More file actions
76 lines (55 loc) · 2.25 KB
/
lin_reg.py
File metadata and controls
76 lines (55 loc) · 2.25 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
from ml_hpx import LinearRegression
import pandas as pd
from sklearn.linear_model import LinearRegression as SklearnLinearRegression
from sklearn.metrics import mean_squared_error
from time import perf_counter
print("### Simple Linear Regression")
# Load data
df = pd.read_csv('./datasets/linear_regressor_dataset_10000.csv')
X = df[['x']]
Y = df['y']
X_values = X.values
Y_values = Y.values
# ML-HPX Linear Regression
lin_reg = LinearRegression(5000, 1e-5)
start_hpx = perf_counter()
hpx_mse = lin_reg.fit(X_values.tolist(), Y_values.tolist())
y_pred_hpx = lin_reg.predict(X_values.tolist())
end_hpx = perf_counter()
print(f"ML-HPX Linear Regression MSE: {hpx_mse:.4f}")
print(f"ML-HPX Linear Regression Time: {end_hpx - start_hpx:.4f} seconds")
# sklearn Linear Regression
sklearn_lin_reg = SklearnLinearRegression()
start_sklearn = perf_counter()
sklearn_lin_reg.fit(X_values, Y_values)
y_pred_sklearn = sklearn_lin_reg.predict(X_values)
sklearn_mse = mean_squared_error(Y_values, y_pred_sklearn)
end_sklearn = perf_counter()
print(f"Sklearn Linear Regression MSE: {sklearn_mse:.4f}")
print(f"Sklearn Linear Regression Time: {end_sklearn - start_sklearn:.4f} seconds")
print("\n### Multi-feature Linear Regression")
# Load data
df = pd.read_csv('./datasets/multi-feature-linear-regression.csv')
# Use all feature columns
X = df.drop(columns=['y'])
Y = df['y']
X_values = X.values
Y_values = Y.values
# ML-HPX Linear Regression
# Here, 5000 could be the number of iterations, and 1e-5 the learning rate (depending on implementation)
lin_reg = LinearRegression(5000, 1e-5)
start_hpx = perf_counter()
hpx_mse = lin_reg.fit(X_values.tolist(), Y_values.tolist())
y_pred_hpx = lin_reg.predict(X_values.tolist())
end_hpx = perf_counter()
print(f"ML-HPX Linear Regression MSE: {hpx_mse:.4f}")
print(f"ML-HPX Linear Regression Time: {end_hpx - start_hpx:.4f} seconds")
# sklearn Linear Regression
sklearn_lin_reg = SklearnLinearRegression()
start_sklearn = perf_counter()
sklearn_lin_reg.fit(X_values, Y_values)
y_pred_sklearn = sklearn_lin_reg.predict(X_values)
sklearn_mse = mean_squared_error(Y_values, y_pred_sklearn)
end_sklearn = perf_counter()
print(f"Sklearn Linear Regression MSE: {sklearn_mse:.4f}")
print(f"Sklearn Linear Regression Time: {end_sklearn - start_sklearn:.4f} seconds")