NumPy: lesson 2 of 3

NumPy

PATH 01MODULE 03LESSON 02 OF 03Next: Vectorization, Aggregations, and Broadcasting

Indexing, Slicing, Shapes, and Reshaping

Select, filter, and reorganize NumPy arrays while keeping observation and feature dimensions clear.

Beginner15 min readnumpyindexingslicingreshapingfeature-matrix

Concept

Indexing selects individual values, slicing selects a range, and reshaping reorganizes an array without changing its values. These operations are essential because Data Science rarely uses every value in exactly the original arrangement.

Why It Matters

You may need high-scoring predictions, the first week of temperature readings, one feature column, or a matrix ready for a model. Accurate selection and shape awareness prevent accidental use of the wrong rows, columns, or dimensions.

Intuition

An index is an address. A slice is a range of addresses. Shape is the layout of the array: for a feature matrix, rows usually mean observations and columns mean features.

One-Dimensional Indexing and Slicing

NumPy uses zero-based indexing, so the first value has index 0. Negative indexes count from the end.

scores = np.array([72, 91, 65, 88, 95])

print(scores[0])
print(scores[-1])
print(scores[1:4])
print(scores[:3])

Expected output:

72
95
[91 65 88]
[72 91 65]

The end of a slice is excluded. scores[1:4] starts at index 1 and stops before index 4.

Boolean Indexing / Filtering

A comparison produces a Boolean array that can select matching values.

high_scores = scores[scores >= 80]
print(high_scores)

Expected output:

[91 88 95]

This is filtering-like logic without a Python loop. The Boolean condition must line up with the array being filtered.

Two-Dimensional Arrays: Rows and Columns

Consider a small feature matrix where each row is a customer and each column is a feature.

features = np.array([
    [24, 42000],
    [31, 55000],
    [45, 68000],
])

print(features.shape)
print(features[0, 1])
print(features[:, 0])
print(features[1:, :])

Expected output:

(3, 2)
42000
[24 31 45]
[[   31 55000]
 [   45 68000]]

features[row, column] selects one value. : means “all” along that dimension, so features[:, 0] selects the first column: every customer's age. features[1:, :] selects rows from the second observation onward and all columns.

Shape and reshape()

The shape (3, 2) means three rows and two columns. You can reshape only when the total element count stays the same.

monthly_sales = np.array([1200, 1500, 1800, 1100, 1400, 1600])
weekly_sales = monthly_sales.reshape(2, 3)

print(weekly_sales)
print(weekly_sales.shape)

Expected output:

[[1200 1500 1800]
 [1100 1400 1600]]
(2, 3)

Six values can become (2, 3) or (3, 2), but not (4, 2). Attempting an impossible shape raises an error because eight positions cannot be filled by six values.

Flattening

flatten() returns a one-dimensional copy of the values.

flat_sales = weekly_sales.flatten()
print(flat_sales)

Expected output: [1200 1500 1800 1100 1400 1600]. Flattening can be useful before an operation that expects a single vector, but do not flatten a feature matrix merely to avoid understanding its dimensions.

Real-World / Data Science Example

Suppose a model input contains customer age and monthly spend. The first column can be checked for implausible ages.

customer_features = np.array([
    [22, 120.0],
    [35, 210.0],
    [17, 75.0],
])

ages = customer_features[:, 0]
adult_features = customer_features[ages >= 18]
print(adult_features)

Expected output:

[[ 22. 120.]
 [ 35. 210.]]

The first selection creates a condition from the age column; the second applies that condition to complete rows, keeping age and spend aligned.

Code Explanation

Commas separate dimensions in a 2D index. A Boolean mask is an array of True and False values, one per row or element being selected. reshape() changes only the arrangement, not the values, so always verify the new shape still represents a meaningful structure.

When to Use It

Use indexing for individual values or known positions, slicing for contiguous ranges, Boolean indexing for conditions, and reshape for a valid new representation of the same data. Use shape checks before passing arrays into a model or combining arrays.

Failure Signals

Common Mistakes

  1. Forgetting that indexes start at zero.
  2. Expecting a slice end index to be included.
  3. Mixing up rows and columns in array[row, column].
  4. Applying a row-sized Boolean mask to a differently sized array.
  5. Attempting a reshape with a different total number of elements.

Best Practices

Print shape after selections and reshapes. State in a comment or variable name what rows and columns mean. Use Boolean masks to preserve whole observations when filtering on one feature, and check that the selected columns are in the expected order before modeling.

Data Science Perspective

The row-observation, column-feature convention is central to machine learning and tabular data. Pandas uses labels more often, but NumPy indexing teaches the underlying matrix thinking used in feature engineering, preprocessing, and model predictions.

Interview Perspective

Question: What does shape (100, 5) usually mean for a feature matrix? A strong answer: 100 observations or samples, each described by 5 features.

Practice Questions

  1. Select the last two values from np.array([4, 8, 15, 16, 23]).
  2. Select only model probabilities above 0.7 from a NumPy array.
  3. Reshape 12 monthly values into a (3, 4) matrix and explain what each dimension could represent.

Quick Quiz

  1. What does array[:, 1] select from a 2D array? Answer: all rows from the second column.
  2. Can an array with six values become shape (4, 2)? Answer: no.
  3. What does a Boolean mask contain? Answer: True and False values used for selection.

Key Takeaway

Key Takeaways

Indexing and slicing select data precisely, while shape and reshape describe its layout. Treat rows and columns as meaningful Data Science concepts, not just positions.

Next Lesson

Next, use vectorization, aggregations, and broadcasting to calculate across arrays cleanly.

Finish this lesson on your terms

Mark it complete when you have worked through the material and are ready to move on.