substratumx 0.1.2__cp311-cp311-macosx_11_0_arm64.whl
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.
- substratum/__init__.py +5 -0
- substratum/__init__.pyi +941 -0
- substratum/py.typed +0 -0
- substratum/substratum.cpython-311-darwin.so +0 -0
- substratum.pyi +941 -0
- substratumx-0.1.2.dist-info/METADATA +4 -0
- substratumx-0.1.2.dist-info/RECORD +8 -0
- substratumx-0.1.2.dist-info/WHEEL +4 -0
substratum/__init__.pyi
ADDED
|
@@ -0,0 +1,941 @@
|
|
|
1
|
+
"""A Rust-based ndarray library. """
|
|
2
|
+
|
|
3
|
+
from typing import Iterator, List, Literal, Sequence, Tuple, overload
|
|
4
|
+
|
|
5
|
+
class linalg:
|
|
6
|
+
"""Linear algebra functions."""
|
|
7
|
+
|
|
8
|
+
@staticmethod
|
|
9
|
+
def matmul(a: Array, b: Array) -> Array:
|
|
10
|
+
"""Matrix multiplication."""
|
|
11
|
+
...
|
|
12
|
+
|
|
13
|
+
@staticmethod
|
|
14
|
+
def dot(a: Array, b: Array) -> Array:
|
|
15
|
+
"""Dot/matrix product."""
|
|
16
|
+
...
|
|
17
|
+
|
|
18
|
+
@staticmethod
|
|
19
|
+
def transpose(a: Array) -> Array:
|
|
20
|
+
"""Transpose a 2D matrix."""
|
|
21
|
+
...
|
|
22
|
+
|
|
23
|
+
@staticmethod
|
|
24
|
+
def cholesky(a: Array) -> Array:
|
|
25
|
+
"""Compute Cholesky decomposition.
|
|
26
|
+
|
|
27
|
+
Returns lower triangular matrix L where A = L @ L.T.
|
|
28
|
+
|
|
29
|
+
Raises:
|
|
30
|
+
ValueError: If matrix is not square or not positive-definite.
|
|
31
|
+
"""
|
|
32
|
+
...
|
|
33
|
+
|
|
34
|
+
@staticmethod
|
|
35
|
+
def qr(a: Array) -> tuple[Array, Array]:
|
|
36
|
+
"""QR decomposition.
|
|
37
|
+
|
|
38
|
+
Returns (Q, R) where A = Q @ R, Q is orthogonal and R is upper triangular.
|
|
39
|
+
|
|
40
|
+
Raises:
|
|
41
|
+
ValueError: If array is not 2D.
|
|
42
|
+
"""
|
|
43
|
+
...
|
|
44
|
+
|
|
45
|
+
@staticmethod
|
|
46
|
+
def eig(a: Array) -> tuple[Array, Array]:
|
|
47
|
+
"""Compute eigenvalues and eigenvectors.
|
|
48
|
+
|
|
49
|
+
Returns:
|
|
50
|
+
Tuple of (eigenvalues, eigenvectors) where eigenvalues is a 1D array
|
|
51
|
+
and eigenvectors is a 2D array with eigenvectors as columns.
|
|
52
|
+
Eigenvalues are sorted by absolute value (descending).
|
|
53
|
+
|
|
54
|
+
Raises:
|
|
55
|
+
ValueError: If matrix is not square.
|
|
56
|
+
"""
|
|
57
|
+
...
|
|
58
|
+
|
|
59
|
+
@staticmethod
|
|
60
|
+
def eig_with_params(a: Array, max_iter: int = 1000, tol: float = 1e-10) -> tuple[Array, Array]:
|
|
61
|
+
"""Eigendecomposition with custom iteration parameters.
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
a: Input square matrix.
|
|
65
|
+
max_iter: Maximum number of QR iterations.
|
|
66
|
+
tol: Convergence tolerance for off-diagonal elements.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
Tuple of (eigenvalues, eigenvectors).
|
|
70
|
+
|
|
71
|
+
Raises:
|
|
72
|
+
ValueError: If matrix is not square.
|
|
73
|
+
"""
|
|
74
|
+
...
|
|
75
|
+
|
|
76
|
+
@staticmethod
|
|
77
|
+
def eigvals(a: Array) -> Array:
|
|
78
|
+
"""Compute eigenvalues only.
|
|
79
|
+
|
|
80
|
+
More efficient than eig() when eigenvectors are not needed.
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
1D array of eigenvalues sorted by absolute value (descending).
|
|
84
|
+
|
|
85
|
+
Raises:
|
|
86
|
+
ValueError: If matrix is not square.
|
|
87
|
+
"""
|
|
88
|
+
...
|
|
89
|
+
|
|
90
|
+
@staticmethod
|
|
91
|
+
def diagonal(a: Array, k: int | None = None) -> Array:
|
|
92
|
+
"""Extract the k-th diagonal from a 2D array."""
|
|
93
|
+
...
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class stats:
|
|
97
|
+
"""Statistical functions."""
|
|
98
|
+
|
|
99
|
+
@staticmethod
|
|
100
|
+
def sum(a: Array) -> float:
|
|
101
|
+
"""Sum of all elements."""
|
|
102
|
+
...
|
|
103
|
+
|
|
104
|
+
@staticmethod
|
|
105
|
+
def mean(a: Array) -> float:
|
|
106
|
+
"""Mean of all elements."""
|
|
107
|
+
...
|
|
108
|
+
|
|
109
|
+
@staticmethod
|
|
110
|
+
def var(a: Array) -> float:
|
|
111
|
+
"""Variance of all elements (population variance)."""
|
|
112
|
+
...
|
|
113
|
+
|
|
114
|
+
@staticmethod
|
|
115
|
+
def std(a: Array) -> float:
|
|
116
|
+
"""Standard deviation of all elements."""
|
|
117
|
+
...
|
|
118
|
+
|
|
119
|
+
@staticmethod
|
|
120
|
+
def median(a: Array) -> float:
|
|
121
|
+
"""Median of all elements."""
|
|
122
|
+
...
|
|
123
|
+
|
|
124
|
+
@overload
|
|
125
|
+
@staticmethod
|
|
126
|
+
def quantile(a: Array, q: float) -> float:
|
|
127
|
+
"""q-th quantile of all elements (q in [0, 1])."""
|
|
128
|
+
...
|
|
129
|
+
|
|
130
|
+
@overload
|
|
131
|
+
@staticmethod
|
|
132
|
+
def quantile(a: Array, q: Array) -> Array:
|
|
133
|
+
"""Compute multiple quantiles at once (vectorized).
|
|
134
|
+
|
|
135
|
+
Args:
|
|
136
|
+
a: Input array.
|
|
137
|
+
q: Array of quantile values, each in [0, 1].
|
|
138
|
+
|
|
139
|
+
Returns:
|
|
140
|
+
1D array of quantile values corresponding to each q.
|
|
141
|
+
"""
|
|
142
|
+
...
|
|
143
|
+
|
|
144
|
+
@staticmethod
|
|
145
|
+
def any(a: Array) -> bool:
|
|
146
|
+
"""True if any element is non-zero."""
|
|
147
|
+
...
|
|
148
|
+
|
|
149
|
+
@staticmethod
|
|
150
|
+
def all(a: Array) -> bool:
|
|
151
|
+
"""True if all elements are non-zero."""
|
|
152
|
+
...
|
|
153
|
+
|
|
154
|
+
@staticmethod
|
|
155
|
+
def pearson(a: Array, b: Array) -> float:
|
|
156
|
+
"""Compute Pearson correlation coefficient between two arrays.
|
|
157
|
+
|
|
158
|
+
Args:
|
|
159
|
+
a: First 1D array.
|
|
160
|
+
b: Second 1D array of the same length.
|
|
161
|
+
|
|
162
|
+
Returns:
|
|
163
|
+
Pearson correlation coefficient between -1 and 1.
|
|
164
|
+
"""
|
|
165
|
+
...
|
|
166
|
+
|
|
167
|
+
@staticmethod
|
|
168
|
+
def spearman(a: Array, b: Array) -> float:
|
|
169
|
+
"""Compute Spearman rank correlation coefficient between two arrays.
|
|
170
|
+
|
|
171
|
+
Args:
|
|
172
|
+
a: First 1D array.
|
|
173
|
+
b: Second 1D array of the same length.
|
|
174
|
+
|
|
175
|
+
Returns:
|
|
176
|
+
Spearman correlation coefficient between -1 and 1.
|
|
177
|
+
"""
|
|
178
|
+
...
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
class random:
|
|
182
|
+
"""Random number generation."""
|
|
183
|
+
|
|
184
|
+
class Generator:
|
|
185
|
+
"""Random number generator."""
|
|
186
|
+
|
|
187
|
+
def __init__(self) -> None:
|
|
188
|
+
"""Create time-seeded generator."""
|
|
189
|
+
...
|
|
190
|
+
|
|
191
|
+
@staticmethod
|
|
192
|
+
def from_seed(seed: int) -> random.Generator:
|
|
193
|
+
"""Create generator with explicit seed."""
|
|
194
|
+
...
|
|
195
|
+
|
|
196
|
+
@staticmethod
|
|
197
|
+
def new() -> random.Generator:
|
|
198
|
+
"""Create a generator."""
|
|
199
|
+
...
|
|
200
|
+
|
|
201
|
+
def uniform(self, low: float, high: float, shape: Sequence[int]) -> Array: ...
|
|
202
|
+
def standard_normal(self, shape: Sequence[int]) -> Array: ...
|
|
203
|
+
def normal(self, mu: float, sigma: float, shape: Sequence[int]) -> Array: ...
|
|
204
|
+
def randint(self, low: int, high: int, shape: Sequence[int]) -> Array: ...
|
|
205
|
+
def gamma(self, shape_param: float, scale: float, shape: Sequence[int]) -> Array:
|
|
206
|
+
"""Generate gamma-distributed random samples.
|
|
207
|
+
|
|
208
|
+
Args:
|
|
209
|
+
shape_param: Shape parameter (k or alpha), must be positive.
|
|
210
|
+
scale: Scale parameter (theta), must be positive.
|
|
211
|
+
shape: Output array shape.
|
|
212
|
+
|
|
213
|
+
Returns:
|
|
214
|
+
Array of gamma-distributed samples.
|
|
215
|
+
"""
|
|
216
|
+
...
|
|
217
|
+
def beta(self, alpha: float, beta: float, shape: Sequence[int]) -> Array:
|
|
218
|
+
"""Generate beta-distributed random samples.
|
|
219
|
+
|
|
220
|
+
Args:
|
|
221
|
+
alpha: First shape parameter, must be positive.
|
|
222
|
+
beta: Second shape parameter, must be positive.
|
|
223
|
+
shape: Output array shape.
|
|
224
|
+
|
|
225
|
+
Returns:
|
|
226
|
+
Array of beta-distributed samples in the interval (0, 1).
|
|
227
|
+
"""
|
|
228
|
+
...
|
|
229
|
+
|
|
230
|
+
def lognormal(self, mu: float, sigma: float, shape: Sequence[int]) -> Array:
|
|
231
|
+
"""Generate log-normal distributed random samples.
|
|
232
|
+
|
|
233
|
+
Args:
|
|
234
|
+
mu: Mean of the underlying normal distribution.
|
|
235
|
+
sigma: Standard deviation of the underlying normal distribution.
|
|
236
|
+
shape: Output array shape.
|
|
237
|
+
|
|
238
|
+
Returns:
|
|
239
|
+
Array of log-normal distributed samples.
|
|
240
|
+
"""
|
|
241
|
+
...
|
|
242
|
+
|
|
243
|
+
@staticmethod
|
|
244
|
+
def seed(seed: int) -> Generator:
|
|
245
|
+
"""Create a seeded random number generator."""
|
|
246
|
+
...
|
|
247
|
+
|
|
248
|
+
class Array(Sequence[float]):
|
|
249
|
+
"""N-dimensional array of float64 values."""
|
|
250
|
+
|
|
251
|
+
def __init__(self, shape: Sequence[int], data: Sequence[float]) -> None:
|
|
252
|
+
"""Create array from shape and flat data list."""
|
|
253
|
+
...
|
|
254
|
+
|
|
255
|
+
@staticmethod
|
|
256
|
+
def zeros(shape: Sequence[int]) -> Array:
|
|
257
|
+
"""Create array filled with zeros."""
|
|
258
|
+
...
|
|
259
|
+
|
|
260
|
+
@staticmethod
|
|
261
|
+
def ones(shape: Sequence[int]) -> Array:
|
|
262
|
+
"""Create array filled with ones."""
|
|
263
|
+
...
|
|
264
|
+
|
|
265
|
+
@staticmethod
|
|
266
|
+
def full(shape: Sequence[int], fill_value: float) -> Array:
|
|
267
|
+
"""Create array filled with a specified value."""
|
|
268
|
+
...
|
|
269
|
+
|
|
270
|
+
@staticmethod
|
|
271
|
+
def asarray(data: Sequence[float] | Array, shape: Sequence[int] | None = None) -> Array:
|
|
272
|
+
"""Create array from data with optional reshape.
|
|
273
|
+
|
|
274
|
+
Args:
|
|
275
|
+
data: Flat list of float values or existing Array.
|
|
276
|
+
shape: Optional shape. If None, creates a 1D array.
|
|
277
|
+
"""
|
|
278
|
+
...
|
|
279
|
+
|
|
280
|
+
@staticmethod
|
|
281
|
+
def eye(n: int, m: int | None = None, k: int | None = None) -> Array:
|
|
282
|
+
"""Create a 2D identity matrix with ones on the k-th diagonal."""
|
|
283
|
+
...
|
|
284
|
+
|
|
285
|
+
@staticmethod
|
|
286
|
+
def diag(v: Sequence[float] | Array, k: int | None = None) -> Array:
|
|
287
|
+
"""Create a 2D array with v on the k-th diagonal.
|
|
288
|
+
|
|
289
|
+
Args:
|
|
290
|
+
v: 1D array or list of diagonal values.
|
|
291
|
+
k: Diagonal offset (0=main, >0=upper, <0=lower).
|
|
292
|
+
"""
|
|
293
|
+
...
|
|
294
|
+
|
|
295
|
+
@staticmethod
|
|
296
|
+
def outer(a: Sequence[float] | Array, b: Sequence[float] | Array) -> Array:
|
|
297
|
+
"""Compute the outer product of two 1D arrays.
|
|
298
|
+
|
|
299
|
+
Args:
|
|
300
|
+
a: First 1D array or list.
|
|
301
|
+
b: Second 1D array or list.
|
|
302
|
+
"""
|
|
303
|
+
...
|
|
304
|
+
|
|
305
|
+
@property
|
|
306
|
+
def shape(self) -> List[int]:
|
|
307
|
+
"""Get the shape as a list."""
|
|
308
|
+
...
|
|
309
|
+
|
|
310
|
+
def get(self, indices: Sequence[int]) -> float:
|
|
311
|
+
"""Get element at indices."""
|
|
312
|
+
...
|
|
313
|
+
|
|
314
|
+
def tolist(self) -> List[float]:
|
|
315
|
+
"""Return data as flat list."""
|
|
316
|
+
...
|
|
317
|
+
|
|
318
|
+
def diagonal(self, k: int | None = None) -> Array:
|
|
319
|
+
"""Extract the k-th diagonal from a 2D array."""
|
|
320
|
+
...
|
|
321
|
+
|
|
322
|
+
def transpose(self) -> Array:
|
|
323
|
+
"""Transpose a 2D matrix."""
|
|
324
|
+
...
|
|
325
|
+
|
|
326
|
+
def t(self) -> Array:
|
|
327
|
+
"""Transpose (alias for transpose())."""
|
|
328
|
+
...
|
|
329
|
+
|
|
330
|
+
def take(self, indices: Sequence[int]) -> Array:
|
|
331
|
+
"""Take elements from the array at specified flat indices.
|
|
332
|
+
|
|
333
|
+
Args:
|
|
334
|
+
indices: List of flat (1D) indices to select from the array.
|
|
335
|
+
These indices index into the flattened array.
|
|
336
|
+
|
|
337
|
+
Returns:
|
|
338
|
+
A new 1D array containing the elements at the specified indices.
|
|
339
|
+
"""
|
|
340
|
+
...
|
|
341
|
+
|
|
342
|
+
def matmul(self, other: Array) -> Array:
|
|
343
|
+
"""Matrix multiplication."""
|
|
344
|
+
...
|
|
345
|
+
|
|
346
|
+
def dot(self, other: Array) -> Array:
|
|
347
|
+
"""Dot/matrix product."""
|
|
348
|
+
...
|
|
349
|
+
|
|
350
|
+
def __matmul__(self, other: Array) -> Array:
|
|
351
|
+
"""Matrix multiplication operator (@)."""
|
|
352
|
+
...
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
def sin(self) -> Array: ...
|
|
356
|
+
def cos(self) -> Array: ...
|
|
357
|
+
def tan(self) -> Array: ...
|
|
358
|
+
def arcsin(self) -> Array: ...
|
|
359
|
+
def arccos(self) -> Array: ...
|
|
360
|
+
def arctan(self) -> Array: ...
|
|
361
|
+
def exp(self) -> Array: ...
|
|
362
|
+
def sqrt(self) -> Array: ...
|
|
363
|
+
def log(self) -> Array:
|
|
364
|
+
"""Natural logarithm, element-wise."""
|
|
365
|
+
...
|
|
366
|
+
def abs(self) -> Array:
|
|
367
|
+
"""Absolute value, element-wise."""
|
|
368
|
+
...
|
|
369
|
+
def sign(self) -> Array:
|
|
370
|
+
"""Returns -1, 0, or 1 for each element based on sign."""
|
|
371
|
+
...
|
|
372
|
+
def clip(self, min: float, max: float) -> Array: ...
|
|
373
|
+
|
|
374
|
+
# Statistical reductions
|
|
375
|
+
def sum(self) -> float:
|
|
376
|
+
"""Sum of all elements."""
|
|
377
|
+
...
|
|
378
|
+
def mean(self) -> float:
|
|
379
|
+
"""Mean of all elements."""
|
|
380
|
+
...
|
|
381
|
+
def var(self) -> float:
|
|
382
|
+
"""Variance of all elements (population variance)."""
|
|
383
|
+
...
|
|
384
|
+
def std(self) -> float:
|
|
385
|
+
"""Standard deviation of all elements."""
|
|
386
|
+
...
|
|
387
|
+
def median(self) -> float:
|
|
388
|
+
"""Median of all elements."""
|
|
389
|
+
...
|
|
390
|
+
@overload
|
|
391
|
+
def quantile(self, q: float) -> float:
|
|
392
|
+
"""q-th quantile of all elements (q in [0, 1])."""
|
|
393
|
+
...
|
|
394
|
+
@overload
|
|
395
|
+
def quantile(self, q: Array) -> Array:
|
|
396
|
+
"""Compute multiple quantiles at once (vectorized).
|
|
397
|
+
|
|
398
|
+
Args:
|
|
399
|
+
q: Array of quantile values, each in [0, 1].
|
|
400
|
+
|
|
401
|
+
Returns:
|
|
402
|
+
1D array of quantile values corresponding to each q.
|
|
403
|
+
"""
|
|
404
|
+
...
|
|
405
|
+
|
|
406
|
+
# Logical reductions
|
|
407
|
+
def any(self) -> bool:
|
|
408
|
+
"""True if any element is non-zero."""
|
|
409
|
+
...
|
|
410
|
+
def all(self) -> bool:
|
|
411
|
+
"""True if all elements are non-zero."""
|
|
412
|
+
...
|
|
413
|
+
|
|
414
|
+
def __len__(self) -> int: ...
|
|
415
|
+
@overload
|
|
416
|
+
def __getitem__(self, index: int) -> float | Array:
|
|
417
|
+
"""Get element (1D) or row (2D+) at index."""
|
|
418
|
+
...
|
|
419
|
+
@overload
|
|
420
|
+
def __getitem__(self, index: slice) -> Array: ...
|
|
421
|
+
@overload
|
|
422
|
+
def __getitem__(self, index: Tuple[int, ...]) -> float | Array:
|
|
423
|
+
"""Get element at (i, j, ...) or sub-array if fewer indices than dimensions."""
|
|
424
|
+
...
|
|
425
|
+
@overload
|
|
426
|
+
def __setitem__(self, index: int, value: float) -> None:
|
|
427
|
+
"""Set element at index (1D arrays only)."""
|
|
428
|
+
...
|
|
429
|
+
@overload
|
|
430
|
+
def __setitem__(self, index: Tuple[int, ...], value: float) -> None:
|
|
431
|
+
"""Set element at (i, j, ...)."""
|
|
432
|
+
...
|
|
433
|
+
def __iter__(self) -> Iterator[float]: ...
|
|
434
|
+
def __contains__(self, value: float) -> bool: ...
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
@overload
|
|
438
|
+
def __add__(self, other: Array) -> Array: ...
|
|
439
|
+
@overload
|
|
440
|
+
def __add__(self, other: float) -> Array: ...
|
|
441
|
+
def __radd__(self, other: float) -> Array: ...
|
|
442
|
+
|
|
443
|
+
@overload
|
|
444
|
+
def __sub__(self, other: Array) -> Array: ...
|
|
445
|
+
@overload
|
|
446
|
+
def __sub__(self, other: float) -> Array: ...
|
|
447
|
+
def __rsub__(self, other: float) -> Array: ...
|
|
448
|
+
|
|
449
|
+
@overload
|
|
450
|
+
def __mul__(self, other: Array) -> Array: ...
|
|
451
|
+
@overload
|
|
452
|
+
def __mul__(self, other: float) -> Array: ...
|
|
453
|
+
def __rmul__(self, other: float) -> Array: ...
|
|
454
|
+
|
|
455
|
+
@overload
|
|
456
|
+
def __truediv__(self, other: Array) -> Array: ...
|
|
457
|
+
@overload
|
|
458
|
+
def __truediv__(self, other: float) -> Array: ...
|
|
459
|
+
def __rtruediv__(self, other: float) -> Array: ...
|
|
460
|
+
|
|
461
|
+
def __neg__(self) -> Array: ...
|
|
462
|
+
def __repr__(self) -> str: ...
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
def zeros(shape: Sequence[int]) -> Array:
|
|
467
|
+
"""Create array filled with zeros."""
|
|
468
|
+
...
|
|
469
|
+
|
|
470
|
+
def eye(n: int, m: int | None = None, k: int | None = None) -> Array:
|
|
471
|
+
"""Create a 2D identity matrix with ones on the k-th diagonal."""
|
|
472
|
+
...
|
|
473
|
+
|
|
474
|
+
def diag(v: Sequence[float] | Array, k: int | None = None) -> Array:
|
|
475
|
+
"""Create a 2D array with v on the k-th diagonal.
|
|
476
|
+
|
|
477
|
+
Args:
|
|
478
|
+
v: 1D array or list of diagonal values.
|
|
479
|
+
k: Diagonal offset (0=main, >0=upper, <0=lower).
|
|
480
|
+
"""
|
|
481
|
+
...
|
|
482
|
+
|
|
483
|
+
def outer(a: Sequence[float] | Array, b: Sequence[float] | Array) -> Array:
|
|
484
|
+
"""Compute the outer product of two 1D arrays.
|
|
485
|
+
|
|
486
|
+
Args:
|
|
487
|
+
a: First 1D array or list.
|
|
488
|
+
b: Second 1D array or list.
|
|
489
|
+
"""
|
|
490
|
+
...
|
|
491
|
+
|
|
492
|
+
def column_stack(arrays: Sequence[Array]) -> Array:
|
|
493
|
+
"""Stack 1D or 2D arrays as columns into a 2D array.
|
|
494
|
+
|
|
495
|
+
Args:
|
|
496
|
+
arrays: Sequence of 1D or 2D arrays to stack. 1D arrays are converted
|
|
497
|
+
to columns (n, 1). All arrays must have the same number of rows.
|
|
498
|
+
|
|
499
|
+
Returns:
|
|
500
|
+
A 2D array formed by stacking the given arrays column-wise.
|
|
501
|
+
|
|
502
|
+
Raises:
|
|
503
|
+
ValueError: If arrays is empty or if arrays have mismatched row counts.
|
|
504
|
+
"""
|
|
505
|
+
...
|
|
506
|
+
|
|
507
|
+
def ones(shape: Sequence[int]) -> Array:
|
|
508
|
+
"""Create array filled with ones."""
|
|
509
|
+
...
|
|
510
|
+
|
|
511
|
+
def full(shape: Sequence[int], fill_value: float) -> Array:
|
|
512
|
+
"""Create array filled with a specified value."""
|
|
513
|
+
...
|
|
514
|
+
|
|
515
|
+
def asarray(data: Sequence[float] | Array, shape: Sequence[int] | None = None) -> Array:
|
|
516
|
+
"""Create array from data with optional reshape.
|
|
517
|
+
|
|
518
|
+
Args:
|
|
519
|
+
data: Flat list of float values or existing Array.
|
|
520
|
+
shape: Optional shape. If None, creates a 1D array.
|
|
521
|
+
"""
|
|
522
|
+
...
|
|
523
|
+
|
|
524
|
+
class spatial:
|
|
525
|
+
"""Spatial data structures and algorithms."""
|
|
526
|
+
|
|
527
|
+
class BallTree:
|
|
528
|
+
"""Ball tree for efficient nearest neighbor queries.
|
|
529
|
+
|
|
530
|
+
A ball tree recursively partitions data into nested hyperspheres (balls).
|
|
531
|
+
Each node in the tree represents a ball that contains a subset of points.
|
|
532
|
+
"""
|
|
533
|
+
|
|
534
|
+
@staticmethod
|
|
535
|
+
def from_array(
|
|
536
|
+
array: Array,
|
|
537
|
+
leaf_size: int = 20,
|
|
538
|
+
metric: Literal["euclidean", "manhattan", "chebyshev"] = "euclidean"
|
|
539
|
+
) -> "spatial.BallTree":
|
|
540
|
+
"""Construct a ball tree from a 2D array of points.
|
|
541
|
+
|
|
542
|
+
Args:
|
|
543
|
+
array: 2D array of shape (n_points, n_features) containing the data points.
|
|
544
|
+
leaf_size: Maximum number of points in a leaf node. Smaller values lead to
|
|
545
|
+
faster queries but slower construction and more memory usage.
|
|
546
|
+
Defaults to 20.
|
|
547
|
+
metric: Distance metric to use for measuring distances between points.
|
|
548
|
+
Options are:
|
|
549
|
+
- "euclidean": Standard Euclidean (L2) distance (default)
|
|
550
|
+
- "manhattan": Manhattan (L1) distance (taxicab distance)
|
|
551
|
+
- "chebyshev": Chebyshev (L∞) distance (maximum coordinate difference)
|
|
552
|
+
|
|
553
|
+
Returns:
|
|
554
|
+
A constructed BallTree instance.
|
|
555
|
+
|
|
556
|
+
Raises:
|
|
557
|
+
AssertionError: If array is not 2-dimensional.
|
|
558
|
+
ValueError: If metric is not one of the valid options.
|
|
559
|
+
"""
|
|
560
|
+
...
|
|
561
|
+
|
|
562
|
+
def query_radius(self, query: float | Sequence[float] | Array, radius: float) -> List:
|
|
563
|
+
"""Find all points within a given radius of the query point.
|
|
564
|
+
|
|
565
|
+
Args:
|
|
566
|
+
query: Query point as a scalar (for 1D data), list of coordinates, or Array.
|
|
567
|
+
radius: Search radius. All points with distance <= radius are returned.
|
|
568
|
+
|
|
569
|
+
Returns:
|
|
570
|
+
1D Array of row indices (as floats) for all points within the specified
|
|
571
|
+
radius of the query point. These indices can be used to look up the actual
|
|
572
|
+
points in the original data array.
|
|
573
|
+
"""
|
|
574
|
+
...
|
|
575
|
+
|
|
576
|
+
def query_knn(self, query: float | Sequence[float] | Array, k: int) -> List:
|
|
577
|
+
"""Find the k nearest neighbors to the query point.
|
|
578
|
+
|
|
579
|
+
Args:
|
|
580
|
+
query: Query point as a scalar (for 1D data), list of coordinates, or Array.
|
|
581
|
+
k: Number of nearest neighbors to return.
|
|
582
|
+
|
|
583
|
+
Returns:
|
|
584
|
+
1D Array of row indices (as floats) for the k nearest neighbors,
|
|
585
|
+
sorted by distance (closest first). These indices can be used to look up
|
|
586
|
+
the actual points in the original data array.
|
|
587
|
+
"""
|
|
588
|
+
...
|
|
589
|
+
|
|
590
|
+
@overload
|
|
591
|
+
def kernel_density(
|
|
592
|
+
self,
|
|
593
|
+
queries: float | Sequence[float],
|
|
594
|
+
bandwidth: float = 1.0,
|
|
595
|
+
kernel: Literal["gaussian", "epanechnikov", "uniform", "triangular"] = "gaussian"
|
|
596
|
+
) -> float:
|
|
597
|
+
"""Estimate kernel density at a single query point.
|
|
598
|
+
|
|
599
|
+
Args:
|
|
600
|
+
queries: Single query point as a scalar (for 1D data) or list of coordinates.
|
|
601
|
+
bandwidth: Bandwidth (smoothing parameter) for the kernel. Larger values
|
|
602
|
+
produce smoother estimates. Defaults to 1.0.
|
|
603
|
+
kernel: Kernel function to use for density estimation. Options are:
|
|
604
|
+
- "gaussian": Gaussian (normal) kernel (default)
|
|
605
|
+
- "epanechnikov": Epanechnikov kernel
|
|
606
|
+
- "uniform": Uniform (rectangular) kernel
|
|
607
|
+
- "triangular": Triangular kernel
|
|
608
|
+
|
|
609
|
+
Returns:
|
|
610
|
+
Density estimate at the query point (float).
|
|
611
|
+
"""
|
|
612
|
+
...
|
|
613
|
+
|
|
614
|
+
@overload
|
|
615
|
+
def kernel_density(
|
|
616
|
+
self,
|
|
617
|
+
queries: Array,
|
|
618
|
+
bandwidth: float = 1.0,
|
|
619
|
+
kernel: Literal["gaussian", "epanechnikov", "uniform", "triangular"] = "gaussian"
|
|
620
|
+
) -> List:
|
|
621
|
+
"""Estimate kernel density at multiple query points.
|
|
622
|
+
|
|
623
|
+
Args:
|
|
624
|
+
queries: 2D array of query points with shape (n_queries, n_features),
|
|
625
|
+
or 1D array representing a single point.
|
|
626
|
+
bandwidth: Bandwidth (smoothing parameter) for the kernel. Larger values
|
|
627
|
+
produce smoother estimates. Defaults to 1.0.
|
|
628
|
+
kernel: Kernel function to use for density estimation. Options are:
|
|
629
|
+
- "gaussian": Gaussian (normal) kernel (default)
|
|
630
|
+
- "epanechnikov": Epanechnikov kernel
|
|
631
|
+
- "uniform": Uniform (rectangular) kernel
|
|
632
|
+
- "triangular": Triangular kernel
|
|
633
|
+
|
|
634
|
+
Returns:
|
|
635
|
+
1D Array of density estimates, one for each query point.
|
|
636
|
+
"""
|
|
637
|
+
...
|
|
638
|
+
|
|
639
|
+
@overload
|
|
640
|
+
def kernel_density(
|
|
641
|
+
self,
|
|
642
|
+
queries: None = None,
|
|
643
|
+
bandwidth: float = 1.0,
|
|
644
|
+
kernel: Literal["gaussian", "epanechnikov", "uniform", "triangular"] = "gaussian"
|
|
645
|
+
) -> List:
|
|
646
|
+
"""Estimate kernel density at all training points (leave-one-out).
|
|
647
|
+
|
|
648
|
+
Args:
|
|
649
|
+
queries: If None, computes density at each training point.
|
|
650
|
+
bandwidth: Bandwidth (smoothing parameter) for the kernel. Larger values
|
|
651
|
+
produce smoother estimates. Defaults to 1.0.
|
|
652
|
+
kernel: Kernel function to use for density estimation. Options are:
|
|
653
|
+
- "gaussian": Gaussian (normal) kernel (default)
|
|
654
|
+
- "epanechnikov": Epanechnikov kernel
|
|
655
|
+
- "uniform": Uniform (rectangular) kernel
|
|
656
|
+
- "triangular": Triangular kernel
|
|
657
|
+
|
|
658
|
+
Returns:
|
|
659
|
+
1D Array of density estimates at each training point.
|
|
660
|
+
"""
|
|
661
|
+
...
|
|
662
|
+
|
|
663
|
+
class KDTree:
|
|
664
|
+
"""KD-tree for efficient nearest neighbor queries.
|
|
665
|
+
|
|
666
|
+
A KD-tree (k-dimensional tree) recursively partitions data by splitting along
|
|
667
|
+
coordinate axes. Each node represents a hyperrectangular region and splits
|
|
668
|
+
data along the axis with the largest spread.
|
|
669
|
+
"""
|
|
670
|
+
|
|
671
|
+
@staticmethod
|
|
672
|
+
def from_array(
|
|
673
|
+
array: Array,
|
|
674
|
+
leaf_size: int = 20,
|
|
675
|
+
metric: Literal["euclidean", "manhattan", "chebyshev"] = "euclidean"
|
|
676
|
+
) -> "spatial.KDTree":
|
|
677
|
+
"""Construct a KD-tree from a 2D array of points.
|
|
678
|
+
|
|
679
|
+
Args:
|
|
680
|
+
array: 2D array of shape (n_points, n_features) containing the data points.
|
|
681
|
+
leaf_size: Maximum number of points in a leaf node. Smaller values lead to
|
|
682
|
+
faster queries but slower construction and more memory usage.
|
|
683
|
+
Defaults to 20.
|
|
684
|
+
metric: Distance metric to use for measuring distances between points.
|
|
685
|
+
Options are:
|
|
686
|
+
- "euclidean": Standard Euclidean (L2) distance (default)
|
|
687
|
+
- "manhattan": Manhattan (L1) distance (taxicab distance)
|
|
688
|
+
- "chebyshev": Chebyshev (L∞) distance (maximum coordinate difference)
|
|
689
|
+
|
|
690
|
+
Returns:
|
|
691
|
+
A constructed KDTree instance.
|
|
692
|
+
|
|
693
|
+
Raises:
|
|
694
|
+
AssertionError: If array is not 2-dimensional.
|
|
695
|
+
ValueError: If metric is not one of the valid options.
|
|
696
|
+
"""
|
|
697
|
+
...
|
|
698
|
+
|
|
699
|
+
def query_radius(self, query: float | Sequence[float] | Array, radius: float) -> List:
|
|
700
|
+
"""Find all points within a given radius of the query point.
|
|
701
|
+
|
|
702
|
+
Args:
|
|
703
|
+
query: Query point as a scalar (for 1D data), list of coordinates, or Array.
|
|
704
|
+
radius: Search radius. All points with distance <= radius are returned.
|
|
705
|
+
|
|
706
|
+
Returns:
|
|
707
|
+
1D Array of row indices (as floats) for all points within the specified
|
|
708
|
+
radius of the query point. These indices can be used to look up the actual
|
|
709
|
+
points in the original data array.
|
|
710
|
+
"""
|
|
711
|
+
...
|
|
712
|
+
|
|
713
|
+
def query_knn(self, query: float | Sequence[float] | Array, k: int) -> List:
|
|
714
|
+
"""Find the k nearest neighbors to the query point.
|
|
715
|
+
|
|
716
|
+
Args:
|
|
717
|
+
query: Query point as a scalar (for 1D data), list of coordinates, or Array.
|
|
718
|
+
k: Number of nearest neighbors to return.
|
|
719
|
+
|
|
720
|
+
Returns:
|
|
721
|
+
1D Array of row indices (as floats) for the k nearest neighbors,
|
|
722
|
+
sorted by distance (closest first). These indices can be used to look up
|
|
723
|
+
the actual points in the original data array.
|
|
724
|
+
"""
|
|
725
|
+
...
|
|
726
|
+
|
|
727
|
+
@overload
|
|
728
|
+
def kernel_density(
|
|
729
|
+
self,
|
|
730
|
+
queries: float | Sequence[float],
|
|
731
|
+
bandwidth: float = 1.0,
|
|
732
|
+
kernel: Literal["gaussian", "epanechnikov", "uniform", "triangular"] = "gaussian"
|
|
733
|
+
) -> float:
|
|
734
|
+
"""Estimate kernel density at a single query point.
|
|
735
|
+
|
|
736
|
+
Args:
|
|
737
|
+
queries: Single query point as a scalar (for 1D data) or list of coordinates.
|
|
738
|
+
bandwidth: Bandwidth (smoothing parameter) for the kernel. Larger values
|
|
739
|
+
produce smoother estimates. Defaults to 1.0.
|
|
740
|
+
kernel: Kernel function to use for density estimation. Options are:
|
|
741
|
+
- "gaussian": Gaussian (normal) kernel (default)
|
|
742
|
+
- "epanechnikov": Epanechnikov kernel
|
|
743
|
+
- "uniform": Uniform (rectangular) kernel
|
|
744
|
+
- "triangular": Triangular kernel
|
|
745
|
+
|
|
746
|
+
Returns:
|
|
747
|
+
Density estimate at the query point (float).
|
|
748
|
+
"""
|
|
749
|
+
...
|
|
750
|
+
|
|
751
|
+
@overload
|
|
752
|
+
def kernel_density(
|
|
753
|
+
self,
|
|
754
|
+
queries: Array,
|
|
755
|
+
bandwidth: float = 1.0,
|
|
756
|
+
kernel: Literal["gaussian", "epanechnikov", "uniform", "triangular"] = "gaussian"
|
|
757
|
+
) -> List:
|
|
758
|
+
"""Estimate kernel density at multiple query points.
|
|
759
|
+
|
|
760
|
+
Args:
|
|
761
|
+
queries: 2D array of query points with shape (n_queries, n_features),
|
|
762
|
+
or 1D array representing a single point.
|
|
763
|
+
bandwidth: Bandwidth (smoothing parameter) for the kernel. Larger values
|
|
764
|
+
produce smoother estimates. Defaults to 1.0.
|
|
765
|
+
kernel: Kernel function to use for density estimation. Options are:
|
|
766
|
+
- "gaussian": Gaussian (normal) kernel (default)
|
|
767
|
+
- "epanechnikov": Epanechnikov kernel
|
|
768
|
+
- "uniform": Uniform (rectangular) kernel
|
|
769
|
+
- "triangular": Triangular kernel
|
|
770
|
+
|
|
771
|
+
Returns:
|
|
772
|
+
1D Array of density estimates, one for each query point.
|
|
773
|
+
"""
|
|
774
|
+
...
|
|
775
|
+
|
|
776
|
+
@overload
|
|
777
|
+
def kernel_density(
|
|
778
|
+
self,
|
|
779
|
+
queries: None = None,
|
|
780
|
+
bandwidth: float = 1.0,
|
|
781
|
+
kernel: Literal["gaussian", "epanechnikov", "uniform", "triangular"] = "gaussian"
|
|
782
|
+
) -> List:
|
|
783
|
+
"""Estimate kernel density at all training points (leave-one-out).
|
|
784
|
+
|
|
785
|
+
Args:
|
|
786
|
+
queries: If None, computes density at each training point.
|
|
787
|
+
bandwidth: Bandwidth (smoothing parameter) for the kernel. Larger values
|
|
788
|
+
produce smoother estimates. Defaults to 1.0.
|
|
789
|
+
kernel: Kernel function to use for density estimation. Options are:
|
|
790
|
+
- "gaussian": Gaussian (normal) kernel (default)
|
|
791
|
+
- "epanechnikov": Epanechnikov kernel
|
|
792
|
+
- "uniform": Uniform (rectangular) kernel
|
|
793
|
+
- "triangular": Triangular kernel
|
|
794
|
+
|
|
795
|
+
Returns:
|
|
796
|
+
1D Array of density estimates at each training point.
|
|
797
|
+
"""
|
|
798
|
+
...
|
|
799
|
+
|
|
800
|
+
class VPTree:
|
|
801
|
+
"""Vantage-point tree for efficient nearest neighbor queries.
|
|
802
|
+
|
|
803
|
+
A vantage-point tree recursively partitions data by selecting vantage points
|
|
804
|
+
and partitioning based on distances to those points. Each node selects a point
|
|
805
|
+
as a vantage point and splits remaining points by their median distance to it.
|
|
806
|
+
This structure can be more efficient than KD-trees for high-dimensional data.
|
|
807
|
+
"""
|
|
808
|
+
|
|
809
|
+
@staticmethod
|
|
810
|
+
def from_array(
|
|
811
|
+
array: Array,
|
|
812
|
+
leaf_size: int = 20,
|
|
813
|
+
metric: Literal["euclidean", "manhattan", "chebyshev"] = "euclidean",
|
|
814
|
+
selection: Literal["first", "random"] = "first"
|
|
815
|
+
) -> "spatial.VPTree":
|
|
816
|
+
"""Construct a vantage-point tree from a 2D array of points.
|
|
817
|
+
|
|
818
|
+
Args:
|
|
819
|
+
array: 2D array of shape (n_points, n_features) containing the data points.
|
|
820
|
+
leaf_size: Maximum number of points in a leaf node. Smaller values lead to
|
|
821
|
+
faster queries but slower construction and more memory usage.
|
|
822
|
+
Defaults to 20.
|
|
823
|
+
metric: Distance metric to use for measuring distances between points.
|
|
824
|
+
Options are:
|
|
825
|
+
- "euclidean": Standard Euclidean (L2) distance (default)
|
|
826
|
+
- "manhattan": Manhattan (L1) distance (taxicab distance)
|
|
827
|
+
- "chebyshev": Chebyshev (L∞) distance (maximum coordinate difference)
|
|
828
|
+
selection: Method for selecting vantage points during tree construction.
|
|
829
|
+
Options are:
|
|
830
|
+
- "first": Always select the first point in the partition (default)
|
|
831
|
+
- "random": Randomly select a point from the partition
|
|
832
|
+
|
|
833
|
+
Returns:
|
|
834
|
+
A constructed VPTree instance.
|
|
835
|
+
|
|
836
|
+
Raises:
|
|
837
|
+
AssertionError: If array is not 2-dimensional.
|
|
838
|
+
ValueError: If metric or selection is not one of the valid options.
|
|
839
|
+
"""
|
|
840
|
+
...
|
|
841
|
+
|
|
842
|
+
def query_radius(self, query: float | Sequence[float] | Array, radius: float) -> Array:
|
|
843
|
+
"""Find all points within a given radius of the query point.
|
|
844
|
+
|
|
845
|
+
Args:
|
|
846
|
+
query: Query point as a scalar (for 1D data), list of coordinates, or Array.
|
|
847
|
+
radius: Search radius. All points with distance <= radius are returned.
|
|
848
|
+
|
|
849
|
+
Returns:
|
|
850
|
+
1D Array of row indices (as floats) for all points within the specified
|
|
851
|
+
radius of the query point. These indices can be used to look up the actual
|
|
852
|
+
points in the original data array.
|
|
853
|
+
"""
|
|
854
|
+
...
|
|
855
|
+
|
|
856
|
+
def query_knn(self, query: float | Sequence[float] | Array, k: int) -> Array:
|
|
857
|
+
"""Find the k nearest neighbors to the query point.
|
|
858
|
+
|
|
859
|
+
Args:
|
|
860
|
+
query: Query point as a scalar (for 1D data), list of coordinates, or Array.
|
|
861
|
+
k: Number of nearest neighbors to return.
|
|
862
|
+
|
|
863
|
+
Returns:
|
|
864
|
+
1D Array of row indices (as floats) for the k nearest neighbors,
|
|
865
|
+
sorted by distance (closest first). These indices can be used to look up
|
|
866
|
+
the actual points in the original data array.
|
|
867
|
+
"""
|
|
868
|
+
...
|
|
869
|
+
|
|
870
|
+
@overload
|
|
871
|
+
def kernel_density(
|
|
872
|
+
self,
|
|
873
|
+
queries: float | Sequence[float],
|
|
874
|
+
bandwidth: float = 1.0,
|
|
875
|
+
kernel: Literal["gaussian", "epanechnikov", "uniform", "triangular"] = "gaussian"
|
|
876
|
+
) -> float:
|
|
877
|
+
"""Estimate kernel density at a single query point.
|
|
878
|
+
|
|
879
|
+
Args:
|
|
880
|
+
queries: Single query point as a scalar (for 1D data) or list of coordinates.
|
|
881
|
+
bandwidth: Bandwidth (smoothing parameter) for the kernel. Larger values
|
|
882
|
+
produce smoother estimates. Defaults to 1.0.
|
|
883
|
+
kernel: Kernel function to use for density estimation. Options are:
|
|
884
|
+
- "gaussian": Gaussian (normal) kernel (default)
|
|
885
|
+
- "epanechnikov": Epanechnikov kernel
|
|
886
|
+
- "uniform": Uniform (rectangular) kernel
|
|
887
|
+
- "triangular": Triangular kernel
|
|
888
|
+
|
|
889
|
+
Returns:
|
|
890
|
+
Density estimate at the query point (float).
|
|
891
|
+
"""
|
|
892
|
+
...
|
|
893
|
+
|
|
894
|
+
@overload
|
|
895
|
+
def kernel_density(
|
|
896
|
+
self,
|
|
897
|
+
queries: Array,
|
|
898
|
+
bandwidth: float = 1.0,
|
|
899
|
+
kernel: Literal["gaussian", "epanechnikov", "uniform", "triangular"] = "gaussian"
|
|
900
|
+
) -> List:
|
|
901
|
+
"""Estimate kernel density at multiple query points.
|
|
902
|
+
|
|
903
|
+
Args:
|
|
904
|
+
queries: 2D array of query points with shape (n_queries, n_features),
|
|
905
|
+
or 1D array representing a single point.
|
|
906
|
+
bandwidth: Bandwidth (smoothing parameter) for the kernel. Larger values
|
|
907
|
+
produce smoother estimates. Defaults to 1.0.
|
|
908
|
+
kernel: Kernel function to use for density estimation. Options are:
|
|
909
|
+
- "gaussian": Gaussian (normal) kernel (default)
|
|
910
|
+
- "epanechnikov": Epanechnikov kernel
|
|
911
|
+
- "uniform": Uniform (rectangular) kernel
|
|
912
|
+
- "triangular": Triangular kernel
|
|
913
|
+
|
|
914
|
+
Returns:
|
|
915
|
+
1D Array of density estimates, one for each query point.
|
|
916
|
+
"""
|
|
917
|
+
...
|
|
918
|
+
|
|
919
|
+
@overload
|
|
920
|
+
def kernel_density(
|
|
921
|
+
self,
|
|
922
|
+
queries: None = None,
|
|
923
|
+
bandwidth: float = 1.0,
|
|
924
|
+
kernel: Literal["gaussian", "epanechnikov", "uniform", "triangular"] = "gaussian"
|
|
925
|
+
) -> List:
|
|
926
|
+
"""Estimate kernel density at all training points (leave-one-out).
|
|
927
|
+
|
|
928
|
+
Args:
|
|
929
|
+
queries: If None, computes density at each training point.
|
|
930
|
+
bandwidth: Bandwidth (smoothing parameter) for the kernel. Larger values
|
|
931
|
+
produce smoother estimates. Defaults to 1.0.
|
|
932
|
+
kernel: Kernel function to use for density estimation. Options are:
|
|
933
|
+
- "gaussian": Gaussian (normal) kernel (default)
|
|
934
|
+
- "epanechnikov": Epanechnikov kernel
|
|
935
|
+
- "uniform": Uniform (rectangular) kernel
|
|
936
|
+
- "triangular": Triangular kernel
|
|
937
|
+
|
|
938
|
+
Returns:
|
|
939
|
+
1D Array of density estimates at each training point.
|
|
940
|
+
"""
|
|
941
|
+
...
|