Python Library Showcase

NumPy

Preview

NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays. 

NumPy stands for Numerical Python and is one of the most extensively used Python libraries used out there.

In this tutorial, we are going to take a look at the some of the applications of NumPy.

Working With Arrays

We first create an array, then then take a look at what all can be done with it.

import numpy as np
arr = np.array([1, 2, 3, 4, 5,6]) #creates an one dimensional (1*1) array
print(arr)
print("\n")
arr3 = np.array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],]) #creates an multi-dimensional(3*3) array
print(arr3)
print('\n number of dimensions :', arr3.ndim) #displays the dimesions of the array
print(arr3[0, 1, 2])#displays the element in row 1,column 2(The index starts at 0)
print (arr3.shape)
for x in arr3:
  for y in x:
    for z in y:
      print(z) #iterates all the elements of the array 
print ("We have got a new array:")
newarr = arr.reshape(1, 3, 2) #reshapes the first array with some new dimensions
print (newarr)

The code would display an error if try to modify the first parameter(0) in print (arr3[0,1,2]) as we don’t have enough elements in the array. You will have to create an array with 3^3 elements to properly utilize the first axis. As the index starts from 0,any value above 2 in the above code snippet would display an error. You can also use negative values to to traverse the array in reverse. Iterating in the context of tables is simply traversing its element one by one. There are ways to iterate 2D arrays out of an 3D array but covering that would be out of the scope of this course.

Join, Split, Filter, Search, Sort

That’s not the secret code to activate your own Winter Soldier, but a unsorted list of topics that we are going to cover next. The following code snippet covers the implementation of all the above mentioned features.

import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr3= np.array([[9,10],[13,14]])
arr4= np.array([[7,8],[11,12]])
arr = np.concatenate((arr1, arr2))#direct joining
newarr = np.array_split(arr, 3) #splitting the concatenated array into three parts
print(newarr)
print (arr)
print("\n")
array = np.concatenate(( arr4,arr3),axis=1)#joining upon an axis
print(array)
x = np.where(arr%2 == 0)#displays the index of values in arr that are even(remember index starts at 0)
print(x)
filter_arr = array>8#checks whether the elements of array are above 8 through a filter
print(filter_arr)

fruit = np.array(['banana', 'cherry', 'apple'])
print(np.sort(fruit)) #sorts the elements in lexical order, try passing around values of different data types to see its effect

Assignment

Completing this assignment is necessary to move forward in the course.

Problem Statement: Create two 3*3 arrays containing names of football players and merge them. Create a new array in which only the names of players having their name’s first letter starting from a letter after c.