python:numpy
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| python:numpy [2021/08/04 19:39] – [ndarray] utedass | python:numpy [2022/09/12 00:30] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 19: | Line 19: | ||
| </ | </ | ||
| + | ===== Creation ===== | ||
| + | |||
| + | <code python> | ||
| + | import numpy as np | ||
| + | |||
| + | #### np.array(content) #### | ||
| + | np.array(4) | ||
| + | # array(4) | ||
| + | |||
| + | np.array((1, | ||
| + | # array([1, 2, 3, 4]) | ||
| + | |||
| + | np.array(((1, | ||
| + | # array([[1, 2, 3], | ||
| + | # [4, 5, 6]]) | ||
| + | |||
| + | #### np.ndarray(shape) #### | ||
| + | np.ndarray(4) | ||
| + | # array([1.11949605e-311, | ||
| + | |||
| + | np.ndarray((2, | ||
| + | # array([[0. | ||
| + | # [0.586, 0.77 , 0.954, 1. ]]) | ||
| + | |||
| + | </ | ||
| + | |||
| + | <code python> | ||
| + | import numpy as np | ||
| + | |||
| + | a1 = np.zeros((4, | ||
| + | |||
| + | a2 = np.ones((3, | ||
| + | |||
| + | a3 = np.arange(1, | ||
| + | a4 = np.arange(10) # Array with values 0..9 | ||
| + | a5 = np.arange(1, | ||
| + | |||
| + | |||
| + | </ | ||
| ===== Indexing ===== | ===== Indexing ===== | ||
| Line 25: | Line 64: | ||
| a1 = a[: | a1 = a[: | ||
| a[:6:2] = 30 # a => [30, 1, 30, 3, 30, 5, 6, 7, 8, 9] | a[:6:2] = 30 # a => [30, 1, 30, 3, 30, 5, 6, 7, 8, 9] | ||
| + | |||
| + | # Note that a1 holds a reference to a, changes made to a1 is reflected in a | ||
| </ | </ | ||
| + | <code python> | ||
| + | # 2d-array | ||
| + | B = A[row_start: | ||
| + | |||
| + | # Note that B holds a reference to A, changes made to B is reflected in A | ||
| + | </ | ||
| + | |||
| + | <code python> | ||
| + | # Using a list | ||
| + | a = np.arange(10) | ||
| + | |||
| + | # a list naming the indices | ||
| + | l = [0, 2, 8, 9] | ||
| + | a[l] | ||
| + | a[[0, | ||
| + | |||
| + | # or a list of bool, masking desired elements. Must be same dimension as the array. | ||
| + | bl = [False]*10 | ||
| + | bl[3] = True | ||
| + | a[bl] # => [3] | ||
| + | |||
| + | # Note that this spawns a new ndarray, changes made to the resulting array is NOT reflected in the original array | ||
| + | </ | ||
| + | |||
| + | <code python> | ||
| + | # Using comparisons to create a masking list | ||
| + | a = np.arange(10) | ||
| + | |||
| + | a[a> | ||
| + | |||
| + | # Note that this spawns a new ndarray, changes made to the resulting array is NOT reflected in the original array | ||
| + | </ | ||
| ====== Datatypes ====== | ====== Datatypes ====== | ||
| + | [[https:// | ||
| + | |||
| ^ Name ^ Datatype ^ | ^ Name ^ Datatype ^ | ||
| | i | integer | | | i | integer | | ||
| - | | b | boolean | + | | b | byte | |
| | u | unsigned integer | | | u | unsigned integer | | ||
| | f | float | | | f | float | | ||
| Line 38: | Line 113: | ||
| | S | zero terminated string | | | S | zero terminated string | | ||
| | U | unicode string | | | U | unicode string | | ||
| + | | c | complex float | | ||
| | V | raw data (void) | | | V | raw data (void) | | ||
| - | ====== | + | |
| + | |||
| + | ====== | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | <code python> | ||
| + | np.dot(A, | ||
| + | np.linalg.det(A) | ||
| + | np.transpose(A) | ||
| + | np.identity(n) | ||
| + | np.linalg.inv(A) | ||
| + | |||
| + | # Let v represent a polynomial | ||
| + | np.roots(v) | ||
| + | </ | ||
| + | |||
| + | ====== Universal | ||
| + | [[https:// | ||
| + | |||
| + | Functions in the numpy library that operates on ndarrays in an element-wise manner. | ||
| <code python> | <code python> | ||
| import numpy as np | import numpy as np | ||
| + | na = np.array([0.23, | ||
| + | np.cos(na) # Returns a new ndarray of same dimensions, with the result | ||
| + | np.log10(na) | ||
| + | # etc.. | ||
| - | a1 = np.zeros((4, | + | # Also arithmetic functions. Given nparray A and B of same size |
| + | elementwise_sum = A+B | ||
| + | elementwise_division = A/B | ||
| + | elementwise_square = A**B | ||
| - | a2 = np.ones((3, | + | elementwise_test |
| + | elementwise_test = A > B | ||
| - | a3 = np.arange(1, | ||
| - | a4 = np.arange(10) # Array with values 0..9 | ||
| - | a5 = np.arange(1, | ||
| + | </ | ||
| + | |||
| + | ====== resize, reshape, flatten, ravel ====== | ||
| + | Of these four function, only resize creates a new array whilst the rest of them returns references or modify the original array. | ||
| + | |||
| + | <code python> | ||
| + | A = np.ndarray((3, | ||
| + | B = A.flatten() | ||
| + | C = A.ravel() | ||
| + | |||
| + | |||
| + | A = np.arange(1, | ||
| + | A.resize(2, | ||
| + | |||
| + | B = A.reshape(4, | ||
| + | </ | ||
| + | |||
| + | |||
| + | ====== Concatenation ====== | ||
| + | |||
| + | <code python> | ||
| + | np.vstack((A, | ||
| + | np.hstack((A, | ||
| + | np.concatenate((A, | ||
| + | # All of these creates new arrays | ||
| </ | </ | ||
python/numpy.1628105981.txt.gz · Last modified: 2022/09/12 00:30 (external edit)
