Mastering NumPy in Python for Numerical Computations: A Comprehensive Tutorial (92/100 Days of Python)

Martin Mirakyan
5 min readApr 3, 2023

--

Day 92 of the “100 Days of Python” blog post series covering numpy

In today’s data-driven world, efficient numerical computing has become a crucial aspect of various industries. Python, with its rich ecosystem of libraries and packages, offers a perfect platform for handling complex numerical data. One such library is NumPy, which has gained immense popularity among developers, data scientists, and researchers. In this tutorial, we will explore the fundamentals of NumPy and its applications in real-world scenarios.

What is NumPy?

NumPy, short for Numerical Python, is a powerful library that facilitates advanced mathematical and statistical operations on large, multi-dimensional arrays and matrices. Due to its high-performance capabilities and easy-to-use functions, NumPy has become the go-to library for numerical computing in Python.

Why is NumPy popular?

  1. High-performance computing: NumPy is built in C and provides an efficient implementation of array operations, making it faster than native Python lists.
  2. Ease of use: NumPy offers a vast collection of functions that simplify complex mathematical tasks, making it accessible to developers with varying levels of expertise.
  3. Compatibility: NumPy is compatible with a wide range of other Python libraries, such as SciPy, Pandas, and Matplotlib, allowing seamless integration for various data analysis tasks.
  4. Community support: With a large and active community of developers and users, NumPy has extensive documentation and regular updates, ensuring continuous improvements and bug fixes.

Where is NumPy used?

  1. Data Science: NumPy is an essential tool for data manipulation, cleaning, and preprocessing, making it a staple in data science workflows.
  2. Machine Learning: NumPy arrays and functions are widely used for feature engineering, model training, and evaluation in machine learning pipelines.
  3. Image Processing: NumPy’s multi-dimensional array support enables efficient manipulation and transformation of image data.
  4. Scientific Computing: Researchers and scientists in fields like physics, chemistry, and engineering rely on NumPy for simulations, data analysis, and numerical problem-solving.

Getting Started with NumPy

To install NumPy, run the following command in your terminal or command prompt:

pip install numpy

Once installed, you can import the library using:

import numpy as np

Creating NumPy Arrays

NumPy arrays, also known as ndarrays, are the core data structure in NumPy. Here are some common ways to create arrays:

From a list

import numpy as np

my_list = [1, 2, 3, 4, 5]
array = np.array(my_list)
print(array) # [1 2 3 4 5]

Using built-in functions

import numpy as np

zeros = np.zeros(5) # array([0., 0., 0., 0., 0.])
ones = np.ones(5) # array([1., 1., 1., 1., 1.])
range_array = np.arange(0, 10, 2) # array([0, 2, 4, 6, 8])

Array Operations and Functions

NumPy provides a wide range of mathematical and statistical functions for array operations. Below are some examples.

Arithmetic operations

Arithmetic operations in NumPy allow element-wise addition, subtraction, multiplication, and division of arrays. These operations are useful for a wide range of applications, such as image processing, financial calculations, and scientific simulations.

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

addition = a + b # array([5, 7, 9])
subtraction = a - b # array([-3, -3, -3])
multiplication = a * b # array([ 4, 10, 18])
division = a / b # array([0.25, 0.4, 0.5])

Broadcasting

Broadcasting is a powerful feature in NumPy that allows you to perform arithmetic operations on arrays with different shapes, by automatically expanding their dimensions. This can be useful in various scenarios, such as applying a scaling factor to an entire dataset or adding a constant value to every element in an array.

import numpy as np

a = np.array([[1, 2, 3],
[4, 5, 6]])
b = np.array([1, 2, 3])

broadcasted_sum = a + b
# array([[2, 4, 6],
# [5, 7, 9]])

For example, if you have a dataset containing temperature measurements in Celsius, and you want to convert them to Fahrenheit, you can use broadcasting to achieve this.

import numpy as np

celsius = np.array([0, 20, 30])
fahrenheit = celsius * (9/5) + 32 # array([32., 68., 86.])

Mathematical functions

NumPy provides a variety of mathematical functions that can be applied element-wise to arrays, including trigonometric, logarithmic, and exponential functions. These functions can be useful in scientific and engineering applications, where mathematical transformations of data are often required.

import numpy as np

a = np.array([1, 2, 3])

square = np.square(a) # array([1, 4, 9])
sqrt = np.sqrt(a) # array([1., 1.41421356, 1.73205081])
exp = np.exp(a) # array([ 2.71828183, 7.3890561 , 20.08553692])

Statistical functions

Statistical functions in NumPy allow you to calculate various summary statistics of your data, such as mean, median, standard deviation, and variance. These functions can be useful in data analysis, helping you understand the central tendencies and dispersion of your data.

For example, if you have a dataset containing the heights of a group of people, you might want to calculate the average height and the height variation.

import numpy as np

heights = np.array([160, 170, 180, 190, 200])
mean_height = np.mean(heights) # 180.0
std_dev_height = np.std(heights) # 14.14213562373095

A Real-World Example of Analyzing Weather Data

Suppose you have collected historical stock prices for a company over the past year and want to analyze its performance by calculating the average, minimum, and maximum prices for each month. Here’s how you can use NumPy to accomplish this:

import numpy as np

# Assume we have the stock prices for 252 trading days (approximately one year)
stock_prices = np.random.uniform(100, 200, size=(252))

# Reshape the data into a 12x21 array (assuming 21 trading days per month)
monthly_data = stock_prices.reshape(12, -1)

# Calculate the average, minimum, and maximum stock prices for each month
monthly_averages = np.mean(monthly_data, axis=1) # 12 numbers
monthly_min = np.min(monthly_data, axis=1) # 12 numbers
monthly_max = np.max(monthly_data, axis=1) # 12 numbers

In this example, we used NumPy’s random number generation, array reshaping, and some statistical functions to analyze the stock price data.

What’s next?

--

--