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 20:02] – [Good functions] 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 62: | Line 101: | ||
| </ | </ | ||
| ====== 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 72: | 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) | | ||
| - | ====== Good functions ====== | ||
| - | <code python> | ||
| - | import numpy as np | ||
| - | a1 = np.zeros((4, | + | ====== Linear algebra and math ====== |
| - | a2 = np.ones((3, | + | [[https:// |
| - | + | ||
| - | a3 = np.arange(1, | + | |
| - | a4 = np.arange(10) # Array with values 0..9 | + | |
| - | a5 = np.arange(1, | + | |
| + | <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) | ||
| </ | </ | ||
| Line 112: | Line 155: | ||
| + | </ | ||
| + | |||
| + | ====== 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.1628107367.txt.gz · Last modified: 2022/09/12 00:30 (external edit)
