SONUB home

Only our team reads this. Don't include passwords or card numbers.

Thank you! We read every message and use it to decide what to improve next.

Close Send
EN English 한국어 日本語 简体中文 繁體中文 हिन्दी বাংলা اردو Español Português Français Deutsch Русский العربية Bahasa Indonesia Tiếng Việt ไทย Türkçe

How to enable notifications

Notifications are unavailable in this browser or on this address. Try a supported browser on the secure SONUB site.

If notifications are blocked, allow them in your browser settings.

  1. Open the site controls beside the address bar, or your browser’s site settings. Set Notifications for this site to Allow.
  2. In your device settings, allow notifications for your browser and check Do Not Disturb or Focus.
  3. Return to SONUB and reload the page. Once notifications are on, the bell disappears.

On iPhone or iPad, add SONUB to your Home Screen, open it there, then allow notifications.

Build AI in five steps

Every project goes through the same five steps: preparation, & visualization, data , and model evaluation. Build a small spam filter in your and walk through all five.

Messy pieces of writing go into a filter machine, duplicates and scraps fall out, and a neat stack of clean data comes out
5

Model evaluation

A model is only as good as its score on data it has never seen. You use the valid part while you are still changing things, and the test part only once, at the very end.

Accuracy alone can fool you. The baseline gets more than half right just by saying "not spam" every time, yet it never catches a single spam message. That is why spam filters look at two more numbers:

  • Precision: of the messages the model called spam, how many really were spam.
  • Recall: of the real spam messages, how many the model caught.
  • F1 combines the two into one number.

Finally, read the wrong answers. They tell you what to change in the next round.

The logic, step by step

  1. Predict spam or not p ≥ 0.5
  2. Count the four cases tp · fp · fn · tn
  3. Compute the scores accuracy · F1
  4. Compare with the baseline baseline
  5. Read the wrong answers wrong answers

A message is called spam when the probability is 0.5 or more. Each answer then falls into one of four cases: tp (spam, called spam), fp (normal, called spam), fn (spam, called normal) and tn (normal, called normal). This is the confusion table.

accuracy = (tp + tn) / all, precision = tp / (tp + fp), recall = tp / (tp + fn), and F1 = 2 × precision × recall / (precision + recall). The program computes the same scores for the baseline and uses the model only if its F1 is higher.

The same job with real-world tools

scikit-learn has a ready function for every score here, and confusion_matrix builds the four-case table.

  • accuracy_score
  • precision_score
  • recall_score
  • f1_score
  • confusion_matrix

Type and try

Open 5-evaluate.ipynb and run all cells on the valid part. Read the wrong answers and write one change to try next in notes.md. Only when you are finished, change part = "valid" to part = "test" and run everything once more.

5-evaluate.ipynbThe full code of this notebook
# Step 5. Model evaluation — score the model on data it never saw, and compare it with the baseline.
import csv
import json
import math

part = "valid"  # change to "test" only once, at the very end
with open("data/vocab.json", encoding="utf-8") as f:
    vocab = json.load(f)
with open("model.json", encoding="utf-8") as f:
    model = json.load(f)
with open(f"data/{part}.csv", newline="", encoding="utf-8") as f:
    rows = [(r["text"], int(r["spam"])) for r in csv.DictReader(f)]


def predict(text):
    x = [vocab[w] for w in dict.fromkeys(text.split()) if w in vocab]
    p = 1 / (1 + math.exp(-(model["bias"] + sum(model["weights"][i] for i in x))))
    return 1 if p >= 0.5 else 0


def scores(answers):
    tp = sum(1 for (_, y), a in zip(rows, answers) if y == 1 and a == 1)
    fp = sum(1 for (_, y), a in zip(rows, answers) if y == 0 and a == 1)
    fn = sum(1 for (_, y), a in zip(rows, answers) if y == 1 and a == 0)
    tn = sum(1 for (_, y), a in zip(rows, answers) if y == 0 and a == 0)
    precision = tp / (tp + fp) if tp + fp else 0.0
    recall = tp / (tp + fn) if tp + fn else 0.0
    f1 = 2 * precision * recall / (precision + recall) if precision + recall else 0.0
    return {"accuracy": (tp + tn) / len(rows), "precision": precision, "recall": recall, "f1": f1, "table": (tp, fp, fn, tn)}


model_answers = [predict(text) for text, _ in rows]
base = scores([model["baseline"]] * len(rows))
mine = scores(model_answers)

print(f"{part}: {len(rows)} rows")
print(f"{'':10} {'baseline':>9} {'model':>7}")
for key in ("accuracy", "precision", "recall", "f1"):
    print(f"{key:10} {base[key]:9.3f} {mine[key]:7.3f}")

tp, fp, fn, tn = mine["table"]
print("\nconfusion table   said spam  said normal")
print(f"  really spam     {tp:9}  {fn:11}")
print(f"  really normal   {fp:9}  {tn:11}")

print("\nwrong answers")
for (text, y), a in zip(rows, model_answers):
    if y != a:
        print(f"  answer {y}, model {a}: {text}")

print("\nuse the model" if mine["f1"] > base["f1"] else "\nkeep the baseline")
Run ▸ Run All CellsWhat you should see
valid: 18 rows
            baseline   model
accuracy       0.556   0.778
precision      0.000   1.000
recall         0.000   0.500
f1             0.000   0.667

confusion table   said spam  said normal
  really spam             4            4
  really normal           0           10

wrong answers
  answer 1, model 0: dear friend i need your help to move my money. you will get a share
  answer 1, model 0: we are hiring. work two hours a day and earn more
  answer 1, model 0: make money fast. join my team now
  answer 1, model 0: exclusive discount for our best customers. click the link

use the model

Done when

On valid, the model scores F1 0.667 against the baseline's 0.000, so the program says to use the model. Recall is only 0.5: it misses spam that uses words it has not learned, such as money, hiring and share. You have written one change for the next round, for example more spam examples of that kind or keeping words that appear only once.

If you get stuck

ZeroDivisionError means a part has no spam or the model called nothing spam; the program guards precision and recall for this. If your scores differ, run notebooks 3 and 4 again before 5, because each step reads the files of the one before.

Booking time has passed