jmquintana79
4/19/2016 - 11:40 PM

Numpy basic commands

Numpy basic commands

import numpy as np


## CREATE ARRAY

# create array
x = np.array([[1,2.0],[0,0],(1+1j,3.)])

# create array of zeros
x = np.zeros((2, 3)) # array([[ 0., 0., 0.], [ 0., 0., 0.]])

# create range array
x = np.arange(2, 3, 0.1) # array([ 2. , 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9])

# create range array with a specefic number of elements
x = np.linspace(1., 4., 6) # array([ 1. ,  1.6,  2.2,  2.8,  3.4,  4. ])

# create array of indices
x = np.indices((3,3)) # array([[[0, 0, 0], [1, 1, 1], [2, 2, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]]])


## DIMENSSIONS

x.size  # 1 dimenssions
x.shape # multi dimenssions


## FILTERING
x = np.array([1,2,3,4,5])
ifilter = np.where(x < 3) # [0,1]
x[ifilter] # [1,2]


## SORT
a = np.array([[1,4],[3,1]])
np.sort(a)                # sort along the last axis
#array([[1, 4],[1, 3]])
np.sort(a, axis=None)     # sort the flattened array
#array([1, 1, 3, 4])
np.sort(a, axis=0)        # sort along the first axis
#array([[1, 1],[3, 4]])