 JGuizard
6/15/2016 - 9:43 AM

## Numpy basics

Numpy basics

``````import numpy as np

"""
Create an array
"""

#create an array of int
np.array([[1,2,3],[4,5,6],dtype=np.int_) #default is float

#create a vector of five 0s values
np.zeros(5)

#create an arry 3x3 of 0s values
np.zeros([3,3])

#create a vector of five 1s values
np.ones(5)

#create an arry 3x3 of 1s values
np.ones([3,3])

#create an empty array (garbage values)
np.empty((3,3))

#create an array with 1s on the diagonal
np.eye(3, 3, 1) #1 is the diagonal's x offset

#create an array with a custom diagonal
np.diag(np.arange(4))

"""
Sampling
https://docs.scipy.org/doc/numpy/reference/routines.random.html
"""

#create random array
np.array([random.randint(0, 10) for i in range(10)])

#create a random array
np.random.random((2,2))

#crate a normal distributed array
np.random.normal(0,10,(2,2)) #default is mean=0 and s.d=1

#create a random array of int
np.random.randint(0,10,size=(2,2))

"""
Describe an array X
"""

X.shape #get shape (rows,cols)
X.size #get count of vals
X.dtype #get type of vals

"""
Operations on arrays
"""

X.sum() #sum vals
X.sum(axis=0) #sum columns
X.sum(axis=1) #sum rows

X.min() #get min val, can use also axis
X.max() #get max val, can use also axis
X.mean() #get max val, can use also axis

X.argmax() #get index of max val
X.argmin() #get index of min val

"""
Indexing and slicing
"""

X[X<X.mean()] #create an array with values of X less than its mean

"""
Arithmetics operations
"""``````