By the end of this lesson, you will be able to:
:information_source: What is NumPy? NumPy stands for Numerical Python. It's a special Python package that helps us work with numbers and arrays much faster than regular Python lists. Think of it as a super-powered calculator for handling lots of numbers at once!
:bulb: Want to explore more? Visit the official NumPy website: https://numpy.org/
:books: What We'll Learn Today
We'll explore NumPy through hands-on examples:
- Install and Import NumPy - Set up NumPy on your computer
- Create and Generate NumPy arrays - Make different types of arrays
- NumPy array properties - Learn about array characteristics
- Random in NumPy - Generate random numbers
- Access Items in NumPy array - Get specific values from arrays
- Filter Items in NumPy array - Find data that meets certain conditions
- Mathematical operations on NumPy arrays - Do math with entire arrays
:wrench: Install and Import NumPy
Installation Steps
- VSCode users: Open your terminal and type:
bashpy -m pip install numpy
- Google Colab users: Good news! NumPy is already installed for you!
Import NumPy
After installation, import NumPy into your program:
python
import numpy as np
note We use
np
as a shortcut name for NumPy. This makes our code shorter and easier to read!
Let's start by creating a simple array with numbers from 1 to 5:
array = np.array([1,2,3,4,5])
print(array)
Expected output:
[1 2 3 4 5]
NumPy makes it easy to create arrays with number sequences:
array = np.arange(5)
print(array)
Expected output:
[0 1 2 3 4]
Sometimes you need arrays filled with the same number:
array = np.ones(5)
print(array)
Expected output:
[1. 1. 1. 1. 1.]
:bulb: Arrays of ones and zeros are super useful when you need to initialize data or create placeholder arrays!
python
array = np.zeros(5) print(array)
Expected output:
output[0. 0. 0. 0. 0.]
Creating 2D Arrays (Tables)
You can create arrays that look like tables with rows and columns:
python
array2D = np.array([[1,2,3],[4,5,6]]) print(array2D)
Expected output:
output[[1 2 3] [4 5 6]]
You can also create 2D arrays filled with ones or zeros:
python
array2D = np.zeros((2,3)) print(array2D)
Expected output:
output[[0. 0. 0.] [0. 0. 0.]]
note For 2D arrays,
(2,3)
means 2 rows and 3 columns. Always remember: rows first, then columns!
Every NumPy array has special properties that tell us about its structure:
Let's explore these properties:
print(array.dtype)
print(array2D.dtype)
Expected output:
float64
float64
:information_source: Info
float64
means the array stores decimal numbers with high precision
print(array.ndim)
print(array2D.ndim)
Expected output:
1
2
print(array.shape)
print(array2D.shape)
Expected output:
(5,)
(2, 3)
print(array.size)
print(array2D.size)
Expected output:
5
6
NumPy includes tools for generating random numbers, which is useful for simulations and testing:
from numpy import random
Create a random decimal between 0 and 1:
print(random.rand())
Expected output:
0.3567409764700181
Create a random whole number:
print(random.randint(10))
Expected output:
7
:bulb:
random.randint(10)
gives you a random number from 0 to 9 (not including 10)
:emoji: Access Items in NumPy Arrays
Accessing Single Items
Just like Python lists, you can get specific items using their position (index):
python
array = np.array([1,2,3,4,5]) print(array[1])
Expected output:
output2
Accessing Items in 2D Arrays
For 2D arrays, specify both row and column:
python
array2D = np.array([[1,2,3],[4,5,6]]) print(array2D[1,2])
Expected output:
output6
Accessing Multiple Items (Slicing)
You can get multiple items at once using slicing: note Remember the slicing rules:
[start:end]
- gets items from start to end-1[:end]
- gets items from beginning to end-1[start:]
- gets items from start to the endprint(array[1:3])
print(array[1:])
print(array[:3])
Expected output:
[2 3]
[2 3 4 5]
[1 2 3]
You can slice 2D arrays to get specific rows and columns:
print(array2D[:,1:3])
Expected output:
[[2 3]
[5 6]]
:information_source: Info The
:
means "all rows", and1:3
means "columns 1 and 2" (remember, 3 is not included!)
Filtering helps you find specific data that meets certain conditions. This is super useful in data analysis!
Let's see how it works:
array > 2
Expected output:
array([False, False, True, True, True])
Now use this condition to get only the values greater than 2:
array[array > 2]
Expected output:
array([3, 4, 5])
The same technique works for 2D arrays:
array2D > 2
Expected output:
array([[False, False, True],
[True, True, True]])
array2D[array2D > 2]
Expected output:
array([3, 4, 5, 6])
:memo: When you filter a 2D array, the result becomes a 1D array with all matching values!
:emoji: Mathematical Operations on NumPy Arrays
One of NumPy's superpowers is doing math with entire arrays at once! This is much faster than using loops.
Basic Math Operations
python
print(array + 10)
Expected output:
output[11 12 13 14 15]
tip This adds 10 to EVERY element in the array - no loops needed!
print(array * 5)
Expected output:
[ 5 10 15 20 25]
You can also do math between two arrays:
print(array + array)
Expected output:
[ 2 4 6 8 10]
To save your calculation results, assign them back to a variable:
array = (array + array)
print(array)
Expected output:
[2 4 6 8 10]
In this lesson, we learned:
:white_check_mark: NumPy basics: A powerful library for working with numbers and arrays
:white_check_mark: Creating arrays: Using np.array()
, np.arange()
, np.ones()
, and np.zeros()
:white_check_mark: Array properties: Understanding dtype, ndim, shape, and size
:white_check_mark: Accessing data: Using indexing and slicing to get specific values
:white_check_mark: Filtering: Finding data that meets certain conditions
:white_check_mark: Math operations: Performing calculations on entire arrays at once
NumPy is your foundation for data science - master it, and you'll be ready for more advanced tools!
Code with AI: Try using AI to work with NumPy.
Prompts:
Array Creation Challenge: Create three different arrays:
Filter Practice: Create an array of test scores and:
Math Magic: Create two arrays and:
Real-World Application: Think of a situation where you'd need to: