Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 39 ยท Projects and Practice

Practice Exercises

Practice classification, clustering, anomaly detection and text mining with six hands-on exercises, editable starter code, hints and worked solutions, plus a self-check quiz and a final checklist.

You learn data mining by doing it. Each exercise below states a task, gives a hint, and then a runnable solution in a code editor. Try to solve the task yourself first: write your own code in the editor (or on paper), run it, and only then compare with the reference solution. Change the numbers and see what happens.

Exercise 1: Metrics from counts

A medical test is applied to 1,000 patients. It finds 80 of 100 sick patients and wrongly flags 45 of 900 healthy ones. Compute accuracy, precision, recall, specificity and F1.

Hint: write the confusion matrix first (TP, FN, FP, TN).

Python Solution 1: confusion matrix metrics

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The test has high accuracy (93.5%) but a precision of only 64%: when a disease is rare (10% here), many positive results can still be false alarms. See classification basics.

Exercise 2: Pick the best tree depth

Using the breast cancer dataset, find the max_depth between 1 and 10 with the best 5-fold cross-validated accuracy and report the train score at that depth.

Hint: loop over depths with cross_val_score and keep the best mean.

Python Solution 2: tuning depth

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Shallow trees underfit and very deep trees overfit, so the best depth is usually in the middle. Details: decision trees.

Exercise 3: Implement k-NN yourself

Write a function that predicts the class of a point from its k nearest training points by majority vote, then check it against scikit-learn on the iris data.

Hint: use np.argsort on the distances and np.bincount on the neighbor labels.

Python Solution 3: k-NN from scratch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 4: Find the right number of clusters

Generate data with 5 blob centers and see which k the silhouette score prefers. Then repeat with more overlap (cluster_std=2.5) and discuss what changes.

Hint: loop k from 2 to 9 and compute silhouette_score.

Python Solution 4: silhouette search

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The silhouette prefers k = 4 in both runs, although the generator used 5 centers: two of the five blobs lie close together and merge. With heavy overlap the best score falls from about 0.79 to 0.47 and the curve flattens, so the choice becomes much less clear. The silhouette measures what the data shows, not what the generator did. See evaluating clusters.

Exercise 5: Spot the outliers three ways

A column of 200 values has a few errors injected. Flag outliers with the z-score, the modified z-score (MAD) and the IQR rule and report how many each method finds.

Hint: z > 3; 0.6745(x - median)/MAD > 3.5; Tukey fences with 1.5IQR.

Python Solution 5: three detectors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The methods agree on the extreme values but may differ on borderline ones. Learn more in statistical outlier detection.

Exercise 6: A document search ranking

Given five short documents, rank them for the query "machine learning for text" with TF-IDF and cosine similarity. Which document is first and why?

Hint: fit TfidfVectorizer on the documents, then transform the query with the same vectorizer.

Python Solution 6: TF-IDF ranking

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The top documents share the rare words "text", "machine" and "learning", while "for" is removed as a stop word. Read text mining basics for the theory.

Bigger challenges

  1. Compare five classifiers on the wine dataset with cross-validation; justify the winner with at least two metrics.
  2. Imbalance study: take a dataset with 1% positives and compare no weighting, class weights and random oversampling by average precision.
  3. Segment real customers: use any public retail dataset, build RFM features and describe the segments for marketing (see the segmentation project).
  4. Anomaly detective: inject synthetic anomalies into a sensor dataset and measure how many Isolation Forest and LOF each recover.
  5. Write Apriori from scratch (see association rules) and compare with a library on the same data.

Final checklist for any data mining project

  • Is the question clear, and is data mining the right tool for it?
  • Is the data clean, with missing values and outliers handled and documented?
  • Are training and test data separated before any learning step (no leakage)?
  • Is the metric tied to the real cost of each error, and is there a naive baseline?
  • Are results stable (cross-validation, resampling) and understandable to the people who will use them?

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. A rare-disease test has high accuracy but low precision. What is the likely reason?

    When positives are rare, false positives from the large negative group can outnumber true positives.
  2. 2. Which approach avoids choosing a depth that fits one lucky split?

    Cross-validation averages performance over several splits, giving a steadier estimate.
  3. 3. In TF-IDF search, why does the query go through the vectorizer already fitted on the documents?

    Query and documents must live in the same vector space to be comparable.

Summary

  • The best way to learn is to implement: metrics, k-NN, outlier rules, TF-IDF ranking.
  • Always compare against a baseline, validate with cross-validation and relate metrics to real costs.
  • Use the final checklist on every project, then continue with the bigger challenges.

Next: Back to the Data Mining track