numpy

To get bumpy use

conda install bumpy

NUMPY ARRAYS

Numpy arrays essentially come in two flavors: vectors and matrices. Vectors are strictly 1-d arrays and matrices are 2-d (but you should note a matrix can still have only one row or one column

We can create an bumpy array by directly converting a list or list of lists. Numpy arrays differ from a normal Python list because of their ability to broadcast:

my_list = [1,2,3]

np.array(my_list)

my_matrix = [[1,2,3],[4,5,6],[7,8,9]]

my_matrix will return

array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

Built-in Methods

arange

np.arange(0,10) returns array of 1-10 i.e. array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

np.arange(0,11,2) returns array([ 0, 2, 4, 6, 8, 10])

zeros and ones

np.zeros(3) returns array of 3 zeros i.e array([ 0., 0., 0.])

np.zeros((3,3)) returns

array([[ 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0.]])

same for ones np.ones(3) returns array([ 1., 1., 1.])

linspace - returns evenly spaced numbers over a specified interval

np.linspace(0,10,3) return array([ 0., 5., 10.])

np.linspace(0,10,50) - returns matrices of 50 numbers between 1 to 10

eye

np.eye(4) - returns diagonally similar number passed

array([[ 1., 0., 0., 0.], [ 0., 1., 0., 0.], [ 0., 0., 1., 0.], [ 0., 0., 0., 1.]])

Random

Create an array of the given shape and populate it with random samples from a uniform distribution

rand - Uniform distribution

np.random.rand(2) returns array([ 0.11570539, 0.35279769])

randn - Return a sample (or samples) from the "standard normal" distribution. Unlike rand which is uniform

np.random.randn(5,5)-returns 5-5 metric of random values using stand normal dist

randint - Return random integers from low (inclusive) to high (exclusive).

np.random.randint(1,100, 5)- returns 5 random number between 1 and 99(100 excluded). If only 2 arguments are passed then only 1 random number will be returned

np.random.randint(0,50,10) - returns 10 random integers between 0-49

Reshape - Reshapes the array

array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,

17, 18, 19, 20, 21, 22, 23, 24])

arr.reshape(5,5) returns

array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]])

arr.shape() - will return shape (5,5)

max min argmax argmin

array([10, 12, 41, 17, 49, 2, 46, 3, 19, 39])

.max() return max value in array - 49

.argmax() will return the index of the maxval - 4

same for min

dtype

You can also grab the data type of the object in the array using type

arr.dtype returns dtype('int64') i.e the type of the the array will be returned

NUMPY Indexing and Selection

Numpy arrays differ from a normal Python list because of their ability to broadcast:

Setting a value with index range

array([0, 1, 2, 3, 4])

arr[0:3]=100 - Which will set the values from 0 to 3 to 100

Get a value at an index

arr[8]

Get values in a range

arr[1:5]

arr = np.arange(0,11)

array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

slice_of_arr = arr[0:6] - array would be array([0, 1, 2, 3, 4, 5])

slice_of_arr[:]=99

are value would be array([99, 99, 99, 99, 99, 99, 6, 7, 8, 9, 10])

The changes also occur in our original array. Data is not copied, it's a view of the original array! This avoids memory problems!

Indexing a 2D array (matrices)

The general format is **arr_2d[row][col]** or **arr_2d[row,col]**. Recommend usage is by using the comma notation for clarity.

arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))

array([[ 5, 10, 15], [20, 25, 30], [35, 40, 45]])

FancyIndexing

Fancy indexing allows you to select entire rows or columns out of order,to show this, let's quickly build out a bumpy array

Set up matrix

arr2d = np.zeros((10,10))

Length of array arr_length = arr2d.shape[1]

for i in range(arr_length): arr2d[i] = i

array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.], [ 3., 3., 3., 3., 3., 3., 3., 3., 3., 3.], [ 4., 4., 4., 4., 4., 4., 4., 4., 4., 4.], [ 5., 5., 5., 5., 5., 5., 5., 5., 5., 5.], [ 6., 6., 6., 6., 6., 6., 6., 6., 6., 6.], [ 7., 7., 7., 7., 7., 7., 7., 7., 7., 7.], [ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.], [ 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.]])

NUMPY OPERATIONS

You can easily perform array with array arithmetic, or scalar with array arithmetic

arr = np.arange(0,10)

arr + arr --> array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])

arr/arr --> array([ nan, 1., 1., 1., 1., 1., 1., 1., 1., 1.])

arr**3 --> array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729])

Numpy comes with many universal array functions, which are essentially just mathematical operations you can use to perform the operation across the array.

np.sqrt(arr) --> array([ 0. , 1. , 1.41421356, 1.73205081, 2. ,

2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])

np.exp(arr) - Exponential

np.max(arr) - Same as arr.max()

np.sin(arr) or np.log(arr) etc.