Numpy
Necessary Module
import numpy as np
Array Creation
np.array([1,2,3])
np.array((1,2),dtype=np.float)
np.array([[4,5,6],[7,8,9]])
arrayObj.shape
arrayObj.strides
arrayObj.flags
arrayObj.shape = (i, j)
arrayObj.reshape((i, j))
arrayObj.dtype == 'int32'
set(np.typeDict.values())
np.arange(0,10,0.1)
np.linspace(0,100,11)
np.logspace(0,2,5)
np.logspace(0,1,12,base=2,endpoint=False)
np.empty((2,3),np.int)
np.zeros((2,3),np.float)
np.ones((2,3),np.float)
np.zeros_like(arrayObj)
np.ones_like(arrayObj)
np.fromstring(arrayObj,np.init8)
np.random.rand(3)
def fun(i,j):
return(i%4+j%5)
np.fromfunction(fun,(10,5))
np.fromfunction((lambda i: i%4+1),(10,))
Array Access
arrayObj[0]
arrayObj[0:len(arrayObj):2]
arrayObj[:3:2]
arrayObj[3::2]
arrayObj[:-1:3]
arrayObj[arrayObj > 5]
arrayObj[np.array([True,False,True])]
arrayObj[3,8]
arrayObj[3:5,8::2]
arrayObj[slice(3,5),slice(8,None,2)]
arrayObj[(0,1),(1,2)]
arrayObj[0:1,[1,2]]
arrayObj[[1,2]]
Data Structure
personType = np.dtype({\
'names':['name','age','weight'], \
'formats':['S32','i','f']}, \
align=True)
persons = np.array([('a',20,50.2),('b',30,55.6)], \
dtype=personType)
persons[0]['name']
employee = np.dtype({\
'names':['person','number'], \
'formats':[personType,'i']
})
employees = np.array([(persons[0], 1)], dtype=employee)
employees[0]['person']['name']