sandeep sandy
7 min readJan 27, 2022

--

Sandeep 😎😎😎

Intro to NumPy
NumPy is the fundamental package for scientific computing in Python. NumPy is used in many other packages in python even though you may not see it explicitly running in your code.

It is a python library that provides a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more.

Import the numpy package
[ ]
import numpy as np
Make an array from a list.
This will create a numpy array with 3 elements.

Each numpy array contains an attribute called shape. Shape is a tuple which tells you how many elements you have per axis. For example (5, 6, 8) means 5 elements per axis 0, 6 elements per axis 1 and 8 elements per axis 2. Usually you will have only one or two axes.

The shape for the array in this section is (3, ). The reason for this funny notation is python's type system where shape must be a tuple and this is a tuple with 1 element. Writing just 3 would mean a number, not a tuple.

To verify that try np.array([1, 2, 3]).shape

[ ]
np.array([1, 2, 3])
Make an array from a list of lists
The shape of this array is (2, 2) so this is a matrix.

[ ]
np.array([[1, 2], [3, 4]])
Make an array using a function
The arrays made in previous sections were based on us supplying each value.

Very often we need large arrays and it is not possible to use our current approach and thus we need to use array creating functions. The most important functions are zeros and ones. They both take a parameter called shape which determines the shape of the output array.

zeros makes an array which is all 0 docs
ones makes an array which is all 1 docs
[ ]
np.zeros((2, 3))
[ ]
np.ones((3, 2))
Make an array by starting at 0, incrementing by 2 and ending before 10
arange is the exact same thing as regular python range except giving back a numpy array.

This functions requires only a single argument which is the end point. The start point is assumed to be 0 and step is assumed to be 1.

In this variety when in takes three parameters, the first one is the start, second one is the end and the third one is the step.

docs

[ ]
np.arange(0, 10, 2)
Make an array of four evenly spaced numbers including both endpoints
[ ]
np.linspace(0, 1, 4)
Make an array of random numbers (using a normal distribution)
The random submodule of numpy contains a variety of functions for generating random numbers following a specified probability distributions or sampling from a finite set of choices. To learn more about it visit the documentation.

[ ]
mean = 10
stdev = 3
number_values = 20
np.random.normal(mean, stdev, number_values)
Indexing Arrays
Access elements of a 1-dimensional array. Get the first and last element.
In python as is in many other programming languages all arrays start from 0 so the first element is at position 0.

To access the last element we can use -1 as the index. x[-1] is identical to x[x.shape[0] - 1] where shape[0] is the length of the array.

[ ]
x = np.arange(4)

print(x[0])
print(x[-1])
Reshape an array
Remember that shape is the fundamenal property of each array and we can modify it as we desire and we often do so. To change the shape of an array we use the reshape method.

The code in this example creates an 1D-array of 6 elements using the arange functon.

We convert it to a 2D-array using the reshape method.

You see that the total amount of numbers did not change, only our view of them. 6 elements in a vector or 3 x 2 elements in a matrix.

It is very important that when using reshape to ensure that dimensions match or you will get an error. 6 = 3 x 2 is a match so this works but trying np.arange(6).reshape(4, 2) will give you an error.

docs

[ ]
x = np.arange(6).reshape(2, 3)
print(x)
Get the last row
When dealing with arrays which are of more than a single dimension we can use partial indexing specifications go slice the array the way we want.

For example print(x[-1]) will print the last row.

To get the last column take the transpose of the 2d-array and then use [-1].

For exampe print(x.T[-1]) will give you the last columns. x.T is the transpose of x

[ ]
print(x[-1])
Get the last element of the last row
Here we are using full indexing to get a single element.

[ ]
print(x[-1, -1])
Get elements like a list
Using the notation a:b we can slice arrays as precisely as we want.

a:b means give me back all elements whose positions is in that set.

For example y = [1, 3, 5, 7, 9] and y[0:2] would give us the first two elements [1, 3].

Negative indices mean from back so -3 means 3 positions starting from the last position.

Experiment with the values until you get the feel for it.

[ ]
x = np.arange(10)

