Moral88 0.1.0__tar.gz → 0.2.0__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,139 @@
1
+ def mean_absolute_error(y_true, y_pred, normalize=True, threshold=None, method='mean', library='Moral88'):
2
+ """
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.
15
+ """
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'}.")
65
+
66
+
67
+ def mean_squared_error(y_true, y_pred, normalize=True, threshold=None, method='mean', library='Moral88'):
68
+ """
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.
81
+ """
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.")
128
+
129
+ else:
130
+ raise ValueError(f"Invalid library: {library}. Choose from {'Moral88', 'sklearn', 'torch', 'tensorflow'}.")
131
+ def r_squared(y_true, y_pred):
132
+ """
133
+ Compute R-Squared
134
+ """
135
+ import numpy as np
136
+ y_true, y_pred = np.array(y_true), np.array(y_pred)
137
+ ss_total = np.sum((y_true - np.mean(y_true)) ** 2)
138
+ ss_residual = np.sum((y_true - y_pred) ** 2)
139
+ return 1 - (ss_residual / ss_total)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: Moral88
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: A library for regression evaluation metrics.
5
5
  Author: Morteza Alizadeh
6
6
  Author-email: alizadeh.c2m@gmail.com
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: Moral88
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: A library for regression evaluation metrics.
5
5
  Author: Morteza Alizadeh
6
6
  Author-email: alizadeh.c2m@gmail.com
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name='Moral88',
5
- version='0.1.0',
5
+ version='0.2.0',
6
6
  description='A library for regression evaluation metrics.',
7
7
  author='Morteza Alizadeh',
8
8
  author_email='alizadeh.c2m@gmail.com',
@@ -1,25 +0,0 @@
1
- # Moral88/regression.py
2
- import numpy as np
3
-
4
- def mean_absolute_error(y_true, y_pred):
5
- """
6
- Compute MAE (Mean Absolute Error)
7
- """
8
- y_true, y_pred = np.array(y_true), np.array(y_pred)
9
- return np.mean(np.abs(y_true - y_pred))
10
-
11
- def mean_squared_error(y_true, y_pred):
12
- """
13
- Compute MSE (Mean Squared Error)
14
- """
15
- y_true, y_pred = np.array(y_true), np.array(y_pred)
16
- return np.mean((y_true - y_pred) ** 2)
17
-
18
- def r_squared(y_true, y_pred):
19
- """
20
- Compute R-Squared
21
- """
22
- y_true, y_pred = np.array(y_true), np.array(y_pred)
23
- ss_total = np.sum((y_true - np.mean(y_true)) ** 2)
24
- ss_residual = np.sum((y_true - y_pred) ** 2)
25
- return 1 - (ss_residual / ss_total)
File without changes
File without changes
File without changes
File without changes