Introduction
Import Numpy |
import numpy as np |
Version |
np.version |
Version number |
np.version.version, np.__version__ |
Configuration |
np.show_config() |
Info |
np.info(np.add) |
Size |
a.size |
Itemsize |
a.itemsize |
1D |
a = np.array( [1, 2, 3] ) |
2D |
a = np.array( [ [1], [2] ] ) |
3D |
a = np.array( [ [ [1], [2] ], [ [3], [4] ] ] ) |
Empty |
a = np.empty(10) |
Zero & Int |
a = np.zeros(10, int) |
One & Str |
a = np.ones([4, 2], str) |
Range |
a = np.arange(10) |
Linspace |
a = np.linspace(0, 10, num=5) |
Concatenate |
c = np.concatenate((a, b), axis=0) |
Sort & Reverse |
b = np.sort(a)[::-1] |
Reshape |
b = a.reshape(3,2) |
Filters |
a[a < 5], a[b], a[a % 2 ==0], a[1:-1] |
Operations |
a + a, a.sum(), a.min(), a.max() |
Dot |
np.dot(a, b) |
Random 0 |
rng = np.random.default_rng() |
Random 1 |
a = rng.integers(10, size=(3, 4) |
Random 2 |
b = np.random.randint(10, size=10) |
Random 3 |
c = np.random.rand() |
Random 4 |
d = np.random.normal(13, 1, 15) |
Resize |
a.resize(filas, columnas, refcheck=false) |
NaN & Is NaN & Infinity |
np.nan, np.isnan, np.inf |
Is +Infinity & Is -Infinity |
np.isposinf(a), np.isneginf(a) |
Is Complex |
np.iscomplex(a) |
Is Real |
np.isreal(a) |
Is Scalar |
np.isscalar(13.13) |
Tolerance |
np.allclose(a, b) |
Greater & Greater equal |
np.greater(a, b), np.greater_equal(a, b) |
Less & Less equal |
np.less(a, b), np.less_equal(a, b) |
Full |
np.full(10, 5) |
Identity |
np.identity(3) |
|
|
Introduction
Astype |
a.astype(float) |
Changing values |
a[1:-1, 1:-1] = 0 |
Changing values 1 |
a[::2, ::2] = 1 |
Pad |
np.pad(a, pad_width=1, mode='constant', constant_values=0) |
|