PythonSTB commited on
Commit
93d2194
·
verified ·
1 Parent(s): 0ae246f

Upload numpy/NUMPY_USER_GUIDE.txt with huggingface_hub

Browse files
Files changed (1) hide show
  1. numpy/NUMPY_USER_GUIDE.txt +534 -0
numpy/NUMPY_USER_GUIDE.txt ADDED
@@ -0,0 +1,534 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ================================================================================
2
+ NUMPY - USER GUIDE (Android Python STB)
3
+ Generated by RIMI
4
+ ================================================================================
5
+ Covers: what numpy is, install/verify, arrays, dtypes, math, indexing,
6
+ reshaping, linear algebra, random, file I/O, numpy + Pillow,
7
+ printing in the terminal, and common pitfalls.
8
+
9
+ Written for: Python 3.12.2 (RIMI build) on Android
10
+ Version: numpy 2.5.2
11
+ Scripts dir: /storage/emulated/0/PythonSTB/Scripts/
12
+ Installed: /data/user/0/com.pythonstb.rimi/files/python/lib/python3.12/site-packages/
13
+ ================================================================================
14
+
15
+
16
+ --------------------------------------------------------------------------------
17
+ 1) WHAT IS NUMPY?
18
+ --------------------------------------------------------------------------------
19
+
20
+ NumPy is the fundamental library for fast numerical computing in Python.
21
+ It adds:
22
+ - n-dimensional arrays (ndarray) - faster and more compact than Python lists
23
+ - element-wise math without writing loops
24
+ - linear algebra (matrix multiply, inverse, solve, eig, ...)
25
+ - random numbers, FFT, sorting, statistics
26
+ - a bridge to C/Python extensions (OpenCV, Pillow, pandas, scipy, ...)
27
+
28
+ Typical speedup vs plain Python loops: 10x - 100x or more.
29
+
30
+ This Android build of numpy is compiled with OpenBLAS, so matrix operations
31
+ are optimized (BLAS/LAPACK) on both arm64 and x86_64.
32
+
33
+ import numpy as np
34
+ print(np.__version__) # 2.5.2
35
+ print(np.show_config()) # shows BLAS/LAPACK backend + build info
36
+
37
+
38
+ --------------------------------------------------------------------------------
39
+ 2) INSTALL / VERIFY
40
+ --------------------------------------------------------------------------------
41
+
42
+ Install (already done, but if you ever reinstall):
43
+ pip install numpy # or install the wheel file directly
44
+
45
+ Quick smoke test - run in your app:
46
+ import numpy as np
47
+ a = np.arange(12).reshape(3, 4)
48
+ print(a)
49
+ print("sum:", a.sum(), "max:", a.max(), "shape:", a.shape, "dtype:", a.dtype)
50
+ print("blas:", np.__config__.show("build") if hasattr(np.__config__, "show") else "n/a")
51
+
52
+ Expected output pattern:
53
+ [[ 0 1 2 3]
54
+ [ 4 5 6 7]
55
+ [ 8 9 10 11]]
56
+ sum: 66 max: 11 shape: (3, 4) dtype: int64
57
+
58
+
59
+ --------------------------------------------------------------------------------
60
+ 3) ARRAYS - CREATION BASICS
61
+ --------------------------------------------------------------------------------
62
+
63
+ import numpy as np
64
+
65
+ # from a list
66
+ a = np.array([1, 2, 3]) # 1-D, dtype int64
67
+ b = np.array([[1, 2, 3], [4, 5, 6]]) # 2-D shape (2, 3)
68
+ c = np.array([1.0, 2.0, 3.0]) # float64
69
+
70
+ # zeros / ones / full / identity
71
+ np.zeros((3, 4))
72
+ np.ones((2, 2))
73
+ np.full((2, 3), 7.5)
74
+ np.eye(4) # 4x4 identity
75
+ np.identity(3)
76
+
77
+ # ranges
78
+ np.arange(10) # 0..9
79
+ np.arange(0, 1, 0.1) # 0.0, 0.1, ... 0.9
80
+ np.linspace(0, 1, 5) # 5 evenly spaced points 0..1
81
+ np.logspace(1, 3, 3) # 10, 100, 1000
82
+
83
+ # random
84
+ rng = np.random.default_rng(seed=42) # reproducible
85
+ rng.random((3, 3)) # uniform [0,1)
86
+ rng.integers(0, 10, size=(2, 5)) # integers 0..9
87
+ rng.normal(0, 1, size=(4,)) # normal dist
88
+ rng.permutation(10) # shuffled 0..9
89
+
90
+ # important attributes
91
+ a = np.array([[1, 2, 3], [4, 5, 6]])
92
+ a.shape # (2, 3)
93
+ a.ndim # 2
94
+ a.size # 6
95
+ a.dtype # dtype('int64')
96
+ a.itemsize # bytes per element
97
+ a.nbytes # total bytes
98
+
99
+
100
+ --------------------------------------------------------------------------------
101
+ 4) DTYPES (data types)
102
+ --------------------------------------------------------------------------------
103
+
104
+ np.int8 np.int16 np.int32 np.int64 # signed integers
105
+ np.uint8 np.uint16 np.uint32 np.uint64 # unsigned integers
106
+ np.float32 np.float64 # floats (f32 is half memory)
107
+ np.complex64 np.complex128 # complex
108
+ np.bool_ # boolean
109
+ np.str_ np.bytes_ # strings (avoid for math)
110
+ 'f4','f8','i1','i2','i4','i8','u1','u2','u4','u8' # short aliases
111
+
112
+ a = np.array([1, 2, 3], dtype=np.uint8)
113
+ b = a.astype(np.float32) # convert
114
+ c = a.astype('f4')
115
+
116
+ Rules of thumb:
117
+ - use np.float64 (default) for general math
118
+ - use np.float32 or np.uint8 for big arrays / images (half the memory)
119
+ - beware overflow: np.array([200], np.uint8) + 100 wraps to 44
120
+ - beware int division: np.array([5]) // 2 == 2 (floor), np.array([5]) / 2 == 2.5
121
+
122
+
123
+ --------------------------------------------------------------------------------
124
+ 5) INDEXING AND SLICING
125
+ --------------------------------------------------------------------------------
126
+
127
+ a = np.arange(12).reshape(3, 4)
128
+ # [[ 0 1 2 3]
129
+ # [ 4 5 6 7]
130
+ # [ 8 9 10 11]]
131
+
132
+ a[0] # row 0: [0 1 2 3]
133
+ a[0, 2] # scalar 2
134
+ a[:, 1] # column 1: [1 5 9]
135
+ a[1:, :2] # rows 1..2, cols 0..1
136
+ a[-1] # last row
137
+ a[::2] # every other row
138
+
139
+ # boolean masking
140
+ a[a > 5] # 1-D array of values > 5
141
+ a[(a > 2) & (a < 8)] # combine masks with & |
142
+ a[a % 2 == 0] # even values
143
+
144
+ # fancy indexing with arrays
145
+ a[[0, 2]] # rows 0 and 2
146
+ idx = np.array([3, 1])
147
+ a[:, idx] # columns 3 and 1
148
+
149
+ # assignment with masks
150
+ a[a < 5] = 0 # zero out everything below 5
151
+ a[:, 0] = -1 # set first column
152
+
153
+
154
+ --------------------------------------------------------------------------------
155
+ 6) SHAPES - RESHAPE / FLATTEN / TRANSPOSE / BROADCAST
156
+ --------------------------------------------------------------------------------
157
+
158
+ a = np.arange(24)
159
+
160
+ a.reshape(4, 6) # same data, new shape
161
+ a.reshape(2, 3, 4) # 3-D
162
+ a.reshape(-1, 6) # -1 = auto: (4, 6)
163
+ a.ravel() # flatten to 1-D (may copy)
164
+ a.flatten() # always a copy, 1-D
165
+ a.T # transpose
166
+ a.reshape(4, 6).T.shape # (6, 4)
167
+
168
+ # add a new axis
169
+ v = np.array([1, 2, 3])
170
+ v[np.newaxis, :].shape # (1, 3)
171
+ v[:, np.newaxis].shape # (3, 1)
172
+
173
+ # BROADCASTING: shapes line up from the right
174
+ m = np.ones((3, 4))
175
+ m + 1 # scalar broadcast
176
+ m * np.array([10, 20, 30, 40]) # row vector broadcast over rows
177
+ m + np.array([[1], [2], [3]]) # column vector broadcast over cols
178
+ # rule: dimensions must be equal or one of them must be 1
179
+
180
+
181
+ --------------------------------------------------------------------------------
182
+ 7) MATH - ELEMENT-WISE AND REDUCTIONS
183
+ --------------------------------------------------------------------------------
184
+
185
+ a = np.array([1., 2., 3., 4.])
186
+
187
+ a + 1, a - 1, a * 2, a / 2, a ** 2, -a # element-wise
188
+ np.sqrt(a), np.abs(a), np.exp(a), np.log(a)
189
+ np.sin(a), np.cos(a), np.tan(a), np.arctan(a)
190
+ np.round(a), np.floor(a), np.ceil(a), np.clip(a, 1.5, 3.5)
191
+ np.sign(a), np.mod(a, 2), np.power(a, 3)
192
+ np.maximum(a, 2), np.minimum(a, 3)
193
+
194
+ # reductions (default: over all elements)
195
+ a.sum() a.mean() a.min() a.max() a.std() a.var()
196
+ a.prod() a.argmax() a.argmin() a.cumsum() a.cumprod()
197
+ np.median(a) np.percentile(a, 50) np.ptp(a) # peak-to-peak
198
+
199
+ # along an axis
200
+ m = np.arange(6).reshape(2, 3)
201
+ m.sum(axis=0) # per column: [3 5 7]
202
+ m.sum(axis=1) # per row: [3 12]
203
+ m.max(axis=0), m.min(axis=1)
204
+
205
+ # comparisons return boolean arrays
206
+ (a > 2) # array([False, False, True, True])
207
+ np.any(a > 2) # True
208
+ np.all(a > 2) # False
209
+ np.count_nonzero(a > 2) # 2
210
+
211
+
212
+ --------------------------------------------------------------------------------
213
+ 8) LINEAR ALGEBRA (OpenBLAS accelerated)
214
+ --------------------------------------------------------------------------------
215
+
216
+ a = np.array([[1., 2.], [3., 4.]])
217
+ b = np.array([[5., 6.], [7., 8.]])
218
+
219
+ a @ b # matrix multiply (preferred)
220
+ np.matmul(a, b) # same
221
+ a.dot(b) # same (older style)
222
+ a * b # ELEMENT-WISE, NOT matrix multiply
223
+
224
+ np.linalg.inv(a) # inverse
225
+ np.linalg.det(a) # determinant
226
+ np.linalg.solve(a, np.array([1., 2.])) # solve a x = b
227
+ np.linalg.eig(a) # eigenvalues + eigenvectors
228
+ np.linalg.norm(a) # Frobenius norm
229
+ np.linalg.qr(a), np.linalg.svd(a)
230
+ np.linalg.pinv(a) # pseudo-inverse
231
+ np.linalg.matrix_power(a, 3)
232
+
233
+ # vector ops
234
+ v = np.array([1., 2., 3.])
235
+ w = np.array([4., 5., 6.])
236
+ np.dot(v, w) # dot product 32.0
237
+ np.cross(v, w) # cross product
238
+ np.inner(v, w) # inner product
239
+
240
+ # useful on the device: transform a 3D point / homography
241
+ M = np.array([[1., 0., 10.], [0., 1., 20.], [0., 0., 1.]])
242
+ p = np.array([5., 6., 1.])
243
+ out = M @ p
244
+
245
+
246
+ --------------------------------------------------------------------------------
247
+ 9) STACKING, SPLITTING, CONCATENATING
248
+ --------------------------------------------------------------------------------
249
+
250
+ a = np.array([1, 2, 3])
251
+ b = np.array([4, 5, 6])
252
+
253
+ np.concatenate((a, b)) # [1 2 3 4 5 6]
254
+ np.stack((a, b)) # shape (2, 3)
255
+ np.vstack((a, b)) # vertical: shape (2, 3)
256
+ np.hstack((a, b)) # horizontal: [1 2 3 4 5 6]
257
+ np.dstack((a, b)) # depth: shape (1, 3, 2)
258
+
259
+ m1 = np.ones((2, 2))
260
+ m2 = np.zeros((2, 2))
261
+ np.vstack((m1, m2)) # (4, 2)
262
+ np.hstack((m1, m2)) # (2, 4)
263
+
264
+ # split
265
+ x = np.arange(10)
266
+ np.split(x, 2) # two arrays of 5
267
+ np.array_split(x, 3) # uneven split
268
+ np.hsplit(m1, 2), np.vsplit(m1, 2)
269
+
270
+
271
+ --------------------------------------------------------------------------------
272
+ 10) RANDOM NUMBERS
273
+ --------------------------------------------------------------------------------
274
+
275
+ rng = np.random.default_rng(2026) # always seed for reproducibility
276
+ rng.random((2, 3)) # [0,1) floats
277
+ rng.integers(1, 7, size=10) # die rolls 1..6
278
+ rng.normal(loc=0, scale=1, size=(3, 3))
279
+ rng.uniform(0, 10, size=5)
280
+ rng.choice(np.arange(5), size=10, replace=True)
281
+ rng.shuffle(np.arange(10)) # in place
282
+ rng.standard_normal((4,))
283
+
284
+ # old style (np.random.rand etc.) also works, but default_rng is preferred.
285
+
286
+
287
+ --------------------------------------------------------------------------------
288
+ 11) SAVE / LOAD DATA (files)
289
+ --------------------------------------------------------------------------------
290
+
291
+ a = np.arange(12).reshape(3, 4)
292
+
293
+ # numpy binary format (fast, compact, one array per file)
294
+ np.save("/storage/emulated/0/Download/a.npy", a)
295
+ b = np.load("/storage/emulated/0/Download/a.npy")
296
+
297
+ # compressed multi-array archive
298
+ np.savez("/storage/emulated/0/Download/data.npz", x=a, y=a * 2)
299
+ d = np.load("/storage/emulated/0/Download/data.npz")
300
+ d["x"], d["y"]
301
+ # or np.savez_compressed(...) for smaller files
302
+
303
+ # text (human readable)
304
+ np.savetxt("/storage/emulated/0/Download/a.csv", a, delimiter=",")
305
+ c = np.loadtxt("/storage/emulated/0/Download/a.csv", delimiter=",")
306
+ # note: loadtxt returns float64; use dtype= to control
307
+ np.savetxt("/storage/emulated/0/Download/a.tsv", a, delimiter="\t",
308
+ fmt="%.2f")
309
+
310
+ # plain binary (raw, no header)
311
+ a.tofile("/storage/emulated/0/Download/a.bin")
312
+ np.fromfile("/storage/emulated/0/Download/a.bin", dtype=np.int64)
313
+
314
+
315
+ --------------------------------------------------------------------------------
316
+ 12) NUMPY + PILLOW (images are just arrays)
317
+ --------------------------------------------------------------------------------
318
+
319
+ from PIL import Image
320
+ import numpy as np
321
+
322
+ # PIL image -> numpy array (H, W, C)
323
+ im = Image.open("/storage/emulated/0/Download/photo.jpg").convert("RGB")
324
+ arr = np.asarray(im)
325
+ print(arr.shape) # (height, width, 3)
326
+ print(arr.dtype) # uint8
327
+
328
+ # numpy array -> PIL image
329
+ img2 = Image.fromarray(arr)
330
+ img2.save("/storage/emulated/0/Download/out.jpg", quality=95)
331
+
332
+ # grayscale -> (H, W)
333
+ gray = np.asarray(im.convert("L"))
334
+ # alpha -> (H, W, 4)
335
+ rgba = np.asarray(im.convert("RGBA"))
336
+
337
+ # process with numpy, then convert back
338
+ arr2 = arr[:, ::-1] # mirror
339
+ arr3 = np.clip(arr.astype(np.int16) + 50, 0, 255).astype(np.uint8) # brighten
340
+ arr4 = 255 - arr # invert
341
+ arr5 = arr.copy(); arr5[..., 0] = 255 # force red channel to max
342
+ Image.fromarray(arr2).save("/storage/emulated/0/Download/mirror.png")
343
+ Image.fromarray(arr4).save("/storage/emulated/0/Download/invert.png")
344
+
345
+ # crop = slice
346
+ crop = arr[100:200, 50:150]
347
+ Image.fromarray(crop).save("/storage/emulated/0/Download/crop.png")
348
+
349
+ # resize with numpy (nearest) - better to use PIL resize normally
350
+ small = arr[::4, ::4] # nearest-neighbour downsample
351
+
352
+ # build a gradient image
353
+ h, w = 200, 200
354
+ yy, xx = np.mgrid[0:h, 0:w]
355
+ grad = np.stack([
356
+ (xx * 255 // max(1, w - 1)).astype(np.uint8),
357
+ (yy * 255 // max(1, h - 1)).astype(np.uint8),
358
+ ((xx + yy) * 255 // max(1, (w + h) - 2)).astype(np.uint8),
359
+ ], axis=2)
360
+ Image.fromarray(grad).save("/storage/emulated/0/Download/gradient.png")
361
+
362
+ IMPORTANT:
363
+ np.asarray(im) may share memory with the PIL image. If you modify arr
364
+ in place (arr[...] = ...), the image changes too. Use arr.copy() when
365
+ you need an independent buffer.
366
+ uint8 arithmetic overflows (255+1 -> 0). Cast to int16 first for math:
367
+ arr.astype(np.int16).
368
+
369
+
370
+ --------------------------------------------------------------------------------
371
+ 13) PRINTING ARRAYS / MATRICES IN THE TERMINAL
372
+ --------------------------------------------------------------------------------
373
+
374
+ a = np.arange(12).reshape(3, 4)
375
+
376
+ print(a) # default pretty print
377
+ print(a.tolist()) # as nested Python lists
378
+
379
+ # control the output
380
+ import numpy as np
381
+ np.set_printoptions(
382
+ precision=2, # decimals for floats
383
+ threshold=20, # max elements before "..."
384
+ edgeitems=3,
385
+ linewidth=120,
386
+ suppress=True, # avoid scientific notation for small numbers
387
+ formatter={"float_kind": lambda v: f"{v:7.2f}"},
388
+ )
389
+ print(a)
390
+
391
+ # a small helper to show an array as a grid of numbers
392
+ def print_matrix(m):
393
+ m = np.asarray(m)
394
+ for row in m:
395
+ print(" ".join(f"{v:8.3f}" for v in row))
396
+
397
+ print_matrix(np.random.default_rng(1).random((3, 5)))
398
+
399
+ # print a 2D array as colored blocks (with ANSI)
400
+ def print_heatmap(m, cols=40):
401
+ m = np.asarray(m, dtype=np.float64)
402
+ if m.ndim != 2:
403
+ m = m.reshape(m.shape[0], -1)
404
+ h, w = m.shape
405
+ # resample columns to terminal width (nearest)
406
+ if w > cols:
407
+ m = m[:, ::max(1, w // cols)][:, :cols]
408
+ h, w = m.shape
409
+ lo, hi = m.min(), m.max()
410
+ rng = (hi - lo) or 1.0
411
+ out = []
412
+ for row in m:
413
+ line = ""
414
+ for v in row:
415
+ t = (v - lo) / rng # 0..1
416
+ r = int(255 * t)
417
+ g = int(255 * (1 - t))
418
+ line += f"\x1b[48;2;{r};{g};0m "
419
+ out.append(line + "\x1b[0m")
420
+ print("\n".join(out))
421
+
422
+ # example: heatmap of a sinc function
423
+ import numpy as np
424
+ x = np.linspace(-6, 6, 80)
425
+ y = np.linspace(-6, 6, 40)
426
+ yy, xx = np.meshgrid(y, x, indexing="ij")
427
+ z = np.sinc(np.sqrt(xx ** 2 + yy ** 2))
428
+ print_heatmap(z, cols=60)
429
+
430
+ # ASCII density plot from a 2D array
431
+ RAMP = " .:-=+*#%@"
432
+ def print_ascii_grid(m, cols=60):
433
+ m = np.asarray(m, dtype=np.float64)
434
+ if w := m.shape[1] > cols:
435
+ m = m[:, ::w // cols + 1]
436
+ lo, hi = m.min(), m.max()
437
+ rng = (hi - lo) or 1.0
438
+ for row in m:
439
+ print("".join(RAMP[int((v - lo) / rng * (len(RAMP) - 1))] for v in row))
440
+
441
+ print_ascii_grid(z, cols=70)
442
+
443
+
444
+ --------------------------------------------------------------------------------
445
+ 14) USEFUL EVERYDAY SNIPPETS
446
+ --------------------------------------------------------------------------------
447
+
448
+ Statistics of a numeric column:
449
+ data = np.array([1., 2., 3., 4., 100.])
450
+ print("mean %.2f std %.2f median %.2f min %.0f max %.0f" % (
451
+ data.mean(), data.std(), np.median(data), data.min(), data.max()))
452
+
453
+ Normalize to [0, 1]:
454
+ x = np.array([3., 1., 2., 0.])
455
+ n = (x - x.min()) / (x.max() - x.min())
456
+
457
+ Standard score (z-score):
458
+ z = (x - x.mean()) / x.std()
459
+
460
+ One-hot encode categories:
461
+ cats = np.array([0, 2, 1, 2, 0])
462
+ onehot = np.eye(3)[cats]
463
+
464
+ Extract the diagonal of a matrix:
465
+ np.diag(np.arange(9).reshape(3, 3)) # [0 4 8]
466
+
467
+ Histogram:
468
+ values, edges = np.histogram(rng.normal(size=1000), bins=20)
469
+
470
+ Find the index of the maximum in each row:
471
+ m = rng.random((5, 8))
472
+ m.argmax(axis=1)
473
+
474
+ Clip and cast for image math:
475
+ arr.astype(np.float32) * 1.2 + 10 -> clip -> uint8
476
+
477
+ Simple FIR smoothing:
478
+ kernel = np.ones(5) / 5
479
+ smooth = np.convolve(signal, kernel, mode="same")
480
+
481
+ Measure elapsed time:
482
+ import time
483
+ t0 = time.perf_counter()
484
+ ... work ...
485
+ print("elapsed %.3f s" % (time.perf_counter() - t0))
486
+
487
+
488
+ --------------------------------------------------------------------------------
489
+ 15) NUMPY + PANDAS / OTHER PACKAGES
490
+ --------------------------------------------------------------------------------
491
+
492
+ numpy is the foundation for many packages already on the device:
493
+
494
+ import numpy as np
495
+ import pandas as pd
496
+ df = pd.DataFrame({"a": [1, 2, 3], "b": [4.0, 5.0, 6.0]})
497
+ print(df)
498
+ arr = df.to_numpy() # DataFrame -> numpy array
499
+ # pandas is built on numpy; everything in this guide applies.
500
+
501
+ # OpenCV (if installed) also exchanges buffers directly:
502
+ # cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
503
+
504
+
505
+ --------------------------------------------------------------------------------
506
+ 16) PITFALLS & NOTES ON THIS BUILD
507
+ --------------------------------------------------------------------------------
508
+
509
+ - Version is 2.5.2. numpy 2.x changed some 1.x behaviors:
510
+ * np.array(None) no longer allowed
511
+ * np.find_common_type removed
512
+ * copy keyword defaults changed (np.array(..., copy=None) is common)
513
+ Code written for numpy 1.x may need small fixes.
514
+ - uint8 overflow: cast to a wider dtype before math on images.
515
+ - Integer division: use // for floor, / for true (float) division.
516
+ - Broadcasting: shapes must be equal or one must be 1; (3,1) * (1,4) -> (3,4).
517
+ - np.asarray may share memory with the source (PIL image); use .copy() to
518
+ detach.
519
+ - OpenBLAS is compiled in - @ and np.linalg.* are fast; no action needed.
520
+ - Wheels here are tagged cp312-cp312-linux_aarch64 / linux_x86_64
521
+ (this Android build uses the "linux" platform tag for numpy). Make sure you
522
+ install the wheel that matches the device ABI (arm64 phone vs x86_64 emulator).
523
+ - Temp files are not needed: save directly to /storage/emulated/0/Download/
524
+ or any folder the app can write.
525
+ - For very large arrays, watch memory: a float64 array of 10M elements uses
526
+ 80 MB. Use float32 or appropriate dtypes when possible.
527
+ - Reinstall safety: keep a copy of the wheel file
528
+ (numpy-2.5.2-cp312-cp312-linux_aarch64.whl or _x86_64) in
529
+ /storage/emulated/0/Download/ so you can reinstall if needed.
530
+
531
+ ================================================================================
532
+ END OF GUIDE
533
+ Generated by RIMI
534
+ ================================================================================