Introduction to Google Colab and the NumPy library

Kunal Chhikara
4 min readJun 8, 2021

What is Google Colab?

Colaboratory, or “Colab” for brief, is a product from Google Research. Colab permits anyone to type in and execute self-assertive python code through the browser, and is particularly well suited to machine learning, data analysis and instruction. More in fact, Colab is a hosted Jupyter notebook service that requires no setup to use, while providing free access to computing resources including GPUs.

Advantages of Google Colab over Jupyter Notebook

  • Work from any computers. All notebooks are saved in Google Drive.
  • Don’t need to worry that conda create env will clutter your directories
  • Share to someone, or everyone easily. Just like a Google Doc.
  • Automatic history and versioning
  • Free GPU
  • Form widgets are simple and easier to use.

NumPy

NumPy (brief for Numerical Python) is “the principal bundle for logical computing with Python” and it is the library on which Pandas, Matplotlib and Scikit-learn builds on.

Installation

NumPy does not come with Python by default so it should be installed. The most effortless way to get NumPy is to install Anaconda. If you do not want to install anaconda and just install NumPy, you can download the version for your operating system from this page or you can just use the python installation package to download it.

pip install numpy

After this you just need to import it in any IDE that you are using by

import numpy as np

NumPy Array

A numpy array is a grid of values, all of the same sort, and is ordered by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array may be a tuple of integers giving the measure of the array along each dimension. We can initialize numpy arrays from nested Python records, and get to components using square brackets:

a = np.array([1,2,3])
b = np.array([[1,2,3],[4,5,6]])
print(type(a))
print(a.shape)
print(type(b))
print(b.shape)
<class ‘numpy.ndarray’>
(3,)
<class ‘numpy.ndarray’>
(2, 3)

NumPy also has many functions for creating different types of arrays:

# Creates a 2X2 zeros array 
c=np.zeros((2,2))
# Creates a 2X4 ones array
d=np.ones((2,4))
# Create a 2X2 constant array of number 7
e = np.full((2,2), 7)
# Creates a 3X3 identity matrix
f = np.eye(3)
# Creates a 3X2 array filled with random values
g = np.random.random((3,2))
# Creates a range of elements till 7
h = np.arange(7)

Slicing

Similar to Python lists, numpy arrays can be sliced. Since arrays may be multidimensional, you must specify a slice for each dimension of the array:

# b is the sub array consisting of the first 2 rows and columns 1 and 2 of array aa = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
b = a[:2, 1:3]
print(b)
# A slice of an array is a view into the same data, so modifying it will modify the original array.print(a[0, 1])
b[0, 0] = 77 # b[0, 0] is the same piece of data as a[0, 1]
print(a[0, 1])
[[2 3]
[6 7]]
2
77

Adding and removing the elements

The numpy.append() appends values along the mentioned axis at the end of the array. The syntax is numpy.append(array, values, axis = None).

# adds 1,2,3,4 at the end
a = [0]
a = np.append(a, [1,2,3,4])
print(a)
[0 1 2 3 4]

The numpy.delete() function returns a new array with the deletion of sub-arrays along with the mentioned axis. The syntax is numpy.delete(array, values, axis = None).

# Deletes elements 2 and 3 from the array
a = np.delete(a, [2,3])
print(a)
[0 1 4]

Sorting

numpy.sort() function returns a sorted copy of an array.

Parameters :

arr : Array to be sorted.
axis : Axis along which we need array to be started.
order : This argument specifies which fields to compare first.
kind : [‘quicksort’{default}, ‘mergesort’, ‘heapsort’]Sorting algorithm.

# To sort an array, the sort (array, axis, kind, orderby) function is used
a = np.array([[3,2,1],[6,5,4],[9,8,7]])
print(a)
a = np.sort(a, axis=1, kind = ‘quicksort’)
print(a)
[[3 2 1]
[6 5 4]
[9 8 7]]
[[1 2 3]
[4 5 6]
[7 8 9]]

Reshaping the array

The numpy.reshape() function shapes an array without changing data of array. Its syntax is numpy.reshape(array, shape, order = ‘C’)

Parameters :

array : [array_like]Input array
shape : [int or tuples of int] e.g. if we are aranging an array with 10 elements then shaping it like numpy.reshape(4, 8) is wrong; we can
order : [C-contiguous, F-contiguous, A-contiguous; optional]
C-contiguous order in memory(last index varies the fastest)
C order means that operating row-rise on the array will be slightly quicker
FORTRAN-contiguous order in memory (first index varies the fastest).

a = np.arange(10)
print(a)
a = a.reshape(2,5)
a
[0 1 2 3 4 5 6 7 8 9]
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])

Concatenation

numpy.concatenate function concatenate a sequence of arrays along an existing axis.

# Concatenate — Arrays are joined on the basis of their axis
b = [1,2]
c = [3,4]
d=[b,c]
d = np.concatenate(d)
print(d)
[1 2 3 4]

Mathematical Functions

NumPy contains a large number of various mathematical operations. NumPy provides standard trigonometric functions, functions for arithmetic operations, handling complex numbers, etc.

e = np.add(b,c)
print(e)
f = np.subtract(b,c)
print(f)
g = np.multiply(b,c)
print(g)
h = np.divide(b,c)
print(h)
i = np.power(b,c)
print(i)
[4 6]
[-2 -2]
[3 8]
[0.33333333 0.5 ]
[ 1 16]

--

--