Moral88 0.1.0__py3-none-any.whl → 0.2.0__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- Moral88/regression.py +126 -12
- {Moral88-0.1.0.dist-info → Moral88-0.2.0.dist-info}/METADATA +1 -1
- Moral88-0.2.0.dist-info/RECORD +7 -0
- Moral88-0.1.0.dist-info/RECORD +0 -7
- {Moral88-0.1.0.dist-info → Moral88-0.2.0.dist-info}/LICENSE +0 -0
- {Moral88-0.1.0.dist-info → Moral88-0.2.0.dist-info}/WHEEL +0 -0
- {Moral88-0.1.0.dist-info → Moral88-0.2.0.dist-info}/top_level.txt +0 -0
Moral88/regression.py
CHANGED
@@ -1,25 +1,139 @@
|
|
1
|
-
|
2
|
-
import numpy as np
|
3
|
-
|
4
|
-
def mean_absolute_error(y_true, y_pred):
|
1
|
+
def mean_absolute_error(y_true, y_pred, normalize=True, threshold=None, method='mean', library='Moral88'):
|
5
2
|
"""
|
6
|
-
|
3
|
+
Calculate Mean Absolute Error (MAE) or variants based on method and library.
|
4
|
+
|
5
|
+
Parameters:
|
6
|
+
- y_true (list or array): True values (required)
|
7
|
+
- y_pred (list or array): Predicted values (required)
|
8
|
+
- normalize (bool): If True, normalize the result (default: True)
|
9
|
+
- threshold (tuple, optional): Apply a threshold to the predictions (default: None)
|
10
|
+
- method (str): Method of calculation. Options: {'mean', 'sum', 'none'}. Default: 'mean'
|
11
|
+
- library (str): Library to use for calculations. Options: {'Moral88', 'sklearn', 'torch', 'tensor', 'statsmodel', 'Dask-ML', 'MLlib'}. Default: 'Moral88'.
|
12
|
+
|
13
|
+
Returns:
|
14
|
+
- float or list: Calculated error based on selected method and library.
|
7
15
|
"""
|
8
|
-
|
9
|
-
|
16
|
+
if library == 'Moral88':
|
17
|
+
# Original implementation
|
18
|
+
if threshold is not None:
|
19
|
+
y_pred = [min(max(pred, threshold[0]), threshold[1]) for pred in y_pred]
|
20
|
+
|
21
|
+
absolute_errors = [abs(y_t - y_p) for y_t, y_p in zip(y_true, y_pred)]
|
22
|
+
|
23
|
+
if method == 'mean':
|
24
|
+
result = sum(absolute_errors) / len(y_true)
|
25
|
+
elif method == 'sum':
|
26
|
+
result = sum(absolute_errors)
|
27
|
+
elif method == 'none':
|
28
|
+
result = absolute_errors
|
29
|
+
else:
|
30
|
+
raise ValueError("Invalid method. Choose from {'mean', 'sum', 'none'}.")
|
31
|
+
|
32
|
+
if normalize and method != 'none':
|
33
|
+
range_y = max(y_true) - min(y_true)
|
34
|
+
result = result / max(abs(range_y), 1)
|
35
|
+
|
36
|
+
return result
|
37
|
+
|
38
|
+
elif library == 'sklearn':
|
39
|
+
from sklearn.metrics import mean_absolute_error as sklearn_mae
|
40
|
+
return sklearn_mae(y_true, y_pred)
|
41
|
+
|
42
|
+
elif library == 'torch':
|
43
|
+
import torch
|
44
|
+
y_true_tensor = torch.tensor(y_true, dtype=torch.float32)
|
45
|
+
y_pred_tensor = torch.tensor(y_pred, dtype=torch.float32)
|
46
|
+
return torch.mean(torch.abs(y_true_tensor - y_pred_tensor)).item()
|
47
|
+
|
48
|
+
elif library == 'tensorflow':
|
49
|
+
import tensorflow as tf
|
50
|
+
y_true_tensor = tf.convert_to_tensor(y_true, dtype=tf.float32)
|
51
|
+
y_pred_tensor = tf.convert_to_tensor(y_pred, dtype=tf.float32)
|
52
|
+
return tf.reduce_mean(tf.abs(y_true_tensor - y_pred_tensor)).numpy()
|
53
|
+
|
54
|
+
# elif library == 'statsmodel':
|
55
|
+
# raise NotImplementedError("Statsmodel does not have a built-in MAE implementation.")
|
56
|
+
|
57
|
+
# elif library == 'Dask-ML':
|
58
|
+
# raise NotImplementedError("Dask-ML support is not implemented yet.")
|
59
|
+
|
60
|
+
# elif library == 'MLlib':
|
61
|
+
# raise NotImplementedError("MLlib support is not implemented yet.")
|
62
|
+
|
63
|
+
else:
|
64
|
+
raise ValueError(f"Invalid library: {library}. Choose from {'Moral88', 'sklearn', 'torch', 'tensorflow'}.")
|
10
65
|
|
11
|
-
|
66
|
+
|
67
|
+
def mean_squared_error(y_true, y_pred, normalize=True, threshold=None, method='mean', library='Moral88'):
|
12
68
|
"""
|
13
|
-
|
69
|
+
Calculate Mean Squared Error (MSE) or variants based on method and library.
|
70
|
+
|
71
|
+
Parameters:
|
72
|
+
- y_true (list or array): True values (required)
|
73
|
+
- y_pred (list or array): Predicted values (required)
|
74
|
+
- normalize (bool): If True, normalize the result (default: True)
|
75
|
+
- threshold (tuple, optional): Apply a threshold to the predictions (default: None)
|
76
|
+
- method (str): Method of calculation. Options: {'mean', 'sum', 'none'}. Default: 'mean'
|
77
|
+
- library (str): Library to use for calculations. Options: {'Moral88', 'sklearn', 'torch', 'tensor', 'statsmodel', 'Dask-ML', 'MLlib'}. Default: 'Moral88'.
|
78
|
+
|
79
|
+
Returns:
|
80
|
+
- float or list: Calculated error based on selected method and library.
|
14
81
|
"""
|
15
|
-
|
16
|
-
|
82
|
+
if library == 'Moral88':
|
83
|
+
# Original implementation
|
84
|
+
if threshold is not None:
|
85
|
+
y_pred = [min(max(pred, threshold[0]), threshold[1]) for pred in y_pred]
|
86
|
+
|
87
|
+
squared_errors = [(y_t - y_p) ** 2 for y_t, y_p in zip(y_true, y_pred)]
|
88
|
+
|
89
|
+
if method == 'mean':
|
90
|
+
result = sum(squared_errors) / len(y_true)
|
91
|
+
elif method == 'sum':
|
92
|
+
result = sum(squared_errors)
|
93
|
+
elif method == 'none':
|
94
|
+
result = squared_errors
|
95
|
+
else:
|
96
|
+
raise ValueError("Invalid method. Choose from {'mean', 'sum', 'none'}.")
|
97
|
+
|
98
|
+
if normalize and method != 'none':
|
99
|
+
range_y = max(y_true) - min(y_true)
|
100
|
+
result = result / max(abs(range_y), 1)
|
101
|
+
|
102
|
+
return result
|
103
|
+
|
104
|
+
elif library == 'sklearn':
|
105
|
+
from sklearn.metrics import mean_squared_error as sklearn_mse
|
106
|
+
return sklearn_mse(y_true, y_pred)
|
107
|
+
|
108
|
+
elif library == 'torch':
|
109
|
+
import torch
|
110
|
+
y_true_tensor = torch.tensor(y_true, dtype=torch.float32)
|
111
|
+
y_pred_tensor = torch.tensor(y_pred, dtype=torch.float32)
|
112
|
+
return torch.mean((y_true_tensor - y_pred_tensor) ** 2).item()
|
113
|
+
|
114
|
+
elif library == 'tensorflow':
|
115
|
+
import tensorflow as tf
|
116
|
+
y_true_tensor = tf.convert_to_tensor(y_true, dtype=tf.float32)
|
117
|
+
y_pred_tensor = tf.convert_to_tensor(y_pred, dtype=tf.float32)
|
118
|
+
return tf.reduce_mean(tf.square(y_true_tensor - y_pred_tensor)).numpy()
|
119
|
+
|
120
|
+
# elif library == 'statsmodel':
|
121
|
+
# raise NotImplementedError("Statsmodel does not have a built-in MSE implementation.")
|
122
|
+
|
123
|
+
# elif library == 'Dask-ML':
|
124
|
+
# raise NotImplementedError("Dask-ML support is not implemented yet.")
|
125
|
+
|
126
|
+
# elif library == 'MLlib':
|
127
|
+
# raise NotImplementedError("MLlib support is not implemented yet.")
|
17
128
|
|
129
|
+
else:
|
130
|
+
raise ValueError(f"Invalid library: {library}. Choose from {'Moral88', 'sklearn', 'torch', 'tensorflow'}.")
|
18
131
|
def r_squared(y_true, y_pred):
|
19
132
|
"""
|
20
133
|
Compute R-Squared
|
21
134
|
"""
|
135
|
+
import numpy as np
|
22
136
|
y_true, y_pred = np.array(y_true), np.array(y_pred)
|
23
137
|
ss_total = np.sum((y_true - np.mean(y_true)) ** 2)
|
24
138
|
ss_residual = np.sum((y_true - y_pred) ** 2)
|
25
|
-
return 1 - (ss_residual / ss_total)
|
139
|
+
return 1 - (ss_residual / ss_total)
|
@@ -0,0 +1,7 @@
|
|
1
|
+
Moral88/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
2
|
+
Moral88/regression.py,sha256=W_galfrJHIbyP8S1YL0_GMBCKVKfbI2uJfI1PRsAaOk,5939
|
3
|
+
Moral88-0.2.0.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
4
|
+
Moral88-0.2.0.dist-info/METADATA,sha256=L3o3beMiux_cQtS6PE8sWQbC6Q2K4RuId_n6X7-tEtg,407
|
5
|
+
Moral88-0.2.0.dist-info/WHEEL,sha256=pkctZYzUS4AYVn6dJ-7367OJZivF2e8RA9b_ZBjif18,92
|
6
|
+
Moral88-0.2.0.dist-info/top_level.txt,sha256=-dyn5iTprnSUHbtMpvRO-prJsIoaRxao7wlfCHLSsv4,8
|
7
|
+
Moral88-0.2.0.dist-info/RECORD,,
|
Moral88-0.1.0.dist-info/RECORD
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
Moral88/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
2
|
-
Moral88/regression.py,sha256=5Rtc3EduWt8FZmyu9mLCqwoA1s4Hb7ToGh5yev_1rO0,727
|
3
|
-
Moral88-0.1.0.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
4
|
-
Moral88-0.1.0.dist-info/METADATA,sha256=eERIH5OJsirY5Wa6DvSpLrWj9wTobBSRAnHMWs3BGro,407
|
5
|
-
Moral88-0.1.0.dist-info/WHEEL,sha256=pkctZYzUS4AYVn6dJ-7367OJZivF2e8RA9b_ZBjif18,92
|
6
|
-
Moral88-0.1.0.dist-info/top_level.txt,sha256=-dyn5iTprnSUHbtMpvRO-prJsIoaRxao7wlfCHLSsv4,8
|
7
|
-
Moral88-0.1.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|