Things You Need to Know About NumPy

Munia Humaira
5 min readFeb 14, 2020

The first library you learn in python!

Python has been a very dear friend to most of the data scientists and there’s some good reasons for that. One of the main benefits is Python’s extensive set of libraries, a collection of routines and functions that help data scientists perform complex tasks quite effortlessly. NumPy is one of the most useful libraries in Python.One can perform various mathematical operations(i.e., trigonometric, statistical, Fourier Transformations, algebraic etc.) using NumPy. It is an open source numerical Python library. The library contains a large number of mathematical, algebraic transformation functions, universal functions and random number generators. Many pandas objects relies on NumPy objects.

So in my first article in Medium, I will shine some light on the important operations one can do with NumPy. It is always easier to understand a topic when you see some examples.

Creating NumPy Arrays

From a Python list we can create an array by directly converting a list or list of lists:

my_list = [1,2,3]
print(my_list)
array1= np.array(my_list)
array1
my_matrix = [[1,2,3],[4,5,6],[7,8,9]]
print(my_matrix)
array2= np.array(my_matrix)
array2

We can find the shape of the array and change the elements in it .

# Shape of array:print ( array2.shape )# Changing elements in an array:array2[1][1] = 7
array2

Built-in Methods

There are lots of built-in ways to generate Arrays. Such as .arange() returns evenly spaced values within a given interval.

interval1= np.arange(0,20)
interval1
interval2= np.arange(0,20,2)
interval2

We can even create arrays with zeroes and ones. Also can create Identity matrix.

zero= np.zeros((3,3))
zero
one= np.ones((3,3))
one
id_mat = np.eye(4,4)
id_mat

Generating Random Numbers

An array of random numbers can be generated by using the functions rand(), randn() or randint(). rand() create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1). randn() returns a samples from the standard normal distribution unlike rand() which is uniform. Nevertheless, randint() returns random integers from low (inclusive) to high (exclusive).

# Normal distribution like output
random1= np.random.rand(5,5)
random1
# Standard normal distribution like output
random2= np.random.randn(5,5)
random2
# Integer output
integer= np.random.randint(1,100)
print('The random integer:',integer)

Indexing and Slicing

Indexing and slicing can also be done in a simple manner :

#Creating sample array
arr = np.arange(0,15)
arr
#Get a value at an index
print('The value of the element: ' , arr[10])
#Get values in a range
arr[1:8]
#Slices
slice_of_arr = arr[0:7]
#Show slice
slice_of_arr
#Change Slice
slice_of_arr[:5]=99
#Show Slice again
slice_of_arr

Mathematical Operations

There are several mathematical operations one can do with NumPy. The best way to gain more knowledge about that is to go through the NumPy Quickstart tutorial.

arr = np.arange(0,10)
arr
print('Addition: ', arr + arr )
print('Subtraction: ', arr - arr )
print('Multiplication: ', arr * arr )
print('Division: ', arr / arr )

Universal Array Functions

NumPy comes with many universal array functions, which can be used to perform the operation across the array. Let’s show some common ones:

#Taking square root
print('The square root is: ', np.sqrt(arr))
#Calcualting exponential (e^)
print('The exp is: ', np.exp(arr))
# Sine Function
print('The sine is: ', np.sin(arr))
#Natural Log
print('The natural log is: ',np.log(arr))

Importing And Exporting Of Files

To import from a text file you can use:

np.loadtxt(new_file.txt)

To write to a text file you can type:

np.savetxt(‘new_file.txt’,arr,delimiter= '')

P.S. All code snippets are written by me. This story is also published in my personal blog.

Summary

I have tried to provide an overview of the basic functionalities of the NumPy library in this article. NumPy helps to deal with data in almost every data science project. Above all, it makes the data manipulation smoother if the functionalities of this library are well understood.

Hope this article helps !

--

--

Munia Humaira

Writing mostly about Python, Physics, Optics, and Photonics!