Python ML Libraries Questions with Answers

8 practice questions on Python ML Libraries (AI & Machine Learning) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

Sample questions

Question 1Easy

What does this code print?

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.shape)
  1. (2,)
  2. 6
  3. (3, 2)
  4. (2, 3)

Answer: D. (2, 3)

Explanation

The array has 2 rows and 3 columns; shape is a (rows, columns) tuple.

Step-by-step solution

  1. The array has 2 inner lists (rows).
  2. Each row has 3 elements (columns).
  3. shape = (rows, columns) = (2, 3).

Why the other options are wrong

  • A. (2,) would be a 1-D array of two elements.
  • B. 6 is a.size, not the shape.
  • C. (3, 2) swaps rows and columns.

Question 2Easy

What does this code print?

from sklearn.metrics import accuracy_score
print(accuracy_score([1, 0, 1, 1, 0], [1, 1, 1, 0, 0]))
  1. 0.8
  2. 0.4
  3. 3
  4. 0.6

Answer: D. 0.6

Explanation

Positions 1, 3 and 5 match (3 of 5), so accuracy = 3 / 5 = 0.6.

Step-by-step solution

  1. Compare true and predicted values position by position.
  2. Matches: positions 1, 3 and 5; mismatches: positions 2 and 4.
  3. Accuracy = 3 / 5 = 0.6.

Why the other options are wrong

  • A. 0.8 would need 4 matches.
  • B. 0.4 is the error rate (2 / 5).
  • C. 3 is the count of matches; accuracy_score returns a fraction by default.

Question 3Medium

What does this code print?

import pandas as pd
df = pd.DataFrame({'team': ['A', 'B', 'A', 'B'], 'pts': [10, 20, 30, 40]})
print(df.groupby('team')['pts'].mean()['A'])
  1. 10.0
  2. 20.0
  3. 30.0
  4. 25.0

Answer: B. 20.0

Explanation

Team A's rows have points 10 and 30, whose mean is 20.0 (mean returns a float).

Step-by-step solution

  1. Team A rows have pts 10 and 30.
  2. Mean = (10 + 30) / 2 = 20.0.
  3. groupby gives a Series indexed by team; ['A'] picks 20.0.

Why the other options are wrong

  • A. 10.0 is A's first value only.
  • C. 30.0 is team B's mean, not A's.
  • D. 25.0 is the mean of all four rows.

Question 4Medium

What does this code print?

import numpy as np
a = np.arange(12).reshape(3, 4)
print(a.sum(axis=0))
  1. 66
  2. [0 4 8]
  3. [ 6 22 38]
  4. [12 15 18 21]

Answer: D. [12 15 18 21]

Explanation

axis=0 sums down the rows, giving one total per column: 0+4+8 = 12, 1+5+9 = 15, 2+6+10 = 18, 3+7+11 = 21.

Step-by-step solution

  1. a = [[0,1,2,3],[4,5,6,7],[8,9,10,11]].
  2. axis=0 sums down the rows, giving one total per column.
  3. Columns: 0+4+8 = 12, 1+5+9 = 15, 2+6+10 = 18, 3+7+11 = 21.

Why the other options are wrong

  • A. 66 is the total sum with no axis.
  • B. [0 4 8] is the first column, not a sum.
  • C. [6 22 38] is the row sums, axis=1.

Tip: axis=0 -> down the columns; axis=1 -> across the rows.

Question 5Medium

What does this code print?

import numpy as np
a = np.array([1, 2, 3])
b = np.array([[10], [20]])
print((a + b).shape)
  1. Error: shapes do not match
  2. (2, 3)
  3. (3,)
  4. (2, 1)

Answer: B. (2, 3)

Explanation

Broadcasting stretches a (shape (3,)) and b (shape (2, 1)) to a common shape (2, 3).

Step-by-step solution

  1. a has shape (3,), treated as (1, 3); b has shape (2, 1).
  2. Broadcasting stretches size-1 dimensions: (1, 3) and (2, 1) both become (2, 3).
  3. Result shape (2, 3): [[11, 12, 13], [21, 22, 23]].

Why the other options are wrong

  • A. Broadcasting allows this because one side is size 1 in each mismatched dimension.
  • C. The result has the extra row dimension from b.
  • D. (2, 1) is b's shape alone.

Tip: Align shapes on the right; 1 stretches to match.

Question 6Medium

In scikit-learn, what is the correct way to apply a StandardScaler to training and test data?

  1. scaler.fit_transform(X_train), then scaler.transform(X_test)
  2. scaler.fit_transform(X_test), then scaler.transform(X_train)
  3. scaler.transform(X_train) without fitting
  4. scaler.fit_transform(X_train) and scaler.fit_transform(X_test) separately

Answer: A. scaler.fit_transform(X_train), then scaler.transform(X_test)

Explanation

Fit only on training data, then reuse the same mean and standard deviation for test data. Re-fitting on test data leaks information and makes the scales inconsistent.

Step-by-step solution

  1. Fit the scaler on training data to learn its mean and standard deviation.
  2. Transform the training data with fit_transform.
  3. Transform the test data with those same statistics using transform.

Why the other options are wrong

  • B. Fitting on test data leaks test information.
  • C. transform before fit raises an error because no statistics exist yet.
  • D. Fitting separately scales the two sets differently and leaks test statistics.

Tip: fit_transform(train), transform(test).

Question 7Medium

What does this code print?

import pandas as pd
df = pd.DataFrame({'a': [1, 2, None], 'b': [4, None, 6]})
print(df.isnull().sum().sum())
  1. 2
  2. 1
  3. 6
  4. 0

Answer: A. 2

Explanation

isnull() marks the two missing values; summing per column (1 + 1) and then overall gives 2.

Step-by-step solution

  1. Column a has one missing value (None).
  2. Column b has one missing value.
  3. isnull().sum() gives [1, 1]; .sum() again gives 2.

Why the other options are wrong

  • B. 1 counts only one column.
  • C. 6 is the total number of cells.
  • D. None in a numeric column becomes NaN, which counts as null.

Tip: df.isnull().sum() per column; .sum().sum() total.

Question 8Hard

What does this code print?

import pandas as pd
df = pd.DataFrame({'x': [10, 20, 30]}, index=[2, 0, 1])
print(df.loc[0, 'x'], df.iloc[0, 0])
  1. 10 20
  2. 10 10
  3. 20 20
  4. 20 10

Answer: D. 20 10

Explanation

loc selects by index label (label 0 is the row with 20); iloc selects by position (the first row holds 10).

Step-by-step solution

  1. loc uses index labels: label 0 is the second row, value 20.
  2. iloc uses positions: position 0 is the first row, value 10.
  3. Output: 20 10.

Why the other options are wrong

  • A. This swaps loc and iloc.
  • B. loc[0] looks up the label 0, not position 0.
  • C. iloc[0, 0] is the first row by position, 10.

Practise all 8 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all AI & Machine Learning topics · All topics