print(x)
print(x[:3])
print(x[1:4])
print(x[-3:])
Get all elements in reverse order
The enhanced slicing syntax is a:b:c were c is the step. For example 0:10:2.

If we omit a and b we always asume they are a = 0 and b = length of the list so we are only left with a step.

Thus ::2 means slicing from 0 to end with a step 2. ::3 mean slicing with a step 3.

Now what if we used negative steps. Using -1 as the step would reverse the logic and a becomes the end of the list and b becomes zero. So ::-1 will slice in reverse direction.

Finally there is also the slice form ::1 or written short : which means give me back all elements in order as they are present.

[ ]
print(x[::-1])
Get every second element or third element
[ ]
print(x[::2])
print(x[::3])
Create a 5x5 matrix
[ ]
x = np.arange(25).reshape(5, 5)
print(x)
Get the first two columns and the first two rows
Until now you have learned how to slice on a 1D array but slices can be generalized to any dimensional arrays.

The trick is to create a slice for every dimensions.

In this example we have a 2d-array so we create two slices, one for dimension 0 (rows) and one for dimension 1 (columns)

[ ]
print(x[:2, :2])
Get every second column of every second row
[ ]
print(x[::2, ::2])
Access parts of an array with index arrays
Generate six random integers between 0 and 9 inclusive
randint in np.random is a very useful function which generates uniformly distributed random integers from the range provided as its first two parameters (start and end of range respectively). It's third parameter tells the method how many numbers we need.

[ ]
x = np.random.randint(0, 10, 6)
print(x)
Get the first number and the last two numbers
Slices are inhetently linear and give us access to arrays in a linear fashion.

To access elements in any order we must use index arrays which are numpy arrays holding integer numbers which tell what elements to chose from the indexed array.

[ ]
indices = [0, -2, -1]
print(x[indices])
Create two arrays
The array x2 is created as x1 * -1. The multiplication operator here means multiply all elements of x1 with -1 and give back that as result.

[ ]
arrayLength = 5
x1 = np.arange(arrayLength)
x2 = np.arange(arrayLength) * -1

print(x1)
print(x2)
Create a random shuffled range of numbers
This and the following example shows how to shuffle an array in numpy.

Shuffling means reoreding the items in an array and we can do that using random permutations of the array positions.

[ ]
indices = np.random.permutation(arrayLength)
print(indices)
Use indices to re-order arrays
[ ]
print(x1[indices])
print(x2[indices])
Create array of boolean values
The main novelty of this example is print(x > 2).

x > 2 generates an array of boolean values where each element is True or False. Having True at position i of the output means that the conditions is satisfied for the element of x at position i.

For exampe if x=[0,1,2,3,4] then x > 2 would be [False, False, False, True, True].

As you see 0 > 2 = False, 1 > 2 = False, 2 > 2 = False, 3 > 2 = True and 4 > 2 = True and that is shown in the output array.

[ ]
x = np.arange(5)
print(x)
print(x > 2)
Use boolean values to index arrays
Once we have created our boolean array we can use it to index other arrays.

The rule is very simple. If at position i of index we have True include x[i] in the output.

[ ]
print(x[x > 2])
print(x[np.array([False,False,False,True,True])])
Array Math
Try adding to a list
[ ]
x = [1, 2, 3, 4]

print(x + 10)
Try again with an array
When trying with a list you got an error because simple python lists do not support addition with scalars.

But numpy arrays do allow that.

[ ]
x = np.array(x)

print(x)
print(x + 10)
Exercises
Create an array from 0 to 26 assign it to a variable x
[ ]

Reverse the order of the array and print it out
[ ]

Convert the 1-dimensional array you created into a 3 dimensional array
[ ]

Find the index (location) for value 12
[ ]

Create a random shuffled list of values from 0-19 and assign it to variable y
[ ]

Create an array of boolean values for values in y that are greater than 10
[ ]

Create a random sample of 20 data points from a normal distribution with mean of 10 and standard deviation 5
[ ]

--

--

sandeep sandy
0 Followers

shares the machine learning and nlp content... be free with me in work and discussions