6
6
7
7
from typing import TYPE_CHECKING
8
8
if TYPE_CHECKING :
9
- from typing import Tuple
9
+ from typing import Optional , Tuple , Union
10
10
from numpy import ndarray , dtype
11
11
12
12
from typing import NamedTuple
13
13
14
- from numpy import (arccos , arccosh , arcsin , arcsinh , arctan , arctan2 , arctanh ,
15
- left_shift , invert , right_shift , bool_ , concatenate , power ,
16
- transpose , unique )
14
+ import numpy as np
17
15
18
16
# Basic renames
19
- acos = arccos
20
- acosh = arccosh
21
- asin = arcsin
22
- asinh = arcsinh
23
- atan = arctan
24
- atan2 = arctan2
25
- atanh = arctanh
26
- bitwise_left_shift = left_shift
27
- bitwise_invert = invert
28
- bitwise_right_shift = right_shift
29
- bool = bool_
30
- concat = concatenate
31
- pow = power
17
+ acos = np . arccos
18
+ acosh = np . arccosh
19
+ asin = np . arcsin
20
+ asinh = np . arcsinh
21
+ atan = np . arctan
22
+ atan2 = np . arctan2
23
+ atanh = np . arctanh
24
+ bitwise_left_shift = np . left_shift
25
+ bitwise_invert = np . invert
26
+ bitwise_right_shift = np . right_shift
27
+ bool = np . bool_
28
+ concat = np . concatenate
29
+ pow = np . power
32
30
33
31
# These functions are modified from the NumPy versions.
34
32
35
- # Unlike transpose(), the axes argument to permute_dims() is required.
36
- def permute_dims (x : ndarray , / , axes : Tuple [int , ...]) -> ndarray :
37
- return transpose (x , axes )
38
-
39
33
# np.unique() is split into four functions in the array API:
40
34
# unique_all, unique_counts, unique_inverse, and unique_values (this is done
41
35
# to remove polymorphic return types).
@@ -60,7 +54,7 @@ class UniqueInverseResult(NamedTuple):
60
54
61
55
62
56
def unique_all (x : ndarray , / ) -> UniqueAllResult :
63
- values , indices , inverse_indices , counts = unique (
57
+ values , indices , inverse_indices , counts = np . unique (
64
58
x ,
65
59
return_counts = True ,
66
60
return_index = True ,
@@ -79,7 +73,7 @@ def unique_all(x: ndarray, /) -> UniqueAllResult:
79
73
80
74
81
75
def unique_counts (x : ndarray , / ) -> UniqueCountsResult :
82
- res = unique (
76
+ res = np . unique (
83
77
x ,
84
78
return_counts = True ,
85
79
return_index = False ,
@@ -91,7 +85,7 @@ def unique_counts(x: ndarray, /) -> UniqueCountsResult:
91
85
92
86
93
87
def unique_inverse (x : ndarray , / ) -> UniqueInverseResult :
94
- values , inverse_indices = unique (
88
+ values , inverse_indices = np . unique (
95
89
x ,
96
90
return_counts = False ,
97
91
return_index = False ,
@@ -105,7 +99,7 @@ def unique_inverse(x: ndarray, /) -> UniqueInverseResult:
105
99
106
100
107
101
def unique_values (x : ndarray , / ) -> ndarray :
108
- return unique (
102
+ return np . unique (
109
103
x ,
110
104
return_counts = False ,
111
105
return_index = False ,
@@ -118,5 +112,38 @@ def astype(x: ndarray, dtype: dtype, /, *, copy: bool = True) -> ndarray:
118
112
return x
119
113
return x .astype (dtype = dtype , copy = copy )
120
114
115
+ # These functions have different keyword argument names
116
+
117
+ def std (
118
+ x : ndarray ,
119
+ / ,
120
+ * ,
121
+ axis : Optional [Union [int , Tuple [int , ...]]] = None ,
122
+ correction : Union [int , float ] = 0.0 , # correction instead of ddof
123
+ keepdims : bool = False ,
124
+ ) -> ndarray :
125
+ return np .std (x , axis = axis , ddof = correction , keepdims = keepdims )
126
+
127
+ def var (
128
+ x : ndarray ,
129
+ / ,
130
+ * ,
131
+ axis : Optional [Union [int , Tuple [int , ...]]] = None ,
132
+ correction : Union [int , float ] = 0.0 , # correction instead of ddof
133
+ keepdims : bool = False ,
134
+ ) -> ndarray :
135
+ return np .var (x , axis = axis , ddof = correction , keepdims = keepdims )
136
+
137
+ # Unlike transpose(), the axes argument to permute_dims() is required.
138
+ def permute_dims (x : ndarray , / , axes : Tuple [int , ...]) -> ndarray :
139
+ return np .transpose (x , axes )
140
+
121
141
# from numpy import * doesn't overwrite these builtin names
122
142
from numpy import abs , max , min , round
143
+
144
+ __all__ = ['acos' , 'acosh' , 'asin' , 'asinh' , 'atan' , 'atan2' , 'atanh' ,
145
+ 'bitwise_left_shift' , 'bitwise_invert' , 'bitwise_right_shift' ,
146
+ 'bool' , 'concat' , 'pow' , 'UniqueAllResult' , 'UniqueCountsResult' ,
147
+ 'UniqueInverseResult' , 'unique_all' , 'unique_counts' ,
148
+ 'unique_inverse' , 'unique_values' , 'astype' , 'abs' , 'max' , 'min' ,
149
+ 'round' , 'std' , 'var' , 'permute_dims' ]
0 commit comments