Few Things You Should Be Able to Do With the Numpy Library.

Promise Shittu
The Startup
Published in
4 min readAug 29, 2020

What is Numpy?
Numpy is an open-source library. It is a numerical Python library. It contains a multi-dimensional array and matrix data structures and many mathematical, algebraic and transformation functions.

Numpy

What Numpy has to offer?
Numpy is used to increase the speed of one’s python scripts. Python is inherently slow using a for loop but Numpy makes an iteration operation quicker. Numpy uses vectorization causing less memory to be used in the system and to perform operations even more quicker.

How to install Numpy?
The easiest way to install NumPy is by using Pip.

pip install numpy

Using Numpy?
To use Numpy and it’s functionality, it is necessary to always “import numpy” in your coding environment before using it. Conventionally, we import numpy using the following command:

import numpy as np

Numpy Arrays
Numpy arrays are homogeneous. They have only one type of data unlike the Python array which is heterogeneous. This is one reason why Numpy Arrays are faster than Python Arrays.

Creation of Numpy Arrays
After importing numpy, we can create numpy arrays as seen below.

import numpy as np
array_even = np.array([2, 4, 6, 8])

We created an array of even numbers. This is a one-dimensional array.

Dimension of Numpy Arrays
Numpy arrays have dimensions returned in this format:

(a,b)where ‘a’ represents the number of rows in the array and ‘b’ represents the number of columns in the array.

To check for dimension of arrays, for example ‘array_even’ as above, we use “.shape” method:

array_even.shape
This returns (1,4) representing 1 row and 4 columns.

Size of Numpy Arrays
We can check for size of a NumpyArray using the “.size” method. The size of an array is an integer value representing the product of the number of rows and columns. The ‘array_even’ example as above would return a value of 4 after running and printing:

array_even.size

Transpose of NumpyArrays
This interchanges the row with the column of an array and it is performed with the “.T” method. For example:

import numpy as np
array_l = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
array_l_transposed = array_l.T

Default Arrays
You can override default datatypes of an array datatype by specifying it’s datatype during creation. For example:

import numpy as np
array_a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype =float)

The array created is a 2-dimensional array and the values are integer values initially but are overwritten to float datatypes. When array_a is printed, the following is gotten :

[[1. 2. 3. 4.]
[5. 6. 7. 8.]]

The code snippet below illustrates other ways how Arrays could be created:

import numpy as np
#The below will create an array(4 rows and 3 columns) filled with zeros.
#Note that the parameter of the method is shape.
np.zeros((4, 3))
#The below will create an array(1 row and 3 columns) filled with ones.
#Note that the parameter of the method is shape.
np.ones((1, 3))
#The below will create an array(3 rows and 4 columns) filled with 5.
#Note that the first parameter of the method is shape
#And the second parameter is the array element you intend to fill with.
np.full((3, 4), 5)
#The below will create an array(2 rows and 6 columns) filled with random numbers.
np.random.random((2, 6))

Reshaping and Array Indexing
Reshaping is a method of transforming an array A to another array B with a different dimension that will have the same size as array A.
Array indexing involves locating an element or elements of an array. This is illustrated in the code snippets below:

import numpy as np
#Creating an array of 3 rows and 4 columns
array_k = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
#The below allows array_K to be reshaped to another array L with both having same sizes of 12
array_l = array_k.reshape(2, 6)
#Negative indexing(Indexing from behind)
#The below will return 2
print(array_k[0, -3])
#The below will return 10
print(array_k[2, 1])
#Boolean Indexing method
#Using 'where'. The format is ==> np.where(condition, true value, false value)
#Using 'logical_and'. The format is ==> np.logical_and(condition1, condition2)

Array Math

import numpy as np
#Creating an array of 3 rows and 4 columns
array_k = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
array_t = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
#Returns sum of array
array_k.sum()
#Returns sum of array across columns
array_k.sum(axis=0)
#Returns sum of array across rows
array_k.sum(axis=1)
#Returns array with cummulative sum of array
array_k.cumsum()

#Returns product of array
array_k.prod()
#Returns array with cummulative product of array
array_k.cumprod()
#Returns the average element of array
array_k.mean()
#Returns minimum value in array(Axis can be specified in parameter)
array_k.min()
#Returns maximum value in array(Axis can be specified in parameter)
array_k.max()
#Returns array of array_k in powers of array_t
np.power(array_k, array_t)
#Creating an array of 3 rows and 3 columns
j = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]
k = np.array([[2, 2, 2], [3, 4, 5], [6, 7, 8]])
l = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])


#Product of j,k,l
array_JTimesKTimesL = np.array(j * k * l)
#Returns the Range in the matrix(axis could be specified too)
print(array_JTimesKTimesL.ptp())

Broadcasting
Broadcasting allows us to use arrays of different shapes to perform operations. For smaller arrays to be compatible, it needs to have one of its dimension as “1” and the other dimension needs to match the larger array.

import numpy as np
a = np.array([2, 4, 6]) #Has a shape of (1,3)
b = np.array([3])
#Has a shape of (1,1)
#Hence a and b is compatible
c = a + b #Broadcasting takes place
print(c) #Returns [5 7 9]

--

--