CodeBlanks Logo Icon CodeBlanks

Summary - NumPy basics

In this group you learned to create NumPy arrays, use their properties, and index them.

Summary: NumPy basics

What you learned in this group:

  • Creating arrays with np.array, arange, linspace, zeros.
  • Inspecting arrays with shape, ndim, size, dtype.
  • Selecting with a[row, column], slices and boolean masks.
  • Changing shape with reshape, flatten, .T.
X = np.arange(12).reshape(4, 3)
print(X[:, 0])          # first feature
print(X[X[:, 0] > 3])   # rows whose first feature is above 3

Element-wise operations and vectorisation are the key to fast, short ML code.

Practice All together

Turn the numbers 0-11 into a 4×3 matrix. Select the last column, filter the values above 5 with a boolean mask and print them.

Starter code

model.py
import numpy as np

# buraya yaz

Sample solution

model.py
import numpy as np

X = np.arange(12).reshape(4, 3)
son = X[:, -1]
print(son[son > 5])

Expected output

[ 8 11]

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does <code>np.arange(12).reshape(4, 3)[:, 0]</code> give?

  • 0
  • [0 3 6 9] Correct answer
  • [0 1 2]
  • [0 4 8]

Explanation: The rows are [0 1 2], [3 4 5], [6 7 8], [9 10 11]; the first element of each is taken.

2 Compute the sum of all elements.

Question
toplam = X.____()
Answer
toplam = X.sum()

Explanation: sum adds all elements when no axis is given.

3 Match the tools with their roles.

Term Meaning
linspace Evenly spaced values
astype Type conversion
reshape Changing shape
x[x > 0] Conditional selection

All lessons in Beginner

13 Project: Classification

  1. Preparing data
  2. Training model
  3. Prediction
  4. Evaluation