3 min read
Goal
Understand what a NumPy array is and why ndarray is the central object in NumPy.
What Is an ndarray?
The main data structure used by NumPy is called an:
ndarray
ndarray means:
N-dimensional array
The letter N means that the array can have any number of dimensions.
For example, NumPy can represent:
- Single value
- List of values
- Rows and columns
- Stacks of rows and columns
using the same fundamental array structure.
Example :
import numpy as np
arr = np.array([10, 20, 30, 40])
print(arr)
Output :
[10 20 30 40]
Here:
np.array(...)
creates a NumPy ndarray.
What Is Inside the Array?
The array contains four elements:
- 10
- 20
- 30
- 40
Visual representation:
┌────┬────┬────┬────┐
│ 10 │ 20 │ 30 │ 40 │
└────┴────┴────┴────┘
Each value is called an element.
Why Is ndarray Important?
Almost everything we do later in NumPy involves an ndarray.
We will:
- Create arrays
- Access array elements
- Filter arrays
- Reshape arrays
- Combine arrays
- Perform calculations
- Calculate statistics
- Work with matrices
Real-World Example :
A NumPy array could represent daily temperatures:
temperatures = np.array([28, 30, 29, 31, 32])
or student marks:
marks = np.array([78, 82, 91, 69, 88])
Common Beginner Mistake
A NumPy array and a Python list can look similar:
[10, 20, 30]
but they are not the same object.
Python list:
data = [10, 20, 30]
NumPy array:
data = np.array([10, 20, 30])
They behave differently during numerical calculations.
Try It Yourself
Create an array containing:
5, 15, 25, 35, 45
and print it.
Key Takeaway
The ndarray is NumPy's main data structure for organizing numerical data into one or more dimensions.
Goal
The values belong to one logical numerical structure.
Goal
This array could represent marks for five students.
Again:
Goal
Three directions.
How to Think Like a NumPy User
Beginner Challenge
💬 Comments & Discussion
No comments yet
Be the first to share your thoughts!