Data Science / Data Science with Python

Performing Numerical Computations with NumPy

This tutorial teaches you how to use the NumPy library to perform numerical computations in Python. It covers creating arrays, performing basic mathematical operations, and using …

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Explores Python libraries and tools used in data science.

1. Introduction

Welcome to this tutorial on performing numerical computations with NumPy. Our goal is to help you understand how to use the NumPy library to perform numerical computations in Python. By the end of this tutorial, you will be able to create arrays, perform basic mathematical operations on them, and use various built-in NumPy functions.

Prerequisites:
- Basic understanding of Python programming
- Python 3.x installed on your system
- A text editor or IDE to write Python code
- Basic understanding of Python's lists and tuples

2. Step-by-Step Guide

NumPy and Arrays

NumPy (Numerical Python) is a Python library used for numerical computations. It provides a high-performance multidimensional array object, and tools for working with these arrays.

An array is a data structure that stores values of the same data type. In Python, this is the main difference between arrays and lists. While Python lists can contain values corresponding to different data types, arrays in Python can only contain values corresponding to the same data type.

To use NumPy in Python, you have to install it first if you haven't already done so. You can install it with the following command: pip install numpy.

Once installed, you can import it in your Python program as follows:

import numpy as np

Creating a NumPy Array

Creating a NumPy array is as simple as passing a sequence like a list or a tuple to the numpy.array() function.

import numpy as np

# Creating a NumPy array from a Python list
list = [1, 2, 3, 4]
arr = np.array(list)
print(arr)  # Output: [1 2 3 4]

Basic Mathematical Operations

NumPy allows you to perform mathematical operations on arrays. This includes addition, subtraction, multiplication, division, etc.

import numpy as np

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

# Add arrays
c = a + b
print(c)  # Output: [5 7 9]

# Subtract arrays
d = b - a
print(d)  # Output: [3 3 3]

# Multiply arrays
e = a * b
print(e)  # Output: [ 4 10 18]

# Divide arrays
f = a / b
print(f)  # Output: [0.25 0.4  0.5]

3. Code Examples

Let's dive deeper with more examples.

Example 1: Array creation with different dimensions

import numpy as np

# 1-D array
a = np.array([1, 2, 3])
print(a)

# 2-D array
b = np.array([[1, 2, 3], [4, 5, 6]])
print(b)

# 3-D array
c = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(c)

In this example, we create 1-D, 2-D, and 3-D arrays. The dimensions of the array can be checked using array_name.ndim.

Example 2: Using built-in functions

NumPy provides several built-in functions like sum(), min(), max(), mean(), median() etc.

import numpy as np

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

# Use built-in functions
print(np.sum(a))  # Output: 21
print(np.min(a))  # Output: 1
print(np.max(a))  # Output: 6
print(np.mean(a))  # Output: 3.5

4. Summary

In this tutorial, we learned how to create arrays using NumPy, perform basic mathematical operations on these arrays, and use built-in NumPy functions. The next step would be to explore more advanced NumPy features like array slicing, reshaping, broadcasting, etc.

Here are some additional resources:
- NumPy official documentation
- Python for Data Analysis by Wes McKinney

5. Practice Exercises

  1. Create a 2-D array with values ranging from 0 to 8 and print it.
  2. From the array created in exercise 1, print the sum of all the elements.
  3. From the array created in exercise 1, print the minimum and maximum values.

Solutions

  1. python import numpy as np a = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) print(a)
  2. python print(np.sum(a))
  3. python print(np.min(a)) print(np.max(a))

Keep practicing and exploring more functions and operations that can be performed with NumPy. Happy coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Favicon Generator

Create favicons from images.

Use tool

Word to PDF Converter

Easily convert Word documents to PDFs.

Use tool

Percentage Calculator

Easily calculate percentages, discounts, and more.

Use tool

Date Difference Calculator

Calculate days between two dates.

Use tool

CSV to JSON Converter

Convert CSV files to JSON format and vice versa.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help