tools 1.0__tar.gz → 1.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {tools-1.0 → tools-1.0.2}/PKG-INFO +7 -5
- {tools-1.0 → tools-1.0.2}/README.md +6 -4
- {tools-1.0 → tools-1.0.2}/tools/__init__.py +1 -1
- tools-1.0.2/tools/sklearn/metrics.py +112 -0
- {tools-1.0 → tools-1.0.2}/tools.egg-info/PKG-INFO +7 -5
- tools-1.0/tools/sklearn/metrics.py +0 -82
- {tools-1.0 → tools-1.0.2}/LICENSE.txt +0 -0
- {tools-1.0 → tools-1.0.2}/setup.cfg +0 -0
- {tools-1.0 → tools-1.0.2}/setup.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/array.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/data.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/exp.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/hydra/__init__.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/modules.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/numpy/__init__.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/numpy/_f.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/numpy/_utils.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/os.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/pandas/__init__.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/plot/__init__.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/plot/sklearn.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/plot/utils.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/random.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/sklearn/__init__.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/sklearn/model_selection.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/sklearn/preprocessing.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/stats/__init__.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/tools.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/torch/__init__.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/torch/_pandas.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/torch/data.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/torch/estimator.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/torch/federated_learning.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/torch/layers.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/torch/model.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/torch/optim/__init__.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/torch/optim/lr_scheduler.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/torch/plot.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools/torch/utils.py +0 -0
- {tools-1.0 → tools-1.0.2}/tools.egg-info/SOURCES.txt +0 -0
- {tools-1.0 → tools-1.0.2}/tools.egg-info/dependency_links.txt +0 -0
- {tools-1.0 → tools-1.0.2}/tools.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: tools
|
|
3
|
-
Version: 1.0
|
|
3
|
+
Version: 1.0.2
|
|
4
4
|
Summary: python syntax tool
|
|
5
5
|
Home-page: https://github.com/jsyoo61/tools
|
|
6
6
|
Author: JaeSung Yoo
|
|
@@ -25,13 +25,15 @@ jsyoo61@unc.edu
|
|
|
25
25
|
T.save_pickle(data, 'data.p')
|
|
26
26
|
data = T.load_pickle('data.p')
|
|
27
27
|
|
|
28
|
-
Most useful functions are in `tools.tools
|
|
28
|
+
Most useful functions are in `tools.tools` which is loaded to the main module by default. \
|
|
29
29
|
API dependent tools are in their corresponding folders: `tools.API_name` (i.e. `tools.torch.optim`)
|
|
30
30
|
|
|
31
31
|
Planning to document using chatGPT,
|
|
32
32
|
Not documented yet, so you'll have to look at documents written inside the codes. (Most codes contain explanation)
|
|
33
33
|
|
|
34
|
-
I'm
|
|
35
|
-
Please email me before using it.
|
|
34
|
+
I'm happy to discuss questions or suggestions, please contact me via email. :)
|
|
36
35
|
|
|
37
|
-
|
|
36
|
+
1.0.1. (2025.5.1.)
|
|
37
|
+
|
|
38
|
+
- `tools.sklearn.metrics.r2_score()` `multioutput` argument fixed
|
|
39
|
+
- `tools.sklearn.metrics` axis=None returns a single np.float object
|
|
@@ -11,13 +11,15 @@ jsyoo61@unc.edu
|
|
|
11
11
|
T.save_pickle(data, 'data.p')
|
|
12
12
|
data = T.load_pickle('data.p')
|
|
13
13
|
|
|
14
|
-
Most useful functions are in `tools.tools
|
|
14
|
+
Most useful functions are in `tools.tools` which is loaded to the main module by default. \
|
|
15
15
|
API dependent tools are in their corresponding folders: `tools.API_name` (i.e. `tools.torch.optim`)
|
|
16
16
|
|
|
17
17
|
Planning to document using chatGPT,
|
|
18
18
|
Not documented yet, so you'll have to look at documents written inside the codes. (Most codes contain explanation)
|
|
19
19
|
|
|
20
|
-
I'm
|
|
21
|
-
Please email me before using it.
|
|
20
|
+
I'm happy to discuss questions or suggestions, please contact me via email. :)
|
|
22
21
|
|
|
23
|
-
|
|
22
|
+
1.0.1. (2025.5.1.)
|
|
23
|
+
|
|
24
|
+
- `tools.sklearn.metrics.r2_score()` `multioutput` argument fixed
|
|
25
|
+
- `tools.sklearn.metrics` axis=None returns a single np.float object
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
from collections.abc import Iterable
|
|
2
|
+
import warnings
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
from sklearn.metrics import r2_score as r2_score_sklearn
|
|
6
|
+
|
|
7
|
+
def squared_error(y_true, y_pred, axis=None):
|
|
8
|
+
"""
|
|
9
|
+
MSE averaging across specified axis.
|
|
10
|
+
if axis=(), returns entry-by-entry squared error.
|
|
11
|
+
|
|
12
|
+
Parameters
|
|
13
|
+
----------
|
|
14
|
+
y_true : np.ndarray
|
|
15
|
+
y_pred : np.ndarray
|
|
16
|
+
|
|
17
|
+
axis : int or iterable of int, default=None
|
|
18
|
+
Axis to collapse.
|
|
19
|
+
|
|
20
|
+
Returns
|
|
21
|
+
-------
|
|
22
|
+
score : numpy.ndarray of shape with collapsed axis
|
|
23
|
+
"""
|
|
24
|
+
if axis is None:
|
|
25
|
+
axis = tuple(range(y_true.ndim)) # Collapse all dimensions
|
|
26
|
+
score = (y_true-y_pred)**2
|
|
27
|
+
score = np.mean(score, axis=axis)
|
|
28
|
+
return score
|
|
29
|
+
|
|
30
|
+
def absolute_error(y_true, y_pred, axis=None):
|
|
31
|
+
"""
|
|
32
|
+
MAE averaging across specified axis.
|
|
33
|
+
if axis=(), returns entry-by-entry squared error.
|
|
34
|
+
|
|
35
|
+
Parameters
|
|
36
|
+
----------
|
|
37
|
+
y_true : np.ndarray
|
|
38
|
+
y_pred : np.ndarray
|
|
39
|
+
|
|
40
|
+
axis : int or iterable of int, default=None
|
|
41
|
+
Axis to collapse.
|
|
42
|
+
|
|
43
|
+
Returns
|
|
44
|
+
-------
|
|
45
|
+
score : numpy.ndarray of shape with collapsed axis
|
|
46
|
+
"""
|
|
47
|
+
if axis is None:
|
|
48
|
+
axis = tuple(range(y_true.ndim)) # Collapse all dimensions
|
|
49
|
+
score = np.abs(y_true-y_pred)
|
|
50
|
+
score = np.mean(score, axis=axis)
|
|
51
|
+
return score
|
|
52
|
+
|
|
53
|
+
def r2_score(y_true, y_pred, axis=None, multioutput='raw_values'):
|
|
54
|
+
"""
|
|
55
|
+
R^2 score for multidimensional predictions.
|
|
56
|
+
collapses all axes except the specified axis.
|
|
57
|
+
|
|
58
|
+
Parameters
|
|
59
|
+
----------
|
|
60
|
+
y_true : np.ndarray
|
|
61
|
+
y_pred : np.ndarray
|
|
62
|
+
axis: int or iterable of int, default=None
|
|
63
|
+
Axis to collapse.
|
|
64
|
+
It must be specified if y_true and y_pred dimensions are > 2
|
|
65
|
+
multioutput : Reference to `sklearn.metrics.r2_score`
|
|
66
|
+
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html
|
|
67
|
+
|
|
68
|
+
Returns
|
|
69
|
+
-------
|
|
70
|
+
z : np.ndarray
|
|
71
|
+
if axis is specified, returns an array of shape (y_true.shape[axis],)
|
|
72
|
+
"""
|
|
73
|
+
assert y_true.shape == y_pred.shape, f"y_true and y_pred must have the same shape, received: {y_true.shape} and {y_pred.shape}"
|
|
74
|
+
if axis is None:
|
|
75
|
+
axis = list(range(y_true.ndim)) # Collapse all dimensions
|
|
76
|
+
axis_was_none = True
|
|
77
|
+
elif not isinstance(axis, Iterable):
|
|
78
|
+
axis = [axis]
|
|
79
|
+
axis_was_none = False
|
|
80
|
+
else:
|
|
81
|
+
axis = list(axis)
|
|
82
|
+
axis_was_none = False
|
|
83
|
+
|
|
84
|
+
if len(axis) > y_true.ndim:
|
|
85
|
+
raise ValueError("Axis is greater than the number of dimensions of y_true and y_pred")
|
|
86
|
+
|
|
87
|
+
dim_collapse = axis
|
|
88
|
+
dim_final = list(set(range(y_true.ndim)).difference(axis))
|
|
89
|
+
shape_final = np.array(y_true.shape)[dim_final] if len(dim_final)!=0 else (1,)
|
|
90
|
+
|
|
91
|
+
y_true = np.transpose(y_true, (*dim_collapse, *dim_final)).reshape(-1, np.prod(shape_final)) # Move axis to the end, and flatten the rest
|
|
92
|
+
y_pred = np.transpose(y_pred, (*dim_collapse, *dim_final)).reshape(-1, np.prod(shape_final))
|
|
93
|
+
|
|
94
|
+
# score = r2_score_sklearn(y_true, y_pred, multioutput=multioutput)
|
|
95
|
+
score = r2_score_sklearn(y_true, y_pred, multioutput='raw_values')
|
|
96
|
+
|
|
97
|
+
if type(score) == float and np.isnan(score).item():
|
|
98
|
+
warnings.warn("R2 score is a single NaN, shape matching with NaN")
|
|
99
|
+
score = np.full(shape_final, np.nan)
|
|
100
|
+
return score
|
|
101
|
+
|
|
102
|
+
if multioutput == 'raw_values':
|
|
103
|
+
score = score.reshape(*shape_final) if not axis_was_none else score[0]
|
|
104
|
+
return score
|
|
105
|
+
elif multioutput == 'uniform_average':
|
|
106
|
+
return score.mean()
|
|
107
|
+
elif multioutput == 'variance_weighted':
|
|
108
|
+
return np.average(score, weights=np.var(y_true, axis=0))
|
|
109
|
+
else:
|
|
110
|
+
raise ValueError("multioutput must be one of ['raw_values', 'uniform_average', 'variance_weighted']")
|
|
111
|
+
|
|
112
|
+
# %%
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: tools
|
|
3
|
-
Version: 1.0
|
|
3
|
+
Version: 1.0.2
|
|
4
4
|
Summary: python syntax tool
|
|
5
5
|
Home-page: https://github.com/jsyoo61/tools
|
|
6
6
|
Author: JaeSung Yoo
|
|
@@ -25,13 +25,15 @@ jsyoo61@unc.edu
|
|
|
25
25
|
T.save_pickle(data, 'data.p')
|
|
26
26
|
data = T.load_pickle('data.p')
|
|
27
27
|
|
|
28
|
-
Most useful functions are in `tools.tools
|
|
28
|
+
Most useful functions are in `tools.tools` which is loaded to the main module by default. \
|
|
29
29
|
API dependent tools are in their corresponding folders: `tools.API_name` (i.e. `tools.torch.optim`)
|
|
30
30
|
|
|
31
31
|
Planning to document using chatGPT,
|
|
32
32
|
Not documented yet, so you'll have to look at documents written inside the codes. (Most codes contain explanation)
|
|
33
33
|
|
|
34
|
-
I'm
|
|
35
|
-
Please email me before using it.
|
|
34
|
+
I'm happy to discuss questions or suggestions, please contact me via email. :)
|
|
36
35
|
|
|
37
|
-
|
|
36
|
+
1.0.1. (2025.5.1.)
|
|
37
|
+
|
|
38
|
+
- `tools.sklearn.metrics.r2_score()` `multioutput` argument fixed
|
|
39
|
+
- `tools.sklearn.metrics` axis=None returns a single np.float object
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
from collections.abc import Iterable
|
|
2
|
-
import warnings
|
|
3
|
-
|
|
4
|
-
import numpy as np
|
|
5
|
-
from sklearn.metrics import r2_score as r2_score_sklearn
|
|
6
|
-
|
|
7
|
-
def squared_error(y_true, y_pred, axis=None):
|
|
8
|
-
'''MSE without mean
|
|
9
|
-
if axis is provided, it will return the mean along the axis
|
|
10
|
-
'''
|
|
11
|
-
score = (y_true-y_pred)**2
|
|
12
|
-
if axis is not None:
|
|
13
|
-
score = np.mean(score, axis=axis)
|
|
14
|
-
return score
|
|
15
|
-
|
|
16
|
-
def absolute_error(y_true, y_pred, axis=None):
|
|
17
|
-
'''MAE without mean'''
|
|
18
|
-
score = np.abs(y_true-y_pred)
|
|
19
|
-
if axis is not None:
|
|
20
|
-
score = np.mean(score, axis=axis)
|
|
21
|
-
return score
|
|
22
|
-
|
|
23
|
-
def r2_score(y_true, y_pred, axis=None, multioutput='raw_values'):
|
|
24
|
-
"""
|
|
25
|
-
R^2 score for multidimensional predictions.
|
|
26
|
-
collapses all axes except the specified axis.
|
|
27
|
-
|
|
28
|
-
Parameters
|
|
29
|
-
----------
|
|
30
|
-
y_true : np.ndarray
|
|
31
|
-
y_pred : np.ndarray
|
|
32
|
-
axis: int or iterable of int, default=None
|
|
33
|
-
Axis to collapse.
|
|
34
|
-
It must be specified if y_true and y_pred dimensions are > 2
|
|
35
|
-
multioutput : Reference to `sklearn.metrics.r2_score`
|
|
36
|
-
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html
|
|
37
|
-
|
|
38
|
-
Returns
|
|
39
|
-
-------
|
|
40
|
-
z : np.ndarray
|
|
41
|
-
if axis is specified, returns an array of shape (y_true.shape[axis],)
|
|
42
|
-
"""
|
|
43
|
-
assert y_true.shape == y_pred.shape, f"y_true and y_pred must have the same shape, received: {y_true.shape} and {y_pred.shape}"
|
|
44
|
-
if axis is not None:
|
|
45
|
-
if not isinstance(axis, Iterable):
|
|
46
|
-
axis = [axis]
|
|
47
|
-
else:
|
|
48
|
-
axis = list(axis)
|
|
49
|
-
if len(axis) > y_true.ndim:
|
|
50
|
-
raise ValueError("Axis is greater than the number of dimensions of y_true and y_pred")
|
|
51
|
-
|
|
52
|
-
# shape_final = np.array(y_true.shape)[axis]
|
|
53
|
-
# shape_collapse = list(set(range(y_true.ndim)).difference(axis))
|
|
54
|
-
dim_collapse = axis
|
|
55
|
-
dim_final = list(set(range(y_true.ndim)).difference(axis))
|
|
56
|
-
shape_final = np.array(y_true.shape)[dim_final]
|
|
57
|
-
|
|
58
|
-
y_true = np.transpose(y_true, (*dim_collapse, *dim_final)).reshape(-1, np.prod(shape_final)) # Move axis to the end, and flatten the rest
|
|
59
|
-
y_pred = np.transpose(y_pred, (*dim_collapse, *dim_final)).reshape(-1, np.prod(shape_final))
|
|
60
|
-
|
|
61
|
-
score = r2_score_sklearn(y_true, y_pred, multioutput=multioutput)
|
|
62
|
-
|
|
63
|
-
if type(score) == float and np.isnan(score).item():
|
|
64
|
-
warnings.warn("R2 score is a single NaN, shape matching with NaN")
|
|
65
|
-
score = np.full(shape_final, np.nan)
|
|
66
|
-
return score
|
|
67
|
-
|
|
68
|
-
if multioutput == 'raw_values':
|
|
69
|
-
return score.reshape(*shape_final)
|
|
70
|
-
elif multioutput == 'uniform_average':
|
|
71
|
-
return score
|
|
72
|
-
elif multioutput == 'variance_weighted':
|
|
73
|
-
# return np.mean(score, weights=np.var(y_true, axis=0))
|
|
74
|
-
raise NotImplementedError("variance_weighted is not implemented yet")
|
|
75
|
-
else:
|
|
76
|
-
raise ValueError("multioutput must be one of ['raw_values', 'uniform_average', 'variance_weighted']")
|
|
77
|
-
|
|
78
|
-
else:
|
|
79
|
-
assert (y_true.ndim <= 2) and (y_pred.ndim <= 2), "If axis is None, y_true and y_pred must be smaller than 2D"
|
|
80
|
-
return r2_score_sklearn(y_true, y_pred, multioutput=multioutput)
|
|
81
|
-
|
|
82
|
-
# %%
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|