1. What is a NumPy Array?
A NumPy array (ndarray) is the core data structure in NumPy.
- Stands for n-dimensional array
- Represents data as vectors
- Supports fast, efficient computation
Key Point:
ndarray = foundation of NumPy
2. Why Arrays are Powerful
Arrays enable:
- Vectorization → operations on all elements at once
- Faster computation
- Lower memory usage
Key Point:
Arrays process data efficiently
3. Creating an Array
Convert Python objects (like lists) into arrays:
np.array([1, 2, 3, 4])Key Point:
Use np.array() to create arrays
4. Mutability of Arrays
Arrays are mutable:
- You can change element values
Example:
arr[-1] = 5Key Point:
Values can be modified
5. Array Size Limitation
- Cannot change size directly
- Must reassign to modify size
Example:
- Cannot append like lists
Key Point:
Size change requires reassignment
6. Data Type Consistency
All elements must have the same data type.
Example:
- Mixing int + string → all become string
Key Point:
Arrays enforce uniform data types
7. Checking Array Type
Use:
type(arr)Returns:
- NumPy array type
Key Point:
Array is a specific object type
8. Checking Data Type (dtype)
arr.dtypeReturns:
- Data type of elements
Example:
int,float,U21(string)
Key Point:
dtype shows element type
9. Dimensions of Arrays
1D Array
- Single sequence
- Shape:
(n,)
Not row or column
2D Array
- List of lists
- Like a table
Example:
- Rows × Columns
3D Array
- List of 2D arrays
- Like multiple tables
Key Point:
More dimensions = more complexity
10. Shape of Array
Use:
arr.shapeReturns:
- Dimensions (rows, columns)
Example:
(4, 2)→ 4 rows, 2 columns
Key Point:
Shape describes structure
11. Number of Dimensions (ndim)
Use:
arr.ndimReturns:
- Number of dimensions
Example:
2→ 2D array
Key Point:
ndim = dimensionality
12. Reshaping Arrays
Change structure using:
arr.reshape(new_shape)Example:
(4,2)→(2,4)
Must reassign:
arr = arr.reshape(2,4)Key Point:
Reshape reorganizes data
13. Mathematical Operations
NumPy provides built-in functions:
- Mean →
np.mean() - Log →
np.log() - Floor → round down
- Ceiling → round up
Key Point:
NumPy simplifies math operations
14. Practical Importance
Arrays are used for:
- Data transformation
- Statistical analysis
- Machine learning preprocessing
Key Point:
Core structure for numerical data
15. Relationship with Pandas
- Pandas is built on NumPy
- Understanding NumPy helps:
- DataFrames
- Data analysis
Key Point:
NumPy = foundation of pandas
16. Debugging Insight
Use:
.shape.ndim.dtype
Helps:
- Understand errors
- Fix mismatches
Key Point:
Inspect arrays when debugging
17. Practical Limitation
- Usually work with:
- 1D, 2D, or 3D arrays
- Higher dimensions are rare
Key Point:
Most real-world data is 2D
Final Summary
NumPy arrays (ndarray) are powerful data structures designed for efficient numerical computation. They support vectorized operations, enforce consistent data types, and can represent data in multiple dimensions. Arrays are mutable but have fixed sizes, requiring reassignment for structural changes. Attributes like shape, ndim, and dtype help understand and debug arrays. NumPy forms the foundation for many data science tools, including pandas, making it essential for data professionals.
Key Takeaways
- ndarray = core NumPy structure
- Supports vectorized operations
- Faster than lists
- Same data type required
- Mutable but fixed size
- Use
.shape,.ndim,.dtype - Use
reshape()to change structure - Foundation for pandas
- Essential for data analysis
