NumPy Arrays
NumPy arrays are the foundation of NumPy. They allow us to store numerical data in an organized structure and perform calculations on many values efficiently.
A NumPy array is a collection of values stored in a structured numerical format.
If you understand how to create, access, and work with NumPy arrays, you have learned one of the most important foundations for Data Science, Machine Learning, and AI.
What Is a NumPy Array?
A NumPy array is a data structure used to store multiple values together.
For example, suppose we have five student scores:
80 90 75 88 95
Instead of storing them in separate variables:
score1 = 80 score2 = 90 score3 = 75 score4 = 88 score5 = 95
we can store them together in one NumPy array:
import numpy as np scores = np.array([80, 90, 75, 88, 95]) print(scores)
[80 90 75 88 95]
Creating a NumPy Array
We create an array using:
np.array()
Example:
import numpy as np numbers = np.array([10, 20, 30, 40, 50]) print(numbers)
[10 20 30 40 50]
The Python list:
[10, 20, 30, 40, 50]
is converted into a NumPy array by:
np.array(...)
Array Elements
Each value inside an array is called an element.
numbers = np.array([10, 20, 30, 40, 50])
Here:
10 → element 20 → element 30 → element 40 → element 50 → element
Each element has a position called an index.
Accessing Array Elements
NumPy uses zero-based indexing, just like Python lists.
numbers = np.array([10, 20, 30, 40, 50]) print(numbers[0]) print(numbers[1]) print(numbers[2])
10 20 30
The indexes are:
Index: 0 1 2 3 4 Value: 10 20 30 40 50
So:
numbers[0] → 10 numbers[3] → 40 numbers[4] → 50
Changing an Array Element
You can change an element by assigning a new value to its index.
numbers = np.array([10, 20, 30, 40, 50]) numbers[2] = 100 print(numbers)
[10 20 100 40 50]
The value at index 2 changed from
30 to 100.
Performing Operations on Arrays
One of the biggest advantages of NumPy arrays is that mathematical operations can be applied to the whole array.
Example:
numbers = np.array([10, 20, 30, 40, 50]) result = numbers * 2 print(result)
[ 20 40 60 80 100]
NumPy multiplied every element by 2.
We did not need to write a loop.
Adding a Value to Every Element
We can also add a value to every element.
prices = np.array([100, 200, 300, 400]) new_prices = prices + 50 print(new_prices)
[150 250 350 450]
NumPy automatically applies the addition to every value.
One-Dimensional Arrays
The array we have used so far is a one-dimensional array.
numbers = np.array([10, 20, 30, 40, 50])
You can imagine it as one row:
10 20 30 40 50
This is commonly called a 1D array.
Two-Dimensional Arrays
NumPy can also store arrays containing rows and columns.
data = np.array([
[10, 20, 30],
[40, 50, 60]
])
print(data)
[[10 20 30] [40 50 60]]
Think of it like a table:
Column
0 1 2
Row 0 10 20 30
Row 1 40 50 60
This is a 2D array.
NumPy Arrays in AI
Imagine we have information about three people:
Age Height Weight 25 175 70 30 180 82 22 165 60
We can represent this data as a NumPy array:
import numpy as np
people = np.array([
[25, 175, 70],
[30, 180, 82],
[22, 165, 60]
])
print(people)
[[ 25 175 70] [ 30 180 82] [ 22 165 60]]
This type of numerical structure is much closer to the kind of data that Machine Learning algorithms consume.
Checking the Array Type
We can check whether an object is a NumPy array using:
import numpy as np numbers = np.array([10, 20, 30]) print(type(numbers))
<class 'numpy.ndarray'>
ndarray means
N-dimensional array.
This is the main array object provided by NumPy.
Array Data Type
NumPy arrays have a data type for the values they contain.
numbers = np.array([10, 20, 30]) print(numbers.dtype)
You may see something like:
int64
This means the array contains integer values stored using NumPy's integer data type.
You don't need to memorize all NumPy data types yet. The important idea is that NumPy keeps track of the type of data stored in an array.
Python List vs NumPy Array
| Python List | NumPy Array |
|---|---|
| General-purpose collection | Designed for numerical data |
| Can contain different types | Usually works with a consistent data type |
| Less optimized for numerical calculations | Optimized for numerical calculations |
| Useful for general Python programming | Very useful for Data Science and AI |
This does not mean Python lists are bad. They are excellent general-purpose data structures.
The difference is that NumPy arrays are specifically designed to make numerical computing easier and more efficient.
Complete Example
Let's combine the concepts we learned:
import numpy as np
scores = np.array([80, 90, 70, 85, 95])
print("Scores:", scores)
print("First score:", scores[0])
scores[2] = 75
print("Updated scores:", scores)
double_scores = scores * 2
print("Doubled scores:", double_scores)
print("Average:", np.mean(scores))
print("Highest:", np.max(scores))
print("Lowest:", np.min(scores))
Scores: [80 90 70 85 95] First score: 80 Updated scores: [80 90 75 85 95] Doubled scores: [160 180 150 170 190] Average: 85.0 Highest: 95 Lowest: 75
Why Arrays Matter for AI
AI systems deal with enormous amounts of numerical data.
For example, an image can be represented using numbers. A grayscale image might look conceptually like:
[
[0, 50, 100],
[150, 200, 250],
[100, 50, 0]
]
Each number can represent a pixel intensity.
NumPy arrays give us a convenient way to store and manipulate this type of numerical data.
Later, when you learn Deep Learning, these ideas will become even more important because neural networks work with multidimensional numerical structures called tensors.
NumPy arrays are the foundation of numerical data handling.
A NumPy array stores values in an organized numerical structure. You can access elements using indexes, change values, perform calculations on entire arrays, and work with one-dimensional or multidimensional data. These concepts are fundamental for Data Science, Machine Learning, and AI.