Ujian Akhir Semester & Presentasi Proyek
Rangkuman seluruh semester, lembar rumus, kisi-kisi UAS, simulasi soal terpadu, dan checklist presentasi/defense proyek akhir.
Notebook lab (pratinjau)
Unduh lalu jalankan di Jupyter, VS Code, atau Google Colab.
Lab Minggu 18 — Simulasi UAS: Hitung Manual ➜ Verifikasi dengan Kode
Cara pakai notebook ini:
- Baca soal. Kerjakan dulu di kertas (tanpa kalkulator kode).
- Tulis jawabanmu di variabel
jawaban_saya. - Jalankan sel verifikasi — kode menghitung jawaban yang benar dengan library standar.
Topik mencakup seluruh semester (Minggu 1–17).
import sys
sys.path.insert(0, "..")
import math
from collections import Counter
import numpy as np
import pandas as pd
import networkx as nx
from sklearn.cluster import DBSCAN, KMeans
from sklearn.metrics import confusion_matrix, f1_score, precision_score, recall_score, silhouette_score
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori, association_rules
from common.datasets import play_tennis, transaksi_contoh, dokumen_teks
def cek(nama, saya, benar, tol=1e-3):
if saya is None:
print(f"{nama}: jawaban benar = {benar}")
else:
ok = abs(saya - benar) <= tol
print(f"{nama}: {'✅ BENAR' if ok else '❌ SALAH'} (kamu {saya}, kunci {round(benar, 4)})")
Soal 1 — Asosiasi (Minggu 4–5)
Dataset 9 transaksi Han & Kamber. Hitung support, confidence, lift untuk {I1, I2} → {I5}.
transaksi = transaksi_contoh()
for i, t in enumerate(transaksi, 1):
print(f"T{i}: {t}")
jawaban_saya = {"support": None, "confidence": None, "lift": None}
te = TransactionEncoder()
X = pd.DataFrame(te.fit_transform(transaksi), columns=te.columns_)
fi = apriori(X, min_support=0.2, use_colnames=True)
rules = association_rules(fi, metric="confidence", min_threshold=0.0)
r = rules[(rules.antecedents == frozenset({"I1", "I2"})) & (rules.consequents == frozenset({"I5"}))].iloc[0]
for k in ["support", "confidence", "lift"]:
cek(k, jawaban_saya[k], r[k])
Soal 2 — Decision Tree (Minggu 6)
Hitung Entropy(play) dan Information Gain untuk keempat atribut. Atribut mana jadi akar?
df = play_tennis()
df
jawaban_saya = {"entropy": None, "outlook": None, "temperature": None, "humidity": None, "wind": None}
def entropy(s):
p = s.value_counts(normalize=True)
return -(p * np.log2(p)).sum()
def gain(df, a, target="play"):
return entropy(df[target]) - sum(len(g) / len(df) * entropy(g[target]) for _, g in df.groupby(a))
cek("entropy", jawaban_saya["entropy"], entropy(df["play"]))
for a in ["outlook", "temperature", "humidity", "wind"]:
cek(f"gain {a}", jawaban_saya[a], gain(df, a))
Soal 3 — Evaluasi model (Minggu 8)
Confusion matrix: TP = 40, FP = 10, FN = 20, TN = 30. Hitung accuracy, precision, recall, F1.
jawaban_saya = {"accuracy": None, "precision": None, "recall": None, "f1": None}
y = np.array([1] * 40 + [0] * 10 + [1] * 20 + [0] * 30)
p = np.array([1] * 40 + [1] * 10 + [0] * 20 + [0] * 30)
print(confusion_matrix(y, p, labels=[1, 0]))
cek("accuracy", jawaban_saya["accuracy"], (y == p).mean())
cek("precision", jawaban_saya["precision"], precision_score(y, p))
cek("recall", jawaban_saya["recall"], recall_score(y, p))
cek("f1", jawaban_saya["f1"], f1_score(y, p))
Soal 4 — K-Means satu iterasi (Minggu 10)
Titik: A(1,1), B(1.5,2), C(4,4), D(5,7), E(3.5,5), F(4.5,5), G(3.5,4.5). Centroid awal: (1,1) dan (5,7). Tentukan klaster tiap titik dan centroid baru.
P_ = np.array([[1, 1], [1.5, 2], [4, 4], [5, 7], [3.5, 5], [4.5, 5], [3.5, 4.5]])
km = KMeans(2, init=np.array([[1, 1], [5, 7.0]]), n_init=1, max_iter=1).fit(P_)
print("label :", km.labels_)
print("centroid:\n", km.cluster_centers_)
print("silhouette:", round(silhouette_score(P_, km.labels_), 3))
Soal 5 — DBSCAN (Minggu 11)
Titik (0,0), (0,1), (1,0), (1,1), (2.5,1), (10,10); ε = 1.5, MinPts = 4. Tentukan core, border, noise.
Q = np.array([[0, 0], [0, 1], [1, 0], [1, 1], [2.5, 1], [10, 10]])
db = DBSCAN(eps=1.5, min_samples=4).fit(Q)
jenis = ["noise" if l == -1 else ("core" if i in db.core_sample_indices_ else "border")
for i, l in enumerate(db.labels_)]
pd.DataFrame({"titik": list(map(tuple, Q)), "jenis": jenis})
Soal 6 — HDFS & MapReduce (Minggu 12)
a. File 1000 MB, blok 128 MB, replikasi 3: berapa blok dan total penyimpanan?
b. Hitung frekuensi kata "data" di dokumen_teks() dengan pola map → shuffle → reduce.
jawaban_saya = {"blok": None, "total_mb": None}
cek("jumlah blok", jawaban_saya["blok"], math.ceil(1000 / 128), tol=0)
cek("total MB", jawaban_saya["total_mb"], 1000 * 3, tol=0)
peta = [(w, 1) for d in dokumen_teks() for w in d.split()] # map
grup = {}
for w, v in peta: # shuffle
grup.setdefault(w, []).append(v)
hasil = {w: sum(v) for w, v in grup.items()} # reduce
print("frekuensi 'data':", hasil["data"], "| cek Counter:", Counter(w for d in dokumen_teks() for w in d.split())["data"])
Soal 7 — Stream, LSH, PageRank (Minggu 15)
a. Decaying window c = 0.1, stream 10, 10, 10 → S₃? b. LSH b = 20, r = 5, s = 0.8 → P(kandidat)? c. Graf A→B, A→C, B→C, C→A, d = 0.85, mulai 1/3: PageRank setelah 1 iterasi?
jawaban_saya = {"S3": None, "p_lsh": None, "rA": None, "rB": None, "rC": None}
s = 0
for x in [10, 10, 10]:
s = 0.9 * s + x
cek("S3", jawaban_saya["S3"], s)
cek("P(kandidat)", jawaban_saya["p_lsh"], 1 - (1 - 0.8 ** 5) ** 20)
r0 = np.ones(3) / 3
M = np.array([[0, 0, 1], [0.5, 0, 0], [0.5, 1, 0]]) # kolom = sumber A, B, C
r1 = 0.15 / 3 + 0.85 * M @ r0
for nama, val in zip(["rA", "rB", "rC"], r1):
cek(nama, jawaban_saya[nama], val)
print("PageRank konvergen (networkx):", {k: round(v, 4) for k, v in nx.pagerank(nx.DiGraph([("A","B"),("A","C"),("B","C"),("C","A")])).items()})
Soal 8 — Isolation Forest (Minggu 16)
n = 256. Hitung c(n), lalu skor s untuk E[h(x)] = 3 dan E[h(x)] = 12.
jawaban_saya = {"c": None, "s3": None, "s12": None}
c = 2 * (math.log(255) + 0.5772156649) - 2 * 255 / 256
cek("c(256)", jawaban_saya["c"], c)
cek("s(E=3)", jawaban_saya["s3"], 2 ** (-3 / c))
cek("s(E=12)", jawaban_saya["s12"], 2 ** (-12 / c))
✍️ Coba sendiri — Soal esai (tulis jawabanmu)
- Jelaskan perbedaan OLTP dan OLAP, dan di mana data warehouse berada dalam siklus KDD.
- Kapan kamu memilih Spark dibanding Pandas? Sebutkan minimal 3 kriteria.
- Jelaskan cara kerja hybrid autoencoder + Isolation Forest untuk 200 sensor.
- Rancang pertanyaan defense tersulit untuk proyekmu sendiri, lalu jawab.
Jawaban:
- ...
- ...
- ...
- ...
Selanjutnya
Kerjakan kuis review UAS dan exercise.py, lalu jalankan python check.py 18 dari folder utama.