Module 2 · Beginner · Before you start: the Python and maths you need

Are you ready for machine learning? A self-check

Machine learning readiness self-check: 25 questions on Python, NumPy, pandas, functions, Σ, MSE, vectors, derivatives and probability, each linked to a lesson.

By theprogrammers team · · 11 min read

Before a long train ride, you check you have your ticket, ID and charger. You don't need to pack your whole house, but forgetting one small thing can ruin the trip. Machine learning is the same: you don't need a maths degree, but a few specific Python and maths skills come up in almost every lesson.

This machine learning readiness self-check has 25 short questions on exactly those skills. Each one links to the lesson that teaches it. Answer honestly before opening the hidden answer, keep a tally, and you'll know which gaps to fill before they slow you down. Most questions ask you to predict what code prints, because that's the skill you'll use constantly: reading code and knowing what it does.

What you need first: nothing. If a question stumps you, the link next to it is where to go.

How to use this check

  • Write your answer down first, then open the answer. Guessing after you've peeked doesn't count.
  • Run the code if you're unsure. Every code block runs as written in Python with NumPy and pandas installed. If you haven't installed Python yet, follow set up a machine learning environment.
  • Count a question as right only if you got it right and could explain why.
  • At the end, the score table tells you what to do next.

Part 1: Python (5 questions)

1. Lists. What does this print?

marks = [72, 85, 64, 90]
marks.append(58)
print(len(marks), marks[0], marks[-1])
Show the answer
5 72 58

append adds 58 to the end, so there are 5 items. marks[0] is the first item and marks[-1] is the last. Revise: Python lists tutorial.

2. Functions. What does this print?

def percent(score, total=100):
    return round(score / total * 100, 1)

print(percent(45, 60), percent(80))
Show the answer
75.0 80.0

45 out of 60 is 75%. In the second call, total isn't given, so it uses its default, 100. Revise: Python functions explained.

3. Loops and conditions. What does this print?

count = 0
for mark in [35, 72, 40, 28, 91]:
    if mark >= 40:
        count += 1
print(count)
Show the answer
3

72, 40 and 91 are 40 or more. 40 counts because the test is >=, not >. Revise: Python for and while loops and if, elif and else.

4. Dictionaries. What does this print?

prices = {"chai": 15, "samosa": 20}
prices["vada pav"] = 25
print(sum(prices.values()), "samosa" in prices)
Show the answer
60 True

Assigning to a new key adds it. values() gives 15, 20 and 25, which sum to 60. in checks the keys. Revise: Python dictionaries.

5. List comprehensions. Using marks from question 1, what does this print?

print([m * 2 for m in marks if m > 60])
Show the answer
[144, 170, 128, 180]

It keeps only marks above 60 (72, 85, 64, 90; 58 is dropped) and doubles each one. Revise: Python comprehensions.

Part 2: NumPy (4 questions)

6. Arrays do maths on every item. What does this print?

import numpy as np

a = np.array([2, 4, 6])
print(a * 3, a.sum(), a.mean())
Show the answer
[ 6 12 18] 12 4.0

a * 3 multiplies every item, with no loop. This is called vectorisation, and machine learning code relies on it everywhere. Revise: NumPy array tutorial.

7. Shape and indexing. What does this print?

grid = np.arange(12).reshape(3, 4)
print(grid.shape, grid[1, 2], grid[:, 0])
Show the answer
(3, 4) 6 [0 4 8]

arange(12) makes 0 to 11, and reshape(3, 4) folds them into 3 rows of 4. Row 1 is [4 5 6 7], so grid[1, 2] is 6. grid[:, 0] means "every row, column 0". Revise: NumPy reshape and NumPy indexing and slicing.

8. Boolean masks. What does this print?

temps = np.array([31, 38, 42, 29, 44])
print(temps[temps > 35], (temps > 35).mean())
Show the answer
[38 42 44] 0.6

temps > 35 gives True or False for each item. Using it inside the brackets keeps the True ones. The mean of True/False values is the share that are True (3 out of 5), because True counts as 1. You'll use this trick to compute accuracy. Revise: NumPy boolean indexing.

9. The axis argument. What does this print?

scores = np.array([[70, 80], [90, 60], [50, 100]])
print(scores.mean(axis=0), scores.mean(axis=1))
Show the answer
[70. 80.] [75. 75. 75.]

axis=0 goes down the rows, giving one mean per column: (70 + 90 + 50) ÷ 3 = 70 and (80 + 60 + 100) ÷ 3 = 80. axis=1 goes across each row, giving one mean per row. In a dataset, axis=0 gives the mean of each feature. Revise: NumPy array tutorial.

Part 3: pandas (4 questions)

10. Filtering rows. What does this print?

import pandas as pd

flats = pd.DataFrame({
    "city": ["Pune", "Delhi", "Pune", "Chennai"],
    "rent": [18000, 25000, 22000, 15000],
})
print(flats.shape)
print(flats[flats["rent"] > 16000]["city"].tolist())
Show the answer
(4, 2)
['Pune', 'Delhi', 'Pune']

4 rows and 2 columns. The filter keeps rows with rent above ₹16,000, in their original order, then takes the city column. Revise: filter rows in pandas.

11. Group and summarise. What does this print?

print(flats.groupby("city")["rent"].mean())
Show the answer
city
Chennai    15000.0
Delhi      25000.0
Pune       20000.0
Name: rent, dtype: float64

groupby("city") puts the rows for each city together, and .mean() averages the rent within each group. The two Pune flats average (18,000 + 22,000) ÷ 2 = 20,000. Groups come out in alphabetical order. Revise: summary statistics in pandas.

12. Missing values. What does this print?

flats.loc[1, "rent"] = None
print(flats["rent"].isna().sum())
flats["rent"] = flats["rent"].fillna(flats["rent"].median())
print(flats["rent"].tolist())
Show the answer
1
[18000.0, 18000.0, 22000.0, 15000.0]

One value is now missing. The median of the remaining three (15,000, 18,000, 22,000) is 18,000, and fillna puts it in the gap. The numbers became decimals because pandas stores a missing value as NaN, which is a float. Revise: handle missing values in pandas.

13. Columns. You have a DataFrame df with a column "price" you want to predict. Write one line for X (every other column) and one for y.

Show the answer

X = df.drop(columns="price") and y = df["price"]. drop returns a new table without that column; square brackets select one column. Revise: data, features and labels and select columns in pandas.

Part 4: Functions, graphs and formulas (5 questions)

14. A function. If f(x) = 2*x* + 3, what is f(4)? What is f(0)?

Show the answer

f(4) = 2 × 4 + 3 = 11, and f(0) = 3. A function takes an input and applies a rule; every model is a function from features to a prediction. Revise: what is a function?

15. Slope and intercept. An auto fare is ₹30 plus ₹15 per km. Write it as a formula. What do the slope and intercept mean?

Show the answer

fare = 15 × km + 30. The slope, 15, is how much the fare goes up for each extra km. The intercept, 30, is the fare at 0 km (the base fare). Linear regression learns exactly these two kinds of number from data. Revise: slope and intercept.

16. Summation. Work out Σ i² for i = 1 to 4 (read: "the sum of i squared, for i from 1 to 4"), then check with Python.

Show the answer

1 + 4 + 9 + 16 = 30.

print(sum(i ** 2 for i in range(1, 5)))
30

range(1, 5) stops before 5. Revise: summation notation.

17. Mean squared error. A model predicts 3, 5 and 2. The true values are 4, 5 and 4. Work out the mean squared error by hand, then with NumPy.

Show the answer

Errors (prediction − truth): −1, 0, −2. Squared: 1, 0, 4. Mean: 5 ÷ 3 ≈ 1.67.

predicted = np.array([3, 5, 2])
actual = np.array([4, 5, 4])
print(round(float(np.mean((predicted - actual) ** 2)), 2))
1.67

Squaring makes every error positive and punishes big errors more. Revise: how to read the MSE formula.

18. Reading a graph. On a graph of loss (up) against a weight w (across), the curve is a U shape with its lowest point at w = 2. If the current weight is 5, should w go up or down to reduce the loss?

Show the answer

Down, towards 2. At w = 5 you're on the right-hand side of the U, where the curve slopes upwards, so moving left lowers the loss. Training a model is this same move, repeated. Revise: how to read graphs.

Part 5: Vectors and derivatives (4 questions)

19. Vectors. What are [1, 2] + [3, 4] and 3 × [1, 2] as vectors? Check with NumPy.

Show the answer

[4, 6] and [3, 6]: add matching items, and multiply every item by 3.

print(np.array([1, 2]) + np.array([3, 4]), 3 * np.array([1, 2]))
[4 6] [3 6]

Each row of a dataset is a vector of features. Revise: what is a vector? and vector addition.

20. The dot product. Two features are [3, 2] (area in 100s of sq ft, rooms) and the weights are [10, 5]. What is the dot product, and what does it mean here?

Show the answer

3 × 10 + 2 × 5 = 40. Multiply matching items and add. It's a weighted sum of the features, which is exactly how a linear model makes a prediction (before adding the intercept).

print(np.dot([3, 2], [10, 5]))
40

Revise: the dot product explained.

21. Matrix times vector. What does this print?

M = np.array([[1, 2], [3, 4]])
v = np.array([1, 1])
print(M @ v)
Show the answer
[3 7]

Each row of the matrix is dotted with the vector: 1 + 2 = 3 and 3 + 4 = 7. With a whole dataset as the matrix and the weights as the vector, @ gives a prediction for every row at once. Revise: matrix-vector multiplication.

22. Derivatives. What is the derivative of x² at x = 3, and what does the number mean?

Show the answer

The derivative of x² is 2*x*, so at x = 3 it's 6. It means that near x = 3, the curve rises about 6 units for each 1 unit you move right: it's the slope there. A quick numerical check, using a tiny step:

f = lambda x: x ** 2
h = 0.0001
print(round((f(3 + h) - f(3)) / h, 3))
6.0

Models use the derivative of the loss to know which way to change each weight. Revise: what is a derivative?

Part 6: Statistics and probability (3 questions)

23. Mean or median? Five friends earn ₹10k, ₹12k, ₹11k, ₹13k and ₹54k a month. Find the mean and median. Which better describes a typical friend?

Show the answer

Mean = 100 ÷ 5 = ₹20k. Median (middle value when sorted: 10, 11, 12, 13, 54) = ₹12k. The median is more typical: the one high earner pulls the mean up. Revise: mean, median and mode.

24. Standard deviation. Two classes both average 60 marks. Class A has a standard deviation of 5 and class B of 20. What's different about them?

Show the answer

In class A, most marks are close to 60 (roughly 55 to 65). In class B, marks are spread widely (roughly 40 to 80). Standard deviation measures the typical distance from the mean. Revise: variance and standard deviation.

25. Conditional probability. Of 100 emails, 30 are spam. The word "free" appears in 20 of the spam emails and in 5 of the others. If an email contains "free", what's the probability it's spam?

Show the answer

25 emails contain "free", and 20 of them are spam, so the probability is 20 ÷ 25 = 0.8. You only look at the emails that contain the word: that's what "given" means in conditional probability. This idea is the heart of the Naive Bayes spam filter. Revise: conditional probability.

Your score: what to do next

ScoreWhat it meansWhat to do
22 to 25You're readyStart the data preparation module, and revise any question you missed
15 to 21Mostly readyRevise the lessons linked from the questions you missed, then start
8 to 14Some gapsSpend a week on the Python, NumPy and pandas lessons before starting machine learning
0 to 7Start a step earlierBegin with Python for beginners and maths for data science from zero

Also look at which part you missed. Parts 1 to 3 matter from the very first lesson. Parts 4 to 6 matter from the "How models learn" module onwards, and each later lesson links back to the maths it uses, so you can fill those gaps as you go.

Key takeaways

  • Machine learning needs a specific, small set of skills, not a maths degree.
  • Python lists, functions, loops and dictionaries, NumPy arrays and pandas DataFrames are needed from day one.
  • Functions, slopes, Σ, MSE, vectors, the dot product and derivatives explain how models learn.
  • Mean, median, standard deviation and conditional probability explain how models are judged and why some work.
  • Fill gaps with the linked lessons; revisit this check later to see your progress.

Frequently asked questions

How much maths do I need for machine learning?

For using and understanding classic algorithms: school algebra, functions and graphs, a little statistics and probability, vectors and the dot product, and the idea of a derivative. Deeper linear algebra and calculus help with neural networks and research, and this course teaches them just before they're needed. Which maths do you need for machine learning? goes into detail.

Can I start machine learning if I scored low on the maths parts?

Yes, if your Python, NumPy and pandas are solid. The early lessons need very little maths, and every lesson links the maths it uses. Fill the maths gaps as each one comes up, rather than trying to learn all of it first.

Should I learn Python or maths first?

Python first. You'll check every maths idea in Python, and code turns formulas into something you can see and test. Most students find maths easier once they can run it.

What to learn next

  1. Scikit-learn tutorial: fit, predict, transform: the first lesson of the data preparation module.
  2. Which maths do you need for machine learning?: a fuller map of the maths.
  3. Gradient descent explained: see the derivative questions put to work.