Bird
Raised Fist0
ML Pythonml~10 mins

Anomaly detection basics in ML Python - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the correct library for anomaly detection.

ML Python
from sklearn.[1] import IsolationForest
Drag options to blanks, or click blank then click option'
Aensemble
Blinear_model
Ccluster
Dneighbors
Attempts:
3 left
💡 Hint
Common Mistakes
Importing IsolationForest from the wrong module causes errors.
Confusing ensemble with linear_model or cluster modules.
2fill in blank
medium

Complete the code to fit the IsolationForest model on data X.

ML Python
model = IsolationForest()
model.[1](X)
Drag options to blanks, or click blank then click option'
Afit
Bscore_samples
Ctransform
Dpredict
Attempts:
3 left
💡 Hint
Common Mistakes
Using predict before fitting causes errors.
Confusing fit with transform or score_samples.
3fill in blank
hard

Fix the error in the code to predict anomalies using the trained model.

ML Python
predictions = model.[1](X_test)
Drag options to blanks, or click blank then click option'
Afit
Bscore_samples
Cfit_predict
Dpredict
Attempts:
3 left
💡 Hint
Common Mistakes
Using fit or fit_predict on test data instead of predict.
Using score_samples which returns anomaly scores, not labels.
4fill in blank
hard

Fill both blanks to create a dictionary of anomaly scores for each sample in X.

ML Python
scores = {i: model.[1](X[i].reshape(1, -1))[0] for i in range(len(X)) if model.[2](X[i].reshape(1, -1))[0] == -1}
Drag options to blanks, or click blank then click option'
Ascore_samples
Bpredict
Cfit
Dtransform
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up predict and score_samples methods.
Using fit or transform which are not for scoring or predicting.
5fill in blank
hard

Fill all three blanks to train IsolationForest with 100 trees, predict anomalies on X_test, and count how many anomalies were found.

ML Python
model = IsolationForest(n_estimators=[1])
model.[2](X_train)
predictions = model.[3](X_test)
anomaly_count = sum(predictions == -1)
Drag options to blanks, or click blank then click option'
Afit
Bpredict
C100
Dscore_samples
Attempts:
3 left
💡 Hint
Common Mistakes
Using score_samples instead of predict for labels.
Not setting n_estimators or using wrong values.
Confusing fit and predict order.

Practice

(1/5)
1. What is the main goal of anomaly detection in machine learning?
easy
A. To reduce the number of features in the data
B. To increase the size of the training dataset
C. To find unusual data points that differ from normal patterns
D. To cluster data points into groups

Solution

  1. Step 1: Understand anomaly detection purpose

    Anomaly detection aims to identify data points that do not fit the expected pattern.
  2. Step 2: Compare options with definition

    Only To find unusual data points that differ from normal patterns describes finding unusual data points, which matches the goal.
  3. Final Answer:

    To find unusual data points that differ from normal patterns -> Option C
  4. Quick Check:

    Anomaly detection = find unusual points [OK]
Hint: Anomaly detection finds data points that stand out [OK]
Common Mistakes:
  • Confusing anomaly detection with clustering
  • Thinking it reduces features
  • Assuming it increases dataset size
2. Which of the following is the correct way to import the IsolationForest model from scikit-learn in Python?
easy
A. from sklearn.ensemble import IsolationForest
B. import IsolationForest from sklearn
C. from sklearn.isolation import Forest
D. import sklearn.IsolationForest

Solution

  1. Step 1: Recall correct import syntax for IsolationForest

    The IsolationForest model is in sklearn.ensemble module and imported as shown in from sklearn.ensemble import IsolationForest.
  2. Step 2: Check other options for syntax errors

    Options A, C, and D have incorrect syntax or wrong module names.
  3. Final Answer:

    from sklearn.ensemble import IsolationForest -> Option A
  4. Quick Check:

    Correct import = from sklearn.ensemble import IsolationForest [OK]
Hint: Remember sklearn.ensemble holds IsolationForest [OK]
Common Mistakes:
  • Using wrong module name like sklearn.isolation
  • Incorrect import syntax like 'import X from Y'
  • Trying to import directly from sklearn without submodule
3. Given the following Python code using IsolationForest, what will be the output of print(predictions)?
from sklearn.ensemble import IsolationForest
import numpy as np

X_train = np.array([[1], [2], [3], [4], [5]])
model = IsolationForest(random_state=42)
model.fit(X_train)

X_test = np.array([[1], [10], [3]])
predictions = model.predict(X_test)
print(predictions)
medium
A. [0 1 0]
B. [-1 -1 -1]
C. [1 1 1]
D. [ 1 -1 1]

Solution

  1. Step 1: Understand IsolationForest prediction output

    IsolationForest predicts 1 for normal points and -1 for anomalies.
  2. Step 2: Analyze test points relative to training data

    Points 1 and 3 are close to training data, so predicted as normal (1). Point 10 is far, predicted as anomaly (-1).
  3. Final Answer:

    [ 1 -1 1] -> Option D
  4. Quick Check:

    Close points = 1, far point = -1 [OK]
Hint: Normal points get 1, anomalies get -1 in IsolationForest [OK]
Common Mistakes:
  • Assuming all predictions are 1
  • Confusing -1 with 0 for anomalies
  • Ignoring distance of test points from training data
4. The following code throws an error. What is the most likely cause?
from sklearn.ensemble import IsolationForest
model = IsolationForest()
model.predict([[1], [2], [3]])
medium
A. The input data format is incorrect
B. The model was not trained before prediction
C. IsolationForest cannot predict on lists
D. Missing random_state parameter

Solution

  1. Step 1: Check if model is trained before prediction

    The code calls predict without calling fit first, so the model is not trained.
  2. Step 2: Understand IsolationForest requirements

    Model must be trained with fit() before predict() to avoid error.
  3. Final Answer:

    The model was not trained before prediction -> Option B
  4. Quick Check:

    Call fit() before predict() [OK]
Hint: Always train model with fit() before predict() [OK]
Common Mistakes:
  • Trying to predict without training
  • Thinking input format causes error here
  • Assuming random_state is mandatory
5. You have a dataset with mostly normal data and a few anomalies. You want to detect anomalies using IsolationForest. Which approach is best to prepare your model?
hard
A. Train IsolationForest only on normal data, then predict on all data
B. Train IsolationForest on all data including anomalies
C. Train IsolationForest only on anomalies
D. Train IsolationForest on random subsets ignoring labels

Solution

  1. Step 1: Understand IsolationForest training strategy

    IsolationForest is an unsupervised algorithm designed to be trained on data that mostly contains normal points. Training only on normal data helps the model learn the normal pattern and then detect anomalies during prediction.
  2. Step 2: Evaluate options for training data

    Training only on normal data, then predicting on all data (D) is the best approach. Training on all data including anomalies (B) can reduce detection accuracy as anomalies influence the model.
  3. Final Answer:

    Train IsolationForest only on normal data, then predict on all data -> Option A
  4. Quick Check:

    Train on normal data only [OK]
Hint: Train IsolationForest on normal data only for better anomaly detection [OK]
Common Mistakes:
  • Training on all data including anomalies
  • Training only on anomalies
  • Ignoring the importance of training data quality
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