Bird
Raised Fist0
NumPydata~20 mins

NumPy with machine learning libraries - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
NumPy ML Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of NumPy array shape after sklearn train_test_split
What is the shape of X_train after running the following code?
NumPy
import numpy as np
from sklearn.model_selection import train_test_split

X = np.arange(20).reshape(10, 2)
y = np.arange(10)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
print(X_train.shape)
A(7, 2)
B(3, 2)
C(10, 2)
D(14, 2)
Attempts:
2 left
💡 Hint
Remember test_size=0.3 means 30% data is for testing, rest for training.
❓ data_output
intermediate
2:00remaining
Result of NumPy array after StandardScaler transform
What is the output array after applying StandardScaler to the data below?
NumPy
import numpy as np
from sklearn.preprocessing import StandardScaler

data = np.array([[1, 2], [3, 4], [5, 6]])
scaler = StandardScaler()
scaled_data = scaler.fit_transform(data)
print(np.round(scaled_data, 2))
A
[[1. 1.]
 [3. 4.]
 [5. 6.]]
B
[[-1.22 -1.22]
 [ 0.   0.  ]
 [ 1.22  1.22]]
C
[[0. 0.]
 [0. 0.]
 [0. 0.]]
D
[[-1.  -1.]
 [ 0.   0.]
 [ 1.   1.]]
Attempts:
2 left
💡 Hint
StandardScaler centers data to mean 0 and scales to unit variance.
🔧 Debug
advanced
2:00remaining
Identify the error when using NumPy array with sklearn LinearRegression
What error will this code raise when fitting the model?
NumPy
import numpy as np
from sklearn.linear_model import LinearRegression

X = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 6, 8, 10])
model = LinearRegression()
model.fit(X, y)
AValueError: Expected 2D array, got 1D array instead
BTypeError: unsupported operand type(s) for +: 'int' and 'str'
CNo error, model fits successfully
DIndexError: index out of bounds
Attempts:
2 left
💡 Hint
Check the shape of X passed to fit method.
❓ visualization
advanced
2:00remaining
Interpret the plot of PCA components from NumPy data
Given the PCA plot below, which statement is true about the data?
NumPy
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA

np.random.seed(0)
data = np.dot(np.random.rand(2, 2), np.random.randn(2, 200)).T
pca = PCA(n_components=2)
components = pca.fit_transform(data)
plt.scatter(components[:, 0], components[:, 1])
plt.xlabel('PC1')
plt.ylabel('PC2')
plt.title('PCA of Data')
plt.show()
APCA components are identical to original features.
BThe data has no variance along the first principal component.
CThe first principal component explains more variance than the second.
DThe second principal component explains more variance than the first.
Attempts:
2 left
💡 Hint
PCA orders components by explained variance descending.
🧠 Conceptual
expert
2:00remaining
Why use NumPy arrays with scikit-learn instead of Python lists?
Which is the main reason scikit-learn prefers NumPy arrays over Python lists for input data?
ANumPy arrays automatically normalize data before training.
BPython lists cannot store numerical data.
CPython lists are immutable and cannot be changed.
DNumPy arrays provide efficient memory usage and fast numerical operations required by scikit-learn.
Attempts:
2 left
💡 Hint
Think about performance and data structure requirements.

Practice

(1/5)
1. Why do machine learning libraries often use NumPy arrays for data input?
easy
A. Because NumPy arrays can only store text data
B. Because NumPy arrays are fast and easy to manipulate for numerical data
C. Because NumPy arrays automatically train machine learning models
D. Because NumPy arrays are the only data type in Python

Solution

  1. Step 1: Understand NumPy array properties

    NumPy arrays store numerical data efficiently and support fast operations.
  2. Step 2: Connect to machine learning needs

    Machine learning needs fast numerical data processing, which NumPy arrays provide.
  3. Final Answer:

    Because NumPy arrays are fast and easy to manipulate for numerical data -> Option B
  4. Quick Check:

    NumPy arrays speed up numerical data handling = A [OK]
Hint: Remember: ML needs fast number crunching, NumPy fits well [OK]
Common Mistakes:
  • Thinking NumPy arrays store only text
  • Believing NumPy trains models automatically
  • Assuming Python has only one data type
2. Which of the following is the correct way to convert a Python list [1, 2, 3] into a NumPy array?
easy
A. np.toarray([1, 2, 3])
B. np.list([1, 2, 3])
C. np.convert([1, 2, 3])
D. np.array([1, 2, 3])

Solution

  1. Step 1: Recall NumPy array creation syntax

    The function to create arrays from lists is np.array().
  2. Step 2: Check each option

    Only np.array([1, 2, 3]) is valid syntax; others are not NumPy functions.
  3. Final Answer:

    np.array([1, 2, 3]) -> Option D
  4. Quick Check:

    Use np.array() to convert lists = A [OK]
Hint: Use np.array() to convert lists to arrays [OK]
Common Mistakes:
  • Using non-existent functions like np.list()
  • Confusing array creation with conversion functions
  • Forgetting to import NumPy as np
3. What will be the output of this code snippet?
import numpy as np
from sklearn.preprocessing import StandardScaler

X = np.array([[1, 2], [3, 4], [5, 6]])
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
print(np.round(X_scaled, 2))
medium
A. [[-1.22 -1.22] [ 0. 0. ] [ 1.22 1.22]]
B. [[1 2] [3 4] [5 6]]
C. [[0. 0.] [0. 0.] [0. 0.]]
D. Error: StandardScaler not imported

Solution

  1. Step 1: Understand StandardScaler effect

    StandardScaler centers data to mean 0 and scales to unit variance.
  2. Step 2: Calculate scaled values

    For each column, mean is 3 and 4; std dev approx 1.63. Values transform to approx -1.22, 0, 1.22.
  3. Final Answer:

    [[-1.22 -1.22] [ 0. 0. ] [ 1.22 1.22]] -> Option A
  4. Quick Check:

    StandardScaler centers and scales data = D [OK]
Hint: StandardScaler centers data to mean 0 and scales variance 1 [OK]
Common Mistakes:
  • Expecting original data output
  • Confusing StandardScaler with MinMaxScaler
  • Missing import causing runtime error
4. Identify the error in this code that uses NumPy with scikit-learn:
import numpy as np
from sklearn.linear_model import LinearRegression

X = [[1, 2], [3, 4], [5, 6]]
y = [1, 2, 3]
model = LinearRegression()
model.fit(X, y)
print(model.coef_)
medium
A. X should be a NumPy array, not a list
B. y should be a 2D array, not a list
C. LinearRegression requires data scaling before fitting
D. model.fit() is missing required parameters

Solution

  1. Step 1: Check data types for model.fit()

    scikit-learn expects NumPy arrays or similar, not plain Python lists.
  2. Step 2: Identify the issue in X

    X is a list of lists; converting to np.array is needed for proper fitting.
  3. Final Answer:

    X should be a NumPy array, not a list -> Option A
  4. Quick Check:

    Use NumPy arrays for input data = B [OK]
Hint: Always convert input data to NumPy arrays before fitting models [OK]
Common Mistakes:
  • Thinking y must be 2D array
  • Believing scaling is mandatory before fitting
  • Assuming fit() needs extra parameters
5. You have a dataset as a Python list of lists representing features. You want to prepare it for a machine learning model that requires normalized NumPy arrays. Which steps should you perform?
hard
A. Convert the list to a Python tuple, then normalize manually with loops
B. Directly pass the list to the model without conversion or scaling
C. Convert the list to a NumPy array, then use a scaler like MinMaxScaler to normalize
D. Use pandas DataFrame without converting to NumPy arrays

Solution

  1. Step 1: Convert list to NumPy array

    Machine learning models expect NumPy arrays for efficient computation.
  2. Step 2: Normalize data using scaler

    Use MinMaxScaler or similar to scale features between 0 and 1 for better model performance.
  3. Final Answer:

    Convert the list to a NumPy array, then use a scaler like MinMaxScaler to normalize -> Option C
  4. Quick Check:

    Convert then scale data before modeling = C [OK]
Hint: Always convert data to NumPy arrays and scale before modeling [OK]
Common Mistakes:
  • Passing raw lists directly to models
  • Using tuples which are not efficient for ML
  • Skipping normalization hurting model accuracy
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard