jmquintana79
4/21/2016 - 2:43 AM

create numpy

Numpy basic commands to create arrays

import numpy as np

# create array
x = np.array([[1,2.0],[0,0],(1+1j,3.)]) # note mix of tuple and lists and types

# get dimenssions
x.shape

# create intrinsic numpy array
np.arange(10) #array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
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])
np.linspace(1., 4., 6) # array([ 1. ,  1.6,  2.2,  2.8,  3.4,  4. ])

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

# create numpy array of zeros
np.zeros(5) # array([ 0.,  0.,  0.,  0.,  0.])
np.zeros((5,), dtype=np.int) # array([0, 0, 0, 0, 0])
np.zeros((2, 1)) # array([[ 0.],[ 0.]])

# create numpy array of ones
np.ones(5) # array([ 1.,  1.,  1.,  1.,  1.])
np.ones((5,), dtype=np.int) # array([1, 1, 1, 1, 1])
np.ones((2, 1)) # array([[ 1.],[ 1.]])
       
# DElETE COLUMNS / ROWs
x = np.array([[1,2,3],
        [4,5,6],
        [7,8,9]])
# delete the first row
x = np.delete(x, (0), axis=0)
# delete the third column
x = np.delete(x,(2), axis=1)

# resize array
a=np.array([[0,1],[2,3]])
np.resize(a,(2,3))
#array([[0, 1, 2],[3, 0, 1]])

# reshape 2d to 1d
a = a.flatten()


# repeat elements
x = np.array([[1,2],[3,4]])
np.repeat(x, 2)
#array([1, 1, 2, 2, 3, 3, 4, 4])
np.repeat(x, 3, axis=1)
#array([[1, 1, 1, 2, 2, 2],
#       [3, 3, 3, 4, 4, 4]])
np.repeat(x, [1, 2], axis=0)
#array([[1, 2],
#       [3, 4],
#       [3, 4]])