jokodm 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
jokodm/__init__.py ADDED
@@ -0,0 +1,12 @@
1
+ """jokodm: algoritma data mining yang ditulis dari nol, lengkap dengan visualisasinya.
2
+
3
+ from jokodm import SVM, DecisionTree
4
+
5
+ Pembagian data dan evaluasi bisa memakai scikit-learn (train_test_split, accuracy_score, dan lain-lain).
6
+ """
7
+
8
+ from .svm import SVM
9
+ from .tree import DecisionTree
10
+
11
+ __version__ = "0.1.0"
12
+ __all__ = ["SVM", "DecisionTree"]
jokodm/_dasar.py ADDED
@@ -0,0 +1,117 @@
1
+ """Bagian bersama: konversi data masukan dan kelas dasar model bergaya scikit-learn.
2
+
3
+ Masukan boleh berupa list, array numpy, atau DataFrame/Series pandas. Di dalam library semua data
4
+ disimpan sebagai list Python biasa, jadi jokodm tidak bergantung pada numpy maupun scikit-learn.
5
+ """
6
+
7
+ import inspect
8
+ import numbers
9
+
10
+
11
+ def ke_tabel(X):
12
+ """Ubah X menjadi (baris, nama_kolom). baris = list of list; nama_kolom = list atau None."""
13
+ nama = None
14
+ if hasattr(X, "columns") and hasattr(X, "values"): # DataFrame pandas
15
+ nama = [str(c) for c in X.columns]
16
+ baris = X.values.tolist()
17
+ elif hasattr(X, "tolist"): # array numpy
18
+ baris = X.tolist()
19
+ else:
20
+ baris = [list(r) if isinstance(r, (list, tuple)) else r for r in X]
21
+ if not baris:
22
+ raise ValueError("Data X kosong.")
23
+ if not isinstance(baris[0], (list, tuple)):
24
+ raise ValueError("X harus 2 dimensi (baris x atribut). Untuk satu atribut pakai X = df[['nama_kolom']] "
25
+ "atau [[nilai], [nilai], ...].")
26
+ lebar = len(baris[0])
27
+ for i, r in enumerate(baris):
28
+ if len(r) != lebar:
29
+ raise ValueError(f"Baris ke-{i} punya {len(r)} nilai, padahal baris pertama punya {lebar}.")
30
+ return [list(r) for r in baris], nama
31
+
32
+
33
+ def ke_list(y):
34
+ """Ubah y (list/array/Series) menjadi list 1 dimensi."""
35
+ if hasattr(y, "values") and hasattr(y, "index"): # Series pandas
36
+ y = y.values
37
+ if hasattr(y, "tolist"):
38
+ y = y.tolist()
39
+ y = list(y)
40
+ if y and isinstance(y[0], (list, tuple)):
41
+ if all(len(v) == 1 for v in y):
42
+ return [v[0] for v in y]
43
+ raise ValueError("y harus 1 dimensi (satu label per baris).")
44
+ return y
45
+
46
+
47
+ def angka(v):
48
+ """True bila v angka (bukan bool)."""
49
+ return isinstance(v, numbers.Number) and not isinstance(v, bool)
50
+
51
+
52
+ def ke_float(baris):
53
+ out = []
54
+ for i, r in enumerate(baris):
55
+ try:
56
+ out.append([float(v) for v in r])
57
+ except (TypeError, ValueError):
58
+ raise ValueError(f"Baris ke-{i} berisi nilai bukan angka: {r}. Algoritma ini butuh atribut numerik.")
59
+ return out
60
+
61
+
62
+ def bersih(v):
63
+ """Nilai numpy (misal numpy.int64) menjadi nilai Python biasa agar tampilan rapi."""
64
+ if hasattr(v, "item") and not isinstance(v, (str, bytes)):
65
+ try:
66
+ return v.item()
67
+ except (ValueError, AttributeError):
68
+ return v
69
+ return v
70
+
71
+
72
+ class ModelDasar:
73
+ """Kelas dasar: get_params / set_params / __repr__ seperti estimator scikit-learn,
74
+ sehingga model jokodm bisa dipakai di cross_val_score dan GridSearchCV."""
75
+
76
+ _estimator_type = "classifier"
77
+
78
+ @classmethod
79
+ def _nama_param(cls):
80
+ sig = inspect.signature(cls.__init__)
81
+ return [p.name for p in sig.parameters.values() if p.name != "self" and p.kind != p.VAR_KEYWORD]
82
+
83
+ def get_params(self, deep=True):
84
+ return {k: getattr(self, k) for k in self._nama_param()}
85
+
86
+ def set_params(self, **params):
87
+ sah = self._nama_param()
88
+ for k, v in params.items():
89
+ if k not in sah:
90
+ raise ValueError(f"Parameter '{k}' tidak dikenal. Parameter yang ada: {', '.join(sah)}.")
91
+ setattr(self, k, v)
92
+ return self
93
+
94
+ def __sklearn_tags__(self):
95
+ """Label model untuk scikit-learn >= 1.6 (dipanggil oleh cross_val_score, GridSearchCV, dan sejenisnya).
96
+ scikit-learn hanya di-import saat fungsi ini dipanggil, jadi jokodm tetap bisa dipakai tanpa scikit-learn."""
97
+ from sklearn.utils import ClassifierTags, InputTags, Tags, TargetTags
98
+
99
+ return Tags(estimator_type="classifier", target_tags=TargetTags(required=True),
100
+ classifier_tags=ClassifierTags(), input_tags=InputTags(string=self._terima_teks))
101
+
102
+ _terima_teks = False
103
+
104
+ def __repr__(self):
105
+ bawaan = {p.name: p.default for p in inspect.signature(type(self).__init__).parameters.values() if p.name != "self"}
106
+ isi = [f"{k}={v!r}" for k, v in self.get_params().items() if bawaan.get(k, object()) != v]
107
+ return f"{type(self).__name__}({', '.join(isi)})"
108
+
109
+ def _cek_terlatih(self):
110
+ if not getattr(self, "terlatih_", False):
111
+ raise RuntimeError(f"Model {type(self).__name__} belum dilatih. Jalankan .fit(X, y) dulu.")
112
+
113
+ def score(self, X, y):
114
+ """Akurasi pada data (X, y)."""
115
+ y = ke_list(y)
116
+ p = self.predict(X)
117
+ return sum(1 for a, b in zip(y, p) if a == b) / len(y)
jokodm/_warna.py ADDED
@@ -0,0 +1,20 @@
1
+ """Palet warna grafik jokodm (tetradic, biru dominan)."""
2
+
3
+ KELAS = ["#1F7AE0", "#F0882A", "#7B57D1", "#4E9A2E", "#155BB0", "#C2621A", "#5A3BA8", "#36721D"]
4
+ MUDA = ["#D6E7FB", "#FCE3CC", "#E4DBF6", "#DCEED3", "#CFDDF0", "#F3D8C2", "#DCD3EF", "#D3E3CA"]
5
+ PENANDA = ["o", "s", "^", "D", "v", "P", "X", "h"]
6
+ TINTA = "#10233F"
7
+ REDUP = "#5B6B82"
8
+ GARIS = "#DCE5F0"
9
+
10
+
11
+ def warna(i):
12
+ return KELAS[i % len(KELAS)]
13
+
14
+
15
+ def muda(i):
16
+ return MUDA[i % len(MUDA)]
17
+
18
+
19
+ def penanda(i):
20
+ return PENANDA[i % len(PENANDA)]
jokodm/svm.py ADDED
@@ -0,0 +1,382 @@
1
+ """Support Vector Machine (klasifikasi 2 kelas) yang ditulis dari nol.
2
+
3
+ Masalah dual SVM:
4
+ minimumkan 1/2 * sum_i sum_j a_i a_j y_i y_j K(x_i, x_j) - sum_i a_i
5
+ dengan 0 <= a_i <= C dan sum_i a_i y_i = 0
6
+ diselesaikan dengan Sequential Minimal Optimization (SMO): setiap langkah memilih sepasang alfa yang paling
7
+ melanggar syarat optimal (pemilihan orde kedua), lalu memperbaruinya secara analitik.
8
+
9
+ Fungsi keputusan: f(x) = sum_i a_i y_i K(x_i, x) + b ; kelas = classes_[1] bila f(x) > 0, selain itu classes_[0].
10
+ Untuk kernel linear: f(x) = w . x + b dengan w = sum_i a_i y_i x_i dan lebar margin = 2 / ||w||.
11
+ """
12
+
13
+ import math
14
+
15
+ from ._dasar import ModelDasar, bersih, ke_float, ke_list, ke_tabel
16
+ from . import _warna as W
17
+
18
+ TAU = 1e-12
19
+
20
+
21
+ def _kernel(nama, gamma, degree, coef0):
22
+ if nama == "linear":
23
+ return lambda a, b: sum(p * q for p, q in zip(a, b))
24
+ if nama == "rbf":
25
+ return lambda a, b: math.exp(-gamma * sum((p - q) ** 2 for p, q in zip(a, b)))
26
+ if nama == "poly":
27
+ return lambda a, b: (gamma * sum(p * q for p, q in zip(a, b)) + coef0) ** degree
28
+ raise ValueError("kernel harus 'linear', 'rbf', atau 'poly'.")
29
+
30
+
31
+ def _urut_kelas(y):
32
+ unik = list(dict.fromkeys(y))
33
+ try:
34
+ return sorted(unik)
35
+ except TypeError:
36
+ return sorted(unik, key=str)
37
+
38
+
39
+ def _fmt(v, d=4):
40
+ s = f"{v:.{d}f}"
41
+ return "0." + "0" * d if s in ("-0." + "0" * d,) else s
42
+
43
+
44
+ class SVM(ModelDasar):
45
+ """Support Vector Machine untuk klasifikasi 2 kelas.
46
+
47
+ Parameter
48
+ ---------
49
+ C : float, bawaan 1.0
50
+ Penalti kesalahan (soft margin). Makin besar C, makin sedikit titik yang boleh melanggar margin.
51
+ C sangat besar (misalnya 1e6) mendekati hard margin.
52
+ kernel : 'linear' (bawaan), 'rbf', atau 'poly'
53
+ gamma : 'scale' (bawaan), 'auto', atau angka. Dipakai kernel rbf dan poly.
54
+ 'scale' = 1 / (jumlah atribut * variansi X), sama dengan scikit-learn.
55
+ degree, coef0 : derajat dan konstanta kernel poly.
56
+ tol : float, bawaan 1e-3. Batas toleransi syarat optimal (KKT).
57
+ max_iter : int, bawaan 100000. Batas jumlah langkah SMO.
58
+ kelas_positif : kelas yang diberi kode +1 (f(x) > 0). Bawaan None: kelas kedua menurut urutan
59
+ (sama dengan scikit-learn). Contoh: SVM(kelas_positif="Layak").
60
+
61
+ Atribut setelah fit
62
+ -------------------
63
+ classes_, coef_ (kernel linear), intercept_, support_, support_vectors_, n_support_, dual_coef_,
64
+ alpha_, margin_ (kernel linear), n_iter_, persamaan_ (kernel linear), feature_names_in_.
65
+ """
66
+
67
+ def __init__(self, C=1.0, kernel="linear", gamma="scale", degree=3, coef0=0.0, tol=1e-3, max_iter=100000,
68
+ kelas_positif=None):
69
+ self.C = C
70
+ self.kelas_positif = kelas_positif
71
+ self.kernel = kernel
72
+ self.gamma = gamma
73
+ self.degree = degree
74
+ self.coef0 = coef0
75
+ self.tol = tol
76
+ self.max_iter = max_iter
77
+
78
+ # ------------------------------------------------------------------ pelatihan
79
+ def fit(self, X, y):
80
+ """Latih model dengan data X (baris x atribut) dan label y (2 kelas)."""
81
+ baris, nama = ke_tabel(X)
82
+ Xf = ke_float(baris)
83
+ y = ke_list(y)
84
+ if len(y) != len(Xf):
85
+ raise ValueError(f"Jumlah baris X ({len(Xf)}) tidak sama dengan jumlah label y ({len(y)}).")
86
+ kelas = _urut_kelas(y)
87
+ if len(kelas) != 2:
88
+ raise ValueError(f"SVM jokodm saat ini untuk 2 kelas, tetapi y berisi {len(kelas)} kelas: {kelas}.")
89
+ if self.kelas_positif is not None:
90
+ if self.kelas_positif not in kelas:
91
+ raise ValueError(f"kelas_positif {self.kelas_positif!r} tidak ada di y. Kelas yang ada: {kelas}.")
92
+ kelas = [k for k in kelas if k != self.kelas_positif] + [self.kelas_positif]
93
+ if not self.C > 0:
94
+ raise ValueError("C harus lebih besar dari 0.")
95
+ n, d = len(Xf), len(Xf[0])
96
+ ys = [1.0 if v == kelas[1] else -1.0 for v in y]
97
+
98
+ if isinstance(self.gamma, str):
99
+ if self.gamma == "scale":
100
+ semua = [v for r in Xf for v in r]
101
+ rata = sum(semua) / len(semua)
102
+ var = sum((v - rata) ** 2 for v in semua) / len(semua)
103
+ g = 1.0 / (d * var) if var > 0 else 1.0
104
+ elif self.gamma == "auto":
105
+ g = 1.0 / d
106
+ else:
107
+ raise ValueError("gamma harus 'scale', 'auto', atau angka.")
108
+ else:
109
+ g = float(self.gamma)
110
+ K = _kernel(self.kernel, g, self.degree, self.coef0)
111
+
112
+ C = float(self.C)
113
+ cache = {}
114
+
115
+ def baris_Q(i): # Q_ij = y_i y_j K(x_i, x_j)
116
+ r = cache.get(i)
117
+ if r is None:
118
+ xi, yi = Xf[i], ys[i]
119
+ r = [yi * ys[j] * K(xi, Xf[j]) for j in range(n)]
120
+ if len(cache) > 600:
121
+ cache.pop(next(iter(cache)))
122
+ cache[i] = r
123
+ return r
124
+
125
+ QD = [K(Xf[i], Xf[i]) for i in range(n)]
126
+ a = [0.0] * n
127
+ G = [-1.0] * n
128
+ it = 0
129
+ while it < self.max_iter:
130
+ pas = self._pilih_pasangan(a, G, ys, QD, baris_Q, C, n, float(self.tol))
131
+ if pas is None:
132
+ break
133
+ i, j = pas
134
+ it += 1
135
+ Qi, Qj = baris_Q(i), baris_Q(j)
136
+ ai_lama, aj_lama = a[i], a[j]
137
+ if ys[i] != ys[j]:
138
+ quad = QD[i] + QD[j] + 2 * Qi[j]
139
+ quad = quad if quad > 0 else TAU
140
+ delta = (-G[i] - G[j]) / quad
141
+ beda = a[i] - a[j]
142
+ a[i] += delta
143
+ a[j] += delta
144
+ if beda > 0:
145
+ if a[j] < 0:
146
+ a[j], a[i] = 0.0, beda
147
+ elif a[i] < 0:
148
+ a[i], a[j] = 0.0, -beda
149
+ if beda > 0:
150
+ if a[i] > C:
151
+ a[i], a[j] = C, C - beda
152
+ elif a[j] > C:
153
+ a[j], a[i] = C, C + beda
154
+ else:
155
+ quad = QD[i] + QD[j] - 2 * Qi[j]
156
+ quad = quad if quad > 0 else TAU
157
+ delta = (G[i] - G[j]) / quad
158
+ jml = a[i] + a[j]
159
+ a[i] -= delta
160
+ a[j] += delta
161
+ if jml > C:
162
+ if a[i] > C:
163
+ a[i], a[j] = C, jml - C
164
+ elif a[j] < 0:
165
+ a[j], a[i] = 0.0, jml
166
+ if jml > C:
167
+ if a[j] > C:
168
+ a[j], a[i] = C, jml - C
169
+ elif a[i] < 0:
170
+ a[i], a[j] = 0.0, jml
171
+ di, dj = a[i] - ai_lama, a[j] - aj_lama
172
+ for k in range(n):
173
+ G[k] += Qi[k] * di + Qj[k] * dj
174
+ self.n_iter_ = it
175
+ self.konvergen_ = it < self.max_iter
176
+ if not self.konvergen_:
177
+ import warnings
178
+ warnings.warn(f"SMO berhenti di batas max_iter={self.max_iter} sebelum optimal. Coba normalisasi data "
179
+ "atau naikkan max_iter.", RuntimeWarning)
180
+
181
+ # bias b (= -rho pada libsvm): rata-rata dari support vector bebas (0 < alfa < C)
182
+ ub, lb, jml, nb = math.inf, -math.inf, 0.0, 0
183
+ for i in range(n):
184
+ yG = ys[i] * G[i]
185
+ if a[i] >= C:
186
+ if ys[i] < 0:
187
+ ub = min(ub, yG)
188
+ else:
189
+ lb = max(lb, yG)
190
+ elif a[i] <= 0:
191
+ if ys[i] > 0:
192
+ ub = min(ub, yG)
193
+ else:
194
+ lb = max(lb, yG)
195
+ else:
196
+ nb += 1
197
+ jml += yG
198
+ rho = jml / nb if nb else (ub + lb) / 2
199
+ b = -rho
200
+
201
+ sv = [i for i in range(n) if a[i] > 0]
202
+ self.classes_ = [bersih(k) for k in kelas]
203
+ self.n_features_in_ = d
204
+ if nama:
205
+ self.feature_names_in_ = nama
206
+ self._nama = nama or [f"x{k + 1}" for k in range(d)]
207
+ self._gamma = g
208
+ self._K = K
209
+ self._X, self._y, self._ys = Xf, [bersih(v) for v in y], ys
210
+ self.alpha_ = a
211
+ self.support_ = sv
212
+ self.support_vectors_ = [Xf[i] for i in sv]
213
+ self._sv_ay = [a[i] * ys[i] for i in sv]
214
+ self.dual_coef_ = [self._sv_ay]
215
+ self.n_support_ = [sum(1 for i in sv if ys[i] < 0), sum(1 for i in sv if ys[i] > 0)]
216
+ self.intercept_ = [b]
217
+ if self.kernel == "linear":
218
+ w = [sum(a[i] * ys[i] * Xf[i][k] for i in sv) for k in range(d)]
219
+ self.coef_ = [w]
220
+ nw = math.sqrt(sum(v * v for v in w))
221
+ self.margin_ = 2 / nw if nw > 0 else math.inf
222
+ self.persamaan_ = self._teks_persamaan(w, b)
223
+ self.terlatih_ = True
224
+ return self
225
+
226
+ @staticmethod
227
+ def _pilih_pasangan(a, G, ys, QD, baris_Q, C, n, eps):
228
+ Gmax, imax = -math.inf, -1
229
+ for t in range(n):
230
+ if ys[t] > 0:
231
+ if a[t] < C and -G[t] >= Gmax:
232
+ Gmax, imax = -G[t], t
233
+ elif a[t] > 0 and G[t] >= Gmax:
234
+ Gmax, imax = G[t], t
235
+ if imax < 0:
236
+ return None
237
+ i = imax
238
+ Qi = baris_Q(i)
239
+ Gmax2, jmin, obj_min = -math.inf, -1, math.inf
240
+ for j in range(n):
241
+ if ys[j] > 0:
242
+ if a[j] > 0:
243
+ gd = Gmax + G[j]
244
+ if G[j] >= Gmax2:
245
+ Gmax2 = G[j]
246
+ if gd > 0:
247
+ quad = QD[i] + QD[j] - 2.0 * ys[i] * Qi[j]
248
+ obj = -(gd * gd) / (quad if quad > 0 else TAU)
249
+ if obj <= obj_min:
250
+ jmin, obj_min = j, obj
251
+ elif a[j] < C:
252
+ gd = Gmax - G[j]
253
+ if -G[j] >= Gmax2:
254
+ Gmax2 = -G[j]
255
+ if gd > 0:
256
+ quad = QD[i] + QD[j] + 2.0 * ys[i] * Qi[j]
257
+ obj = -(gd * gd) / (quad if quad > 0 else TAU)
258
+ if obj <= obj_min:
259
+ jmin, obj_min = j, obj
260
+ if Gmax + Gmax2 < eps or jmin < 0:
261
+ return None
262
+ return i, jmin
263
+
264
+ def _teks_persamaan(self, w, b):
265
+ bagian = []
266
+ for k, (wk, nm) in enumerate(zip(w, self._nama)):
267
+ tanda = "-" if wk < 0 else "+"
268
+ nilai = _fmt(abs(wk))
269
+ bagian.append(f"{'-' if tanda == '-' else ''}{nilai}*{nm}" if k == 0 else f" {tanda} {nilai}*{nm}")
270
+ bagian.append(f" {'-' if b < 0 else '+'} {_fmt(abs(b))}")
271
+ return "f(x) = " + "".join(bagian)
272
+
273
+ # ------------------------------------------------------------------ prediksi
274
+ def decision_function(self, X):
275
+ """Nilai f(x) tiap baris. f(x) > 0 berarti classes_[1], f(x) < 0 berarti classes_[0]."""
276
+ self._cek_terlatih()
277
+ baris, _ = ke_tabel(X)
278
+ Xf = ke_float(baris)
279
+ if len(Xf[0]) != self.n_features_in_:
280
+ raise ValueError(f"X punya {len(Xf[0])} atribut, padahal model dilatih dengan {self.n_features_in_}.")
281
+ return [self._f(x) for x in Xf]
282
+
283
+ def _f(self, x):
284
+ if self.kernel == "linear":
285
+ return sum(p * q for p, q in zip(self.coef_[0], x)) + self.intercept_[0]
286
+ return sum(c * self._K(s, x) for c, s in zip(self._sv_ay, self.support_vectors_)) + self.intercept_[0]
287
+
288
+ def predict(self, X):
289
+ """Kelas prediksi tiap baris X."""
290
+ return [self.classes_[1] if f > 0 else self.classes_[0] for f in self.decision_function(X)]
291
+
292
+ # ------------------------------------------------------------------ informasi
293
+ def ringkasan(self):
294
+ """Tampilkan persamaan, margin, dan daftar support vector."""
295
+ self._cek_terlatih()
296
+ k0, k1 = self.classes_
297
+ print(f"SVM kernel {self.kernel}, C = {float(self.C):g}" + (f", gamma = {self._gamma:.4g}" if self.kernel != "linear" else ""))
298
+ print(f"Kelas: {k1!r} bila f(x) > 0, {k0!r} bila f(x) < 0")
299
+ if self.kernel == "linear":
300
+ print(self.persamaan_)
301
+ print(f"Lebar margin = 2 / ||w|| = {self.margin_:.4f}")
302
+ else:
303
+ print(f"f(x) = sum(alfa_i * y_i * K(x_i, x)) + b, b = {self.intercept_[0]:.4f}")
304
+ print(f"Support vector: {len(self.support_)} ({k0!r}: {self.n_support_[0]}, {k1!r}: {self.n_support_[1]})")
305
+ for i in self.support_:
306
+ x = ", ".join(f"{v:g}" for v in self._X[i])
307
+ jenis = "di batas margin" if self.alpha_[i] < float(self.C) - 1e-9 else "melanggar/di dalam margin (alfa = C)"
308
+ print(f" data ke-{i}: ({x}), kelas {self._y[i]!r}, alfa = {self.alpha_[i]:.4f}, {jenis}")
309
+ print(f"Langkah SMO: {self.n_iter_}")
310
+
311
+ def plot(self, X=None, y=None, baru=None, ax=None, judul=None, skala_sama=None, resolusi=160):
312
+ """Gambar data, hyperplane f(x) = 0, garis margin f(x) = +1 dan -1, serta support vector.
313
+
314
+ X, y : data yang digambar (bawaan: data latih). baru : titik baru yang ingin diprediksi (ditandai bintang).
315
+ skala_sama : True agar sumbu x dan y berskala sama (margin tampak tegak lurus); bawaan otomatis.
316
+ Hanya untuk data dengan tepat 2 atribut.
317
+ """
318
+ import matplotlib.pyplot as plt
319
+
320
+ self._cek_terlatih()
321
+ if self.n_features_in_ != 2:
322
+ raise ValueError(f"plot() butuh tepat 2 atribut, model ini memakai {self.n_features_in_}. "
323
+ "Pilih 2 atribut, misalnya X = df[['atribut1', 'atribut2']].")
324
+ if X is None:
325
+ Xp, yp, latih = self._X, self._y, True
326
+ else:
327
+ Xp, latih = ke_float(ke_tabel(X)[0]), False
328
+ yp = ke_list(y) if y is not None else self.predict(Xp)
329
+ Xb = ke_float(ke_tabel(baru)[0]) if baru is not None else []
330
+
331
+ semua = Xp + Xb + (self.support_vectors_ if latih else [])
332
+ xs, ys_ = [r[0] for r in semua], [r[1] for r in semua]
333
+ rx, ry = (max(xs) - min(xs)) or 1.0, (max(ys_) - min(ys_)) or 1.0
334
+ x0, x1 = min(xs) - 0.15 * rx, max(xs) + 0.15 * rx
335
+ y0, y1 = min(ys_) - 0.15 * ry, max(ys_) + 0.15 * ry
336
+ if ax is None:
337
+ _, ax = plt.subplots(figsize=(7, 5.6))
338
+
339
+ # latar daerah kelas dari tanda f(x)
340
+ gx = [x0 + (x1 - x0) * k / (resolusi - 1) for k in range(resolusi)]
341
+ gy = [y0 + (y1 - y0) * k / (resolusi - 1) for k in range(resolusi)]
342
+ Z = [[self._f([u, v]) for u in gx] for v in gy]
343
+ ax.contourf(gx, gy, Z, levels=[-1e18, 0, 1e18], colors=[W.muda(1), W.muda(0)], alpha=0.55)
344
+ ax.contour(gx, gy, Z, levels=[0], colors=[W.TINTA], linewidths=2.2)
345
+ ax.contour(gx, gy, Z, levels=[-1, 1], colors=[W.REDUP], linewidths=1.4, linestyles="--")
346
+
347
+ for k, kls in enumerate(self.classes_):
348
+ idx = [i for i, v in enumerate(yp) if v == kls]
349
+ ax.scatter([Xp[i][0] for i in idx], [Xp[i][1] for i in idx], s=60, c=W.warna(1 - k), marker=W.penanda(k),
350
+ edgecolors="white", linewidths=1.0, zorder=3, label=f"kelas {kls}")
351
+ if latih:
352
+ ax.scatter([s[0] for s in self.support_vectors_], [s[1] for s in self.support_vectors_], s=230,
353
+ facecolors="none", edgecolors=W.TINTA, linewidths=1.8, zorder=4,
354
+ label=f"support vector ({len(self.support_)})")
355
+ if Xb:
356
+ pb = self.predict(Xb)
357
+ for n_, (r, p) in enumerate(zip(Xb, pb), 1):
358
+ k = self.classes_.index(p)
359
+ ax.scatter([r[0]], [r[1]], s=320, marker="*", c=W.warna(1 - k), edgecolors=W.TINTA, linewidths=1.2,
360
+ zorder=5, label="data baru" if n_ == 1 else None)
361
+ ax.annotate(f"baru {n_}: {p}", (r[0], r[1]), xytext=(8, 8), textcoords="offset points", fontsize=9,
362
+ color=W.TINTA)
363
+ ax.plot([], [], color=W.TINTA, lw=2.2, label="hyperplane f(x) = 0")
364
+ ax.plot([], [], color=W.REDUP, lw=1.4, ls="--", label="margin f(x) = ±1")
365
+
366
+ ax.set_xlim(x0, x1)
367
+ ax.set_ylim(y0, y1)
368
+ if skala_sama is None:
369
+ skala_sama = max(rx, ry) / min(rx, ry) < 4
370
+ if skala_sama:
371
+ ax.set_aspect("equal", adjustable="box")
372
+ ax.set_xlabel(self._nama[0])
373
+ ax.set_ylabel(self._nama[1])
374
+ if judul is None:
375
+ judul = f"SVM kernel {self.kernel}, C = {float(self.C):g}"
376
+ if self.kernel == "linear":
377
+ judul += f"\n{self.persamaan_} margin = {self.margin_:.4f}"
378
+ ax.set_title(judul, fontsize=11)
379
+ ax.grid(True, color=W.GARIS, lw=0.8)
380
+ ax.set_axisbelow(True)
381
+ ax.legend(loc="upper left", bbox_to_anchor=(1.02, 1), fontsize=9, frameon=False, borderaxespad=0)
382
+ return ax
jokodm/tree.py ADDED
@@ -0,0 +1,375 @@
1
+ """Decision Tree untuk klasifikasi, ditulis dari nol.
2
+
3
+ Kriteria pemilihan atribut:
4
+ 'gini' : Gini index Gini(S) = 1 - sum p_k^2 (CART)
5
+ 'entropy' : Information gain Entropy(S) = -sum p_k log2 p_k (ID3)
6
+ 'gain_ratio' : Gain ratio Gain / SplitInfo (C4.5)
7
+ Atribut kategorik (teks) dipecah menjadi satu cabang per nilai dan hanya dipakai sekali di setiap jalur.
8
+ Atribut numerik dipecah menjadi dua cabang: <= ambang dan > ambang, dengan ambang = titik tengah dua nilai berurutan.
9
+ """
10
+
11
+ import math
12
+
13
+ from ._dasar import ModelDasar, angka, bersih, ke_list, ke_tabel
14
+ from . import _warna as W
15
+
16
+
17
+ def _impurity(hitung, n, kriteria):
18
+ if n == 0:
19
+ return 0.0
20
+ if kriteria == "gini":
21
+ v = 1.0 - sum((c / n) ** 2 for c in hitung)
22
+ else:
23
+ v = -sum((c / n) * math.log2(c / n) for c in hitung if c)
24
+ return 0.0 if abs(v) < 1e-12 else v
25
+
26
+
27
+ def _g(v):
28
+ return f"{v:g}" if isinstance(v, float) else str(v)
29
+
30
+
31
+ class Simpul:
32
+ """Satu simpul pohon. Daun bila anak kosong."""
33
+
34
+ def __init__(self, nomor, kedalaman, n, distribusi, impurity, kelas):
35
+ self.nomor = nomor
36
+ self.kedalaman = kedalaman
37
+ self.n = n
38
+ self.distribusi = distribusi # jumlah data per kelas, urut classes_
39
+ self.impurity = impurity
40
+ self.kelas = kelas
41
+ self.atribut = None # indeks atribut pemecah
42
+ self.jenis = None # 'numerik' / 'kategorik'
43
+ self.ambang = None
44
+ self.gain = 0.0
45
+ self.anak = [] # list (label_cabang, nilai_kunci, Simpul)
46
+
47
+ @property
48
+ def daun(self):
49
+ return not self.anak
50
+
51
+
52
+ class DecisionTree(ModelDasar):
53
+ """Decision Tree untuk klasifikasi.
54
+
55
+ Parameter
56
+ ---------
57
+ criterion : 'gini' (bawaan), 'entropy', atau 'gain_ratio'
58
+ max_depth : int atau None. Kedalaman maksimum pohon.
59
+ min_samples_split : jumlah data minimum agar simpul boleh dipecah (bawaan 2).
60
+ min_samples_leaf : jumlah data minimum di setiap cabang hasil pemecahan (bawaan 1).
61
+ min_gain : penurunan impurity minimum agar pemecahan dilakukan (bawaan 0.0).
62
+ atribut_kategorik : 'auto' (bawaan: kolom berisi teks dianggap kategorik), atau daftar nama/indeks kolom.
63
+
64
+ Atribut setelah fit
65
+ -------------------
66
+ classes_, tree_ (simpul akar), feature_importances_, jenis_atribut_, n_features_in_, feature_names_in_.
67
+ """
68
+
69
+ _terima_teks = True
70
+
71
+ def __init__(self, criterion="gini", max_depth=None, min_samples_split=2, min_samples_leaf=1, min_gain=0.0,
72
+ atribut_kategorik="auto"):
73
+ self.criterion = criterion
74
+ self.max_depth = max_depth
75
+ self.min_samples_split = min_samples_split
76
+ self.min_samples_leaf = min_samples_leaf
77
+ self.min_gain = min_gain
78
+ self.atribut_kategorik = atribut_kategorik
79
+
80
+ # ------------------------------------------------------------------ pelatihan
81
+ def fit(self, X, y):
82
+ """Bangun pohon dari data X (baris x atribut) dan label y."""
83
+ if self.criterion not in ("gini", "entropy", "gain_ratio"):
84
+ raise ValueError("criterion harus 'gini', 'entropy', atau 'gain_ratio'.")
85
+ self._nama_label = str(y.name) if getattr(y, "name", None) is not None else "kelas"
86
+ baris, nama = ke_tabel(X)
87
+ y = [bersih(v) for v in ke_list(y)]
88
+ if len(y) != len(baris):
89
+ raise ValueError(f"Jumlah baris X ({len(baris)}) tidak sama dengan jumlah label y ({len(y)}).")
90
+ d = len(baris[0])
91
+ self._nama = nama or [f"x{k + 1}" for k in range(d)]
92
+ jenis = self._tentukan_jenis(baris, d)
93
+ data = []
94
+ for i, r in enumerate(baris):
95
+ rr = []
96
+ for k, v in enumerate(r):
97
+ v = bersih(v)
98
+ if jenis[k] == "numerik":
99
+ if not angka(v) or v != v:
100
+ raise ValueError(f"Baris ke-{i}, atribut '{self._nama[k]}' berisi {v!r}. Atribut numerik tidak "
101
+ "boleh kosong/teks; isi nilai kosong dulu atau tandai sebagai kategorik.")
102
+ v = float(v)
103
+ elif v is None or v != v:
104
+ raise ValueError(f"Baris ke-{i}, atribut '{self._nama[k]}' kosong. Isi nilai kosong dulu.")
105
+ rr.append(v)
106
+ data.append(rr)
107
+ unik = list(dict.fromkeys(y))
108
+ try:
109
+ self.classes_ = sorted(unik)
110
+ except TypeError:
111
+ self.classes_ = sorted(unik, key=str)
112
+ self._ik = {k: i for i, k in enumerate(self.classes_)}
113
+ self._data, self._y, self.jenis_atribut_ = data, y, dict(zip(self._nama, jenis))
114
+ self._jenis = jenis
115
+ self.n_features_in_ = d
116
+ if nama:
117
+ self.feature_names_in_ = nama
118
+ self._penting = [0.0] * d
119
+ self._nomor = 0
120
+ self.tree_ = self._bangun(list(range(len(y))), 0, frozenset())
121
+ total = sum(self._penting)
122
+ self.feature_importances_ = [v / total if total > 0 else 0.0 for v in self._penting]
123
+ self.terlatih_ = True
124
+ return self
125
+
126
+ def _tentukan_jenis(self, baris, d):
127
+ if self.atribut_kategorik == "auto":
128
+ return ["numerik" if all(angka(bersih(r[k])) for r in baris) else "kategorik" for k in range(d)]
129
+ pilih = set()
130
+ for a in self.atribut_kategorik:
131
+ if isinstance(a, int):
132
+ pilih.add(a)
133
+ elif a in self._nama:
134
+ pilih.add(self._nama.index(a))
135
+ else:
136
+ raise ValueError(f"Atribut kategorik '{a}' tidak ada di X.")
137
+ return ["kategorik" if k in pilih else "numerik" for k in range(d)]
138
+
139
+ def _hitung(self, idx):
140
+ c = [0] * len(self.classes_)
141
+ for i in idx:
142
+ c[self._ik[self._y[i]]] += 1
143
+ return c
144
+
145
+ def _simpul(self, idx, kedalaman):
146
+ c = self._hitung(idx)
147
+ n = len(idx)
148
+ imp = _impurity(c, n, "gini" if self.criterion == "gini" else "entropy")
149
+ kelas = self.classes_[max(range(len(c)), key=lambda k: (c[k], -k))]
150
+ s = Simpul(self._nomor, kedalaman, n, c, imp, kelas)
151
+ self._nomor += 1
152
+ return s
153
+
154
+ def _bangun(self, idx, kedalaman, terpakai):
155
+ s = self._simpul(idx, kedalaman)
156
+ if (s.impurity <= 1e-12 or len(idx) < self.min_samples_split
157
+ or (self.max_depth is not None and kedalaman >= self.max_depth)):
158
+ return s
159
+ terbaik = self._cari_pemecah(idx, s, terpakai)
160
+ if terbaik is None:
161
+ return s
162
+ skor, gain, k, ambang, kelompok = terbaik
163
+ if gain <= self.min_gain or gain <= 1e-12:
164
+ return s
165
+ s.atribut, s.jenis, s.ambang, s.gain = k, self._jenis[k], ambang, gain
166
+ self._penting[k] += len(idx) / len(self._y) * gain
167
+ terpakai2 = terpakai | {k} if s.jenis == "kategorik" else terpakai
168
+ for label, kunci, anak_idx in kelompok:
169
+ s.anak.append((label, kunci, self._bangun(anak_idx, kedalaman + 1, terpakai2)))
170
+ return s
171
+
172
+ def _cari_pemecah(self, idx, s, terpakai):
173
+ n, I = len(idx), s.impurity
174
+ dasar = "gini" if self.criterion == "gini" else "entropy"
175
+ m = len(self.classes_)
176
+ terbaik = None
177
+ for k in range(self.n_features_in_):
178
+ if self._jenis[k] == "kategorik":
179
+ if k in terpakai:
180
+ continue
181
+ grup = {}
182
+ for i in idx:
183
+ grup.setdefault(self._data[i][k], []).append(i)
184
+ if len(grup) < 2 or any(len(g) < self.min_samples_leaf for g in grup.values()):
185
+ continue
186
+ anak_I = sum(len(g) / n * _impurity(self._hitung(g), len(g), dasar) for g in grup.values())
187
+ gain = I - anak_I
188
+ split = -sum(len(g) / n * math.log2(len(g) / n) for g in grup.values())
189
+ skor = gain / split if self.criterion == "gain_ratio" else gain
190
+ if terbaik is None or skor > terbaik[0] + 1e-12:
191
+ try:
192
+ urut = sorted(grup)
193
+ except TypeError:
194
+ urut = sorted(grup, key=str)
195
+ terbaik = (skor, gain, k, None, [(f"= {_g(v)}", v, grup[v]) for v in urut])
196
+ else:
197
+ urut = sorted(idx, key=lambda i: self._data[i][k])
198
+ kiri = [0] * m
199
+ kanan = self._hitung(idx)
200
+ for p in range(n - 1):
201
+ i = urut[p]
202
+ c = self._ik[self._y[i]]
203
+ kiri[c] += 1
204
+ kanan[c] -= 1
205
+ v, v2 = self._data[i][k], self._data[urut[p + 1]][k]
206
+ nk = p + 1
207
+ if v2 <= v or nk < self.min_samples_leaf or n - nk < self.min_samples_leaf:
208
+ continue
209
+ gain = I - (nk / n * _impurity(kiri, nk, dasar) + (n - nk) / n * _impurity(kanan, n - nk, dasar))
210
+ if self.criterion == "gain_ratio":
211
+ pk = nk / n
212
+ split = -(pk * math.log2(pk) + (1 - pk) * math.log2(1 - pk))
213
+ skor = gain / split
214
+ else:
215
+ skor = gain
216
+ if terbaik is None or skor > terbaik[0] + 1e-12:
217
+ terbaik = (skor, gain, k, (v + v2) / 2, nk, urut)
218
+ if terbaik is not None and len(terbaik) == 6:
219
+ skor, gain, kk, t, nk, ur = terbaik
220
+ terbaik = (skor, gain, kk, t, [(f"<= {_g(t)}", "kiri", ur[:nk]), (f"> {_g(t)}", "kanan", ur[nk:])])
221
+ return terbaik
222
+
223
+ # ------------------------------------------------------------------ prediksi
224
+ def _daun_untuk(self, x):
225
+ s = self.tree_
226
+ while not s.daun:
227
+ v = x[s.atribut]
228
+ if s.jenis == "numerik":
229
+ s = s.anak[0][2] if float(v) <= s.ambang else s.anak[1][2]
230
+ else:
231
+ cocok = [a for _, kunci, a in s.anak if kunci == v]
232
+ if not cocok: # nilai belum pernah dilihat: pakai kelas mayoritas simpul ini
233
+ return s
234
+ s = cocok[0]
235
+ return s
236
+
237
+ def _baris(self, X):
238
+ self._cek_terlatih()
239
+ baris, _ = ke_tabel(X)
240
+ if len(baris[0]) != self.n_features_in_:
241
+ raise ValueError(f"X punya {len(baris[0])} atribut, padahal model dilatih dengan {self.n_features_in_}.")
242
+ return [[bersih(v) for v in r] for r in baris]
243
+
244
+ def predict(self, X):
245
+ """Kelas prediksi tiap baris X."""
246
+ return [self._daun_untuk(x).kelas for x in self._baris(X)]
247
+
248
+ def predict_proba(self, X):
249
+ """Peluang tiap kelas (urut classes_) = proporsi data latih di daun tujuan."""
250
+ out = []
251
+ for x in self._baris(X):
252
+ s = self._daun_untuk(x)
253
+ out.append([c / s.n for c in s.distribusi])
254
+ return out
255
+
256
+ def get_depth(self):
257
+ self._cek_terlatih()
258
+ return max(s.kedalaman for s in self._semua())
259
+
260
+ def get_n_leaves(self):
261
+ self._cek_terlatih()
262
+ return sum(1 for s in self._semua() if s.daun)
263
+
264
+ def _semua(self):
265
+ tumpuk = [self.tree_]
266
+ while tumpuk:
267
+ s = tumpuk.pop()
268
+ yield s
269
+ tumpuk.extend(a for _, _, a in reversed(s.anak))
270
+
271
+ # ------------------------------------------------------------------ informasi
272
+ def _syarat(self, s, label):
273
+ return f"{self._nama[s.atribut]} {label}"
274
+
275
+ def teks(self):
276
+ """Tampilkan pohon sebagai teks berjenjang."""
277
+ self._cek_terlatih()
278
+ nm = "gini" if self.criterion == "gini" else "entropy"
279
+
280
+ def jalan(s, awal):
281
+ for label, _, a in s.anak:
282
+ if a.daun:
283
+ print(f"{awal}|--- {self._syarat(s, label)}: {a.kelas} (n = {a.n}, {self._dist(a)})")
284
+ else:
285
+ print(f"{awal}|--- {self._syarat(s, label)}")
286
+ jalan(a, awal + "| ")
287
+
288
+ if self.tree_.daun:
289
+ print(f"Pohon hanya satu daun: {self.tree_.kelas} (n = {self.tree_.n})")
290
+ else:
291
+ print(f"Akar: {self._nama[self.tree_.atribut]} ({nm} = {self.tree_.impurity:.3f}, n = {self.tree_.n})")
292
+ jalan(self.tree_, "")
293
+
294
+ def _dist(self, s):
295
+ return ", ".join(f"{k}: {c}" for k, c in zip(self.classes_, s.distribusi))
296
+
297
+ def aturan(self):
298
+ """Tampilkan aturan JIKA ... MAKA ... dari setiap daun."""
299
+ self._cek_terlatih()
300
+ hasil = []
301
+
302
+ def jalan(s, syarat):
303
+ if s.daun:
304
+ hasil.append((syarat, s))
305
+ return
306
+ for label, _, a in s.anak:
307
+ jalan(a, syarat + [self._syarat(s, label)])
308
+
309
+ jalan(self.tree_, [])
310
+ for no, (syarat, s) in enumerate(hasil, 1):
311
+ jika = " DAN ".join(syarat) if syarat else "(semua data)"
312
+ benar = s.distribusi[self._ik[s.kelas]]
313
+ print(f"Aturan {no}: JIKA {jika} MAKA {self._nama_label} = {s.kelas} ({benar}/{s.n} data latih)")
314
+
315
+ def plot(self, ax=None, judul=None, ukuran_huruf=9, tampil_impurity=True):
316
+ """Gambar pohon keputusan: simpul pemecah, cabang, dan daun berwarna sesuai kelas."""
317
+ import matplotlib.pyplot as plt
318
+ from matplotlib.patches import FancyBboxPatch, Patch
319
+
320
+ self._cek_terlatih()
321
+ nm = "gini" if self.criterion == "gini" else "entropy"
322
+ posisi, daun_ke = {}, [0]
323
+
324
+ def tata(s):
325
+ if s.daun:
326
+ posisi[s.nomor] = (daun_ke[0], -s.kedalaman * 1.5)
327
+ daun_ke[0] += 1
328
+ else:
329
+ for _, _, a in s.anak:
330
+ tata(a)
331
+ xs = [posisi[a.nomor][0] for _, _, a in s.anak]
332
+ posisi[s.nomor] = ((xs[0] + xs[-1]) / 2, -s.kedalaman * 1.5)
333
+
334
+ tata(self.tree_)
335
+ n_daun, dalam = daun_ke[0], self.get_depth()
336
+ if ax is None:
337
+ _, ax = plt.subplots(figsize=(max(6.5, 2.1 * n_daun), max(3.2, 1.75 * (dalam + 1))))
338
+ lb, tg = 0.88, 0.9
339
+
340
+ def gambar(s):
341
+ x, y = posisi[s.nomor]
342
+ for label, _, a in s.anak:
343
+ xa, ya = posisi[a.nomor]
344
+ ax.plot([x, xa], [y - tg / 2, ya + tg / 2], color=W.REDUP, lw=1.3, zorder=1)
345
+ ax.text((x + xa) / 2, (y - tg / 2 + ya + tg / 2) / 2, label, ha="center", va="center",
346
+ fontsize=ukuran_huruf - 0.5, color=W.TINTA, zorder=3,
347
+ bbox=dict(boxstyle="round,pad=0.25", fc="white", ec=W.GARIS, lw=0.8))
348
+ gambar(a)
349
+ if s.daun:
350
+ k = self._ik[s.kelas]
351
+ isi, tepi, atas = W.muda(k), W.warna(k), f"{self._nama_label} = {s.kelas}"
352
+ else:
353
+ isi, tepi, atas = "#F6F8FB", W.TINTA, self._nama[s.atribut]
354
+ ax.add_patch(FancyBboxPatch((x - lb / 2, y - tg / 2), lb, tg, boxstyle="round,pad=0.02,rounding_size=0.08",
355
+ fc=isi, ec=tepi, lw=1.6, zorder=2))
356
+ ax.text(x, y + tg * 0.22, atas, ha="center", va="center", fontsize=ukuran_huruf + 0.5, fontweight="bold",
357
+ color=W.TINTA, zorder=3)
358
+ baris2 = f"n = {s.n} [{', '.join(str(c) for c in s.distribusi)}]"
359
+ if tampil_impurity:
360
+ baris2 = f"{nm} = {s.impurity:.3f}\n" + baris2
361
+ ax.text(x, y - tg * 0.17, baris2, ha="center", va="center", fontsize=ukuran_huruf - 1, color=W.REDUP,
362
+ zorder=3, linespacing=1.3)
363
+
364
+ gambar(self.tree_)
365
+ ax.set_xlim(-0.6, n_daun - 0.4)
366
+ ax.set_ylim(-dalam * 1.5 - 0.7, 0.7)
367
+ ax.axis("off")
368
+ ax.legend(handles=[Patch(fc=W.muda(i), ec=W.warna(i), label=f"{self._nama_label} = {k}")
369
+ for i, k in enumerate(self.classes_)],
370
+ loc="upper left", fontsize=ukuran_huruf, frameon=False, bbox_to_anchor=(0, 1.02))
371
+ if judul is None:
372
+ judul = (f"Decision Tree ({ {'gini': 'Gini index', 'entropy': 'information gain', 'gain_ratio': 'gain ratio'}[self.criterion] }), "
373
+ f"kedalaman {dalam}, {n_daun} daun [angka = jumlah data per kelas: {', '.join(map(str, self.classes_))}]")
374
+ ax.set_title(judul, fontsize=ukuran_huruf + 1.5, color=W.TINTA)
375
+ return ax
@@ -0,0 +1,124 @@
1
+ Metadata-Version: 2.4
2
+ Name: jokodm
3
+ Version: 0.1.0
4
+ Summary: Algoritma data mining yang ditulis dari nol (SVM, Decision Tree) lengkap dengan visualisasi, bergaya scikit-learn.
5
+ Author-email: Joko Suntoro <jokosuntoro@gmail.com>
6
+ License-Expression: LicenseRef-Jokodm
7
+ Keywords: data mining,machine learning,svm,decision tree,pembelajaran,visualisasi
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Operating System :: OS Independent
10
+ Classifier: Intended Audience :: Education
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
13
+ Classifier: Natural Language :: Indonesian
14
+ Requires-Python: >=3.8
15
+ Description-Content-Type: text/markdown
16
+ License-File: LICENSE
17
+ Requires-Dist: matplotlib>=3.3
18
+ Provides-Extra: uji
19
+ Requires-Dist: pytest; extra == "uji"
20
+ Requires-Dist: scikit-learn>=1.3; extra == "uji"
21
+ Requires-Dist: numpy; extra == "uji"
22
+ Requires-Dist: pandas; extra == "uji"
23
+ Dynamic: license-file
24
+
25
+ # jokodm
26
+
27
+ Algoritma data mining yang **ditulis dari nol** (tanpa scikit-learn dan tanpa numpy), lengkap dengan
28
+ **visualisasi** untuk memahami cara kerjanya. Cara pakainya mengikuti gaya scikit-learn (`fit`, `predict`),
29
+ jadi pembagian data dan evaluasi tetap bisa memakai scikit-learn.
30
+
31
+ Versi ini berisi:
32
+
33
+ | Algoritma | Visualisasi | Informasi lain |
34
+ |---|---|---|
35
+ | `SVM` (klasifikasi 2 kelas, kernel linear / rbf / poly) | data, hyperplane, garis margin, support vector, titik baru | `ringkasan()`: persamaan f(x), lebar margin, daftar support vector |
36
+ | `DecisionTree` (Gini, information gain, gain ratio) | pohon keputusan berwarna per kelas | `teks()`, `aturan()` (JIKA ... MAKA ...), `feature_importances_` |
37
+
38
+ ## Pemasangan
39
+
40
+ ```python
41
+ !pip install jokodm # di Google Colab / Jupyter
42
+ ```
43
+
44
+ atau di terminal: `pip install jokodm`
45
+
46
+ ## SVM
47
+
48
+ ```python
49
+ import pandas as pd
50
+ from jokodm import SVM
51
+
52
+ df = pd.DataFrame({
53
+ "skor_omzet": [4, 0, 5, 2, 4, 1, 5, 0, 3, 1],
54
+ "skor_bayar": [2, 2, 2, 1, 4, 0, 3, 4, 5, 2],
55
+ "kelayakan": ["Layak", "Tidak Layak", "Layak", "Tidak Layak", "Layak",
56
+ "Tidak Layak", "Layak", "Tidak Layak", "Layak", "Tidak Layak"],
57
+ })
58
+ X, y = df[["skor_omzet", "skor_bayar"]], df["kelayakan"]
59
+
60
+ model = SVM(C=1e6, kelas_positif="Layak") # C sangat besar = hard margin
61
+ model.fit(X, y)
62
+ model.ringkasan() # f(x) = 0.8000*skor_omzet + 0.4000*skor_bayar - 3.0000, margin 2.2361
63
+ model.plot(baru=[[3, 3]]) # hyperplane, margin, support vector, dan titik baru
64
+ model.predict([[3, 3]]) # ['Layak']
65
+ ```
66
+
67
+ Atribut setelah `fit`: `coef_`, `intercept_`, `margin_`, `support_`, `support_vectors_`, `n_support_`,
68
+ `alpha_`, `persamaan_`. Untuk data yang tidak lurus, pakai `SVM(kernel="rbf", C=5)`; `plot()` menggambar
69
+ batas keputusan yang melengkung.
70
+
71
+ Parameter: `C` (bawaan 1.0), `kernel` (`"linear"`, `"rbf"`, `"poly"`), `gamma` (`"scale"`, `"auto"`, angka),
72
+ `degree`, `coef0`, `tol`, `max_iter`, `kelas_positif`.
73
+
74
+ ## Decision Tree
75
+
76
+ ```python
77
+ import pandas as pd
78
+ from jokodm import DecisionTree
79
+
80
+ df = pd.read_csv("play-tennis.csv") # outlook, temperature, humidity, windy, play
81
+ X, y = df.drop(columns=["play"]), df["play"]
82
+
83
+ pohon = DecisionTree(criterion="entropy") # "gini", "entropy" (ID3), "gain_ratio" (C4.5)
84
+ pohon.fit(X, y) # atribut teks dipakai langsung, tanpa one-hot
85
+ pohon.plot() # gambar pohon keputusan
86
+ pohon.aturan() # JIKA outlook = sunny DAN humidity = high MAKA play = no ...
87
+ pohon.teks()
88
+ pohon.feature_importances_
89
+ ```
90
+
91
+ Atribut kategorik (teks) dipecah menjadi satu cabang per nilai; atribut numerik dipecah menjadi
92
+ `<= ambang` dan `> ambang`. Parameter: `criterion`, `max_depth`, `min_samples_split`, `min_samples_leaf`,
93
+ `min_gain`, `atribut_kategorik`.
94
+
95
+ ## Bersama scikit-learn
96
+
97
+ ```python
98
+ from sklearn.model_selection import train_test_split, cross_val_score
99
+ from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
100
+ from jokodm import DecisionTree
101
+
102
+ X_latih, X_uji, y_latih, y_uji = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
103
+ model = DecisionTree(max_depth=3).fit(X_latih, y_latih)
104
+ y_pred = model.predict(X_uji)
105
+
106
+ print(accuracy_score(y_uji, y_pred))
107
+ print(confusion_matrix(y_uji, y_pred))
108
+ print(classification_report(y_uji, y_pred))
109
+ print(cross_val_score(DecisionTree(max_depth=3), X, y, cv=5))
110
+ ```
111
+
112
+ Model jokodm juga bisa dipakai di `GridSearchCV`. Untuk normalisasi data sebelum SVM, pakai
113
+ `MinMaxScaler` atau `StandardScaler` dari scikit-learn.
114
+
115
+ ## Ketepatan
116
+
117
+ Hasil jokodm diuji terhadap scikit-learn: SVM menghasilkan prediksi yang sama dengan `sklearn.svm.SVC`
118
+ (kernel linear, rbf, poly), dan Decision Tree menghasilkan pohon dengan jumlah daun dan akurasi yang sama
119
+ dengan `DecisionTreeClassifier` pada data iris, wine, dan breast cancer.
120
+
121
+ ## Lisensi
122
+
123
+ Hak Cipta (c) 2026 Joko Suntoro. Gratis dipakai untuk belajar, mengajar, penelitian, dan pekerjaan;
124
+ tidak boleh dijual ulang. Lihat berkas LICENSE. Kontak: jokosuntoro@gmail.com
@@ -0,0 +1,10 @@
1
+ jokodm/__init__.py,sha256=x717H9934_nE_khwKZyRsmloayPInF1GWfoZt9O9M_w,349
2
+ jokodm/_dasar.py,sha256=GFGaP1ShOiZlqGPOKct1VUcMVO41vR-nQ-1Vu6n_hRE,4477
3
+ jokodm/_warna.py,sha256=oa4cL4SPO78nj_kOJdtKk8M8mze-PkFAfHJShLrAJ0M,507
4
+ jokodm/svm.py,sha256=-n1vAaVU8pijzDW_8HZRbt_NrcMjrkBwNRdIGeQYmwk,16646
5
+ jokodm/tree.py,sha256=8ONB0VYnG1DdU-Wyn-UJ3CLerb8ytgXMm5FhQH7H0t0,16292
6
+ jokodm-0.1.0.dist-info/licenses/LICENSE,sha256=bf84DIuY2a-9ljJcQOhNTTKWXxqwsiwgAGiU1DPiGR0,594
7
+ jokodm-0.1.0.dist-info/METADATA,sha256=wApIpYtI6T-IXna4LdySiQDf2deI2KYsiBaGnZAGxvA,5036
8
+ jokodm-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
9
+ jokodm-0.1.0.dist-info/top_level.txt,sha256=zpo3fmmQTUzmRd-sCBuoMLyF8vLGqlOV8CV6UDjYgsk,7
10
+ jokodm-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,13 @@
1
+ Hak Cipta (c) 2026 Joko Suntoro
2
+
3
+ Library jokodm boleh dipasang dan dipakai secara gratis untuk belajar, mengajar, penelitian,
4
+ dan pekerjaan, termasuk di dalam kode atau tugas milik Anda sendiri.
5
+
6
+ Tanpa izin tertulis dari pemegang hak cipta, Anda tidak boleh:
7
+ 1. menjual ulang library ini, baik utuh maupun sebagian;
8
+ 2. menerbitkan ulang kode library ini atas nama orang lain atau mengakuinya sebagai karya sendiri.
9
+
10
+ Library ini diberikan "apa adanya", tanpa jaminan apa pun. Pemegang hak cipta tidak bertanggung jawab
11
+ atas kerugian yang timbul dari pemakaiannya.
12
+
13
+ Kontak: jokosuntoro@gmail.com
@@ -0,0 +1 @@
1
+ jokodm