python:numpy
This is an old revision of the document!
Table of Contents
NumPy
Numerical Python
ndarray
Similar to pythons list object.
- All objects in an
ndarraymust have same datatype - All math operators work on each individual element in an ''ndarray'
# Properties a = np.ndarray((2,3,4)) a.shape # Tuple describing shape, e.g. (2,3,4) a.ndim # Number of dimensions, equal to len(a.shape), e.g. 3 a.size # Total number of elements a.dtype # Datatype of the elements a.itemsize # Size in byte of an element
Indexing
a = np.arange(10) # a => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] a1 = a[:6:2] # a1 => [0,2,4] a[:6:2] = 30 # a => [30, 1, 30, 3, 30, 5, 6, 7, 8, 9]
# 2d-array B = A[row_start: row_stop : (row_step) , col_start : col_stop : (col_step)]
# Using a list a = np.arange(10) # a => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # a list naming the indices l = [0, 2, 8, 9] a[l] a[[0,2,8,9]] # or a list of bool, masking desired elements. Must be same dimension as the array. bl = [False]*10 bl[3] = True a[bl] # => [3]
Datatypes
| Name | Datatype |
|---|---|
| i | integer |
| b | boolean |
| u | unsigned integer |
| f | float |
| M | datetime |
| m | timedelta |
| O | python object |
| S | zero terminated string |
| U | unicode string |
| V | raw data (void) |
Good functions
import numpy as np a1 = np.zeros((4,3,2)) # Create 3-dimensional array with zeros a2 = np.ones((3,4,5)) # Create 3-dimensional array with ones a3 = np.arange(1,10) # Array with values 1..9 a4 = np.arange(10) # Array with values 0..9 a5 = np.arange(1,20,5) # Each 5th value between 1 to 19; [1, 6, 11, 16]
python/numpy.1628106466.txt.gz · Last modified: 2022/09/12 00:30 (external edit)
