Why Learn Numpy Arrays?

In Python data analysis and scientific computing, Numpy is the foundation. It provides an efficient multidimensional array object that allows us to manipulate complex numerical collections as easily as simple data, with much faster computation than pure Python lists. To deeply understand Numpy, you must master array structure (shape), element access (indexing), and subarray slicing—these are the core topics of this article.

I. Array Creation: Starting from the Basics

To work with arrays, you first need to create them. Numpy offers several common methods for creating arrays, which beginners should start with:

1. np.array(): Create from Python Lists

import numpy as np

# 1D array (vector)
arr1 = np.array([1, 2, 3, 4, 5])
print("1D array:", arr1)

# 2D array (matrix)
arr2 = np.array([[1, 2, 3], 
                [4, 5, 6], 
                [7, 8, 9]])
print("2D array:\n", arr2)

2. Quickly Create All-Zero/All-One Arrays

# 3x4 all-zero array
zeros = np.zeros((3, 4))  # shape=(3,4)
print("All-zero array:\n", zeros)

# 2x5 all-one array
ones = np.ones((2, 5))
print("All-one array:\n", ones)

3. np.arange(): Similar to Python’s range

# From 0 to 9 with step 2 → [0,2,4,6,8]
arange_arr = np.arange(0, 10, 2)
print("arange array:", arange_arr)

II. Shape: The “Dimensional ID” of Arrays

shape is a core attribute of Numpy arrays, telling us the dimension information (e.g., “rows and columns”). Understanding shape is the first step in array manipulation.

1. Check Shape

Use the .shape attribute to view an array’s dimensions:

print("arr1.shape:", arr1.shape)  # Output: (5,) → 1D array with length 5
print("arr2.shape:", arr2.shape)  # Output: (3, 3) → 3x3 matrix

2. Reshape Array: reshape()

reshape() adjusts array dimensions, but total elements must remain unchanged. For example:

# arr3 = np.arange(6) → [0,1,2,3,4,5] (shape=(6,))
arr3 = np.arange(6)
arr3_reshaped = arr3.reshape(2, 3)  # Reshape to 2x3
print("Reshaped array:\n", arr3_reshaped)
print("New shape:", arr3_reshaped.shape)  # (2, 3)

Key Points:

  • reshape() only rearranges dimensions without changing data order (C-style row-major).
  • To convert to 1xN shape: use reshape(1, -1) (where -1 auto-calculates the number of columns).

III. Indexing: Accessing Array Elements

Indexing is like a “key” to precisely access elements. Numpy array indexing follows Python list rules but supports concise multi-dimensional indexing.

1. 1D Array Indexing

1D arrays use 0-based indexing, with support for negative indices (counting from the end):

print("arr1[0] =", arr1[0])   # First element: 1
print("arr1[-1] =", arr1[-1]) # Last element: 5
print("arr1[2] =", arr1[2])   # Third element: 3

2. 2D Array Indexing

2D arrays require two indices (row, column), separated by commas:

# arr2 = [[1,2,3], [4,5,6], [7,8,9]]
print("arr2[1, 2] =", arr2[1, 2])   # Row 1, Column 2 → 6
print("arr2[0, 0] =", arr2[0, 0])   # Row 0, Column 0 → 1
print("arr2[-1, -1] =", arr2[-1, -1]) # Last row, last column → 9

Note:

  • Prefer arr[i, j] over arr[i][j] (more efficient for Numpy arrays).

IV. Slicing: Extracting Subarrays

Slicing extracts subarrays using the syntax [start:end:step], where start is inclusive, end is exclusive, and step is the interval.

1. 1D Array Slicing

# arr1 = [1,2,3,4,5]
print("arr1[1:4] =", arr1[1:4])    # Elements 1 to 3 (exclusive end) → [2,3,4]
print("arr1[::2] =", arr1[::2])    # Step 2 → [1,3,5]
print("arr1[:3] =", arr1[:3])      # First 3 elements → [1,2,3]
print("arr1[::] =", arr1[::])      # Entire array

2. 2D Array Slicing

2D slicing requires separate row and column slicing:

# arr2 = [[1,2,3], [4,5,6], [7,8,9]]
print("arr2[1:3, 0:2] = \n", arr2[1:3, 0:2])  
# Output: [[4,5], [7,8]] (rows 1-2, columns 0-1)

print("arr2[:, 1] =", arr2[:, 1])  # All rows, column 1 → [2,5,8]
print("arr2[0:2, :] = \n", arr2[0:2, :])  # First 2 rows, all columns → [[1,2,3], [4,5,6]]

3. View vs. Copy

Numpy slicing returns a view (shares memory with the original array), so modifying the subarray affects the original:

sub_arr = arr2[1:3, 0:2]  # Subarray
sub_arr[0, 0] = 100       # Modify subarray
print("Modified original array:\n", arr2)  # Original array is also modified

Avoid Modifying Original Array:

Use .copy() to create an independent copy:

sub_arr = arr2[1:3, 0:2].copy()  # Create copy
sub_arr[0, 0] = 200              # Modify copy, original unchanged
print("Original array unchanged:\n", arr2)

V. Summary and Practice Recommendations

  • Shape: Understand array dimensions; use reshape() to adjust.
  • Indexing: 1D like lists; 2D requires row+column indices (supports negative indexing).
  • Slicing: Use [start:end:step] for subarrays; be cautious with view vs. copy behavior.

Practice: Create a 5x5 random array and:
1. Check its shape.
2. Access the element at row 3, column 4.
3. Slice the top-left 3x3 subarray.
4. Modify the subarray and observe if the original array changes.

Hands-on practice will help you master Numpy array operations quickly!

Xiaoye