Klasifikasi dengan Decision Tree¶
Ada beberapa model/teknik yang dapat digunakan untuk klasifikasi yaitu :
- Decision Tree based Methods
- Rule based Method
- Neural Networks
- Naive Bayes and Bayesian Belief Networks
- Support Vektor Machines
Akan tetapi disini hanya akan dibahas tentang pengklasifikasian dengan teknik Decision Tree based Methos (Metode berbasis pohon keputusan) .
Decision Tree based Model¶
Untuk menentukan akar dari decision tree kita harus menentukan attribut yang memiliki nilai information gain tertinggi.
Entropy¶
Entropy merupakan sebagai ukuran gangguan atau ketidakmurnian suatu sistem dalam banyak contoh. Kita dapat menghitung entorpy dengan rumus berikut. $$ E(S) = \sum \limits_{i=1}^{n} -P_i\text log_2 \text P_i $$
Dimana :
S = Kemurnian suatu data.
P = Probabilitas kelas.
Information Gain¶
Information gain (IG) merupakan ukuran informasi yang diberikan oleh fitur kepada kita tentang kelas. Information gain dinyatakan dengan. $$ Gain(S,A) = entropy(S)-\sum_{i=1}^n \frac{|s_i|}{|s|} \times entropy(S_i) $$ Dimana :
|S| = Banyaknya data
Si = Niliai dari attribut
A = Attribut
Manual Klasifikasi Decision Tree¶
Berikut merupakan sebuah data yang akan digunakan sebagai data training dalam klasifikasi ini.
| Node | Attibut | Value | Jumlah Kasus (S) | C0 | C1 |
|---|---|---|---|---|---|
| 1 | Total | 20 | 10 | 10 | |
| Gender | F | 10 | 6 | 4 | |
| M | 10 | 4 | 6 | ||
| Car Type | Family | 4 | 8 | 0 | |
| Sport | 8 | 1 | 7 | ||
| Luxury | 8 | ||||
| Shirt Size | Small | 5 | 3 | 2 | |
| Medium | 7 | 3 | 4 | ||
| Large | 4 | 2 | 2 | ||
| Extra Large | 4 | 2 | 2 |
$ Entropy(S) = -{10\over20} \times ^2log_{10\over20} + -{10\over20} \times ^2log_{10\over20} $
$ E(S) = 1 $
Menghitung information gain dari fitur Gender
$ G(S,Gender) = 1 - {10\over20} \times E(6,4) + {10\over20} \times E(4,6) $
$ = 1 - {10\over20} \times 0.97095 + {10\over20} \times 0.97095 $
$ = 1 - 0.97095 = 0.02905 $
Menghitung information gain dari fitur Car Type
$ G(S,Car Type) = 1 - {4\over20} \times E(1,3) + {8\over20} \times E(8,0) + {8\over20} \times E(1,7) $
$ = 1 - {4\over20} \times 0.81128 + {8\over20} \times 0 + {8\over20} \times 0.54356 $
$ = 1 - 0.37968 = 0.62032 $
Menghitung information gain dari fitur Shirt Size
$ G(S,Shirt Size) = 1 - {5\over20} \times E(3,2) + {7\over20} \times E(3,4) + {4\over20} \times E(2,2) + {4\over20} \times E(2,2) $
$ = 1 - {5\over20} \times 0.97095 + {7\over20} \times 0.98523 + {4\over20} \times 1 + {8\over20} \times 1 $
$ = 1 - 0.98756 = 0.12440 $

| Node | Attribut | Value | Jumlah Kasus (S) | C0 | C1 |
|---|---|---|---|---|---|
| 2 | Car Type=Family | 4 | 1 | 3 | |
| Gender | M | 4 | 1 | 3 | |
| Shirt Size | Small | 1 | 1 | 0 | |
| Medium | 1 | 0 | 1 | ||
| Large | 1 | 0 | 1 | ||
| Extra Large | 1 | 0 | 1 |
$ Entropy(Family) = -{1\over4} \times ^2log_{1\over4} + -{3\over4} \times ^2log_{3\over4} $
$ E(Family) = 0,811278 $
Menghitung information gain dari fitur Gender
$ G(S,M) = 0,811278 - {4\over4} \times E(1,3) $
$ = 0,811278 - 1 \times 0,811278 $
$ = 0,811278 - 0,811278 = 0 $
Menghitung information gain dari fitur Shirt Size
$ G(S,M) = 0,811278 - {1\over4} \times E(1,0) + {1\over4} \times E(0,1) + {1\over4} \times E(0,1) + {1\over4} \times E(0,1) $
$ = 0,811278 - (({1\over4} \times 0) + ({1\over4} \times 0) + ({1\over4} \times 0) + ({1\over4} \times 0)) $
$ = 0,811278 - 0 = 0,811278 $

| Node | Attribut | Value | Jumlah Kasus (S) | C0 | C1 |
|---|---|---|---|---|---|
| 2 | Car Type=Sport | 8 | 8 | 0 | |
| Gender | M | 5 | 5 | 0 | |
| F | 3 | 3 | 0 | ||
| Shirt Size | Small | 3 | 3 | 0 | |
| Medium | 2 | 2 | 0 | ||
| Large | 1 | 1 | 0 | ||
| Extra Large | 2 | 2 | 0 |

Selanjutnya kita filter data tersebut dengan rule, seluruh nilai dari Car Type adalah Luxury.
| Node | Attribut | Value | Jumlah Kasus (S) | C0 | C1 |
|---|---|---|---|---|---|
| 2 | Car Type=Luxury | 8 | 1 | 7 | |
| Gender | M | 1 | 0 | 1 | |
| F | 7 | 1 | 6 | ||
| Shirt Size | Small | 2 | 0 | 1 | |
| Medium | 3 | 0 | 3 | ||
| Large | 2 | 1 | 1 | ||
| Extra Large | 1 | 0 | 1 |
$ Entropy(Luxury) = -{1\over8} \times ^2log_{1\over8} + -{7\over8} \times ^2log_{7\over8} $
$ E(Luxury) = 0,543564 $
Menghitung information gain dari fitur gender
$ G(S,M) = 0,543564 - {1\over8} \times E(0,1) + {7\over8} \times E(1,6) $
$ = 0,543564 - ({1\over8} \times 0) + ({7\over8}\times 0,591673)$
$ = 0,543564 - 0,517714 = 0,025851 $
Menghitung information gain dari fitur shirt size
$ G(S,M) = 0,543564 - {2\over8} \times E(0,1) + {3\over8} \times E(0,3) + {2\over8} \times E(1,1) + {1\over8} \times E(0,1) $
$ = 0,543564 - (({2\over8} \times 0) + ({3\over8} \times 0) + ({2\over8} \times 1) + ({1\over8} \times 0)) $
$ = 0,543564 - 0,25 =0,293564 $
Nilai information gain tertinggi pada perhitung yaitu Shirt Size. dan kita dapat membentuk decision tree seperti berikut:

Data yang difilter dengan rule, car type adalah family dan shirt size adalah small.
| Node | Attribut | Value | Jumlah Kasus (S) | C0 | C1 |
|---|---|---|---|---|---|
| 3 | Car Type=Family, Shirt Size=Small | 1 | 1 | 0 | |
| Gender | M | 1 | 1 | 0 |
Data yang difilter dengan rule, car type adalah family dan shirt size adalah medium.
| Node | Attribut | Value | Jumlah Kasus (S) | C0 | C1 |
|---|---|---|---|---|---|
| 3 | Car Type=Family, Shirt Size=Medium | 1 | 0 | 1 | |
| Gender | M | 1 | 0 | 1 |
Data yang difilter dengan rule, car type adalah family dan shirt size adalah large.
| Node | Attribut | Value | Jumlah Kasus (S) | C0 | C1 |
|---|---|---|---|---|---|
| 3 | Car Type=Family, Shirt Size=Large | 1 | 0 | 1 | |
| Gender | M | 1 | 0 | 1 |
Data yang difilter dengan rule, car type adalah family dan shirt size adalah extra large.
| Node | Attribut | Value | Jumlah Kasus (S) | C0 | C1 |
|---|---|---|---|---|---|
| 3 | Car Type=Family, Shirt Size=Extra Large | 1 | 0 | 1 | |
| Gender | M | 1 | 0 | 1 |
Data yang difilter dengan rule, car type adalah luxury dan shirt size adalah small.
| Node | Attribut | Value | Jumlah Kasus (S) | C0 | C1 |
|---|---|---|---|---|---|
| 3 | Car Type=Luxury, Shirt Size=Small | 2 | 0 | 2 | |
| Gender | F | 2 | 0 | 2 |
Data yang difilter dengan rule, car type adalah luxury dan shirt size adalah medium.
| Node | Attribut | Value | Jumlah Kasus (S) | C0 | C1 |
|---|---|---|---|---|---|
| 3 | Car Type=Luxury, Shirt Size=Medium | 3 | 0 | 3 | |
| Gender | F | 3 | 0 | 3 |
Data yang difilter dengan rule, car type adalah luxury dan shirt size adalah large.
| Node | Attribut | Value | Jumlah Kasus (S) | C0 | C1 |
|---|---|---|---|---|---|
| 3 | Car Type=Luxury, Shirt Size=Large | 2 | 1 | 1 | |
| Gender | F | 2 | 1 | 1 |
Data yang difilter dengan rule, car type adalah luxury dan shirt size adalah extra large.
| Node | Attribut | Value | Jumlah Kasus (S) | C0 | C1 |
|---|---|---|---|---|---|
| 3 | Car Type=Luxury, Shirt Size=Extra Large | 1 | 0 | 1 | |
| Gender | F | 1 | 0 | 1 |

Penerapan Klasifikasi Decision Tree Menggunakan Python¶
Alat dan Bahan¶
Berikut merupakan library yang harus di persiapkan:
- pandas, digunakan untuk data manajemen dan data analysis.
- scipy, merupakan library berisi kumpulan algoritma dan fungsi matematika.
Proses¶
import pandas as pd from sklearn.preprocessing import LabelEncoder from sklearn.tree import DecisionTreeClassifier, plot_tree data = pd.read_excel('klasifikasi_data.xlsx') df = pd.DataFrame(data) df.style.hide_index()
Maka akan menampilkan data dalam bentuk dataframe sebagai berikut:
| Customer ID | Gender | Car Type | Shirt Size | Class |
|---|---|---|---|---|
| 1 | M | Family | Small | C0 |
| 2 | M | Sports | Medium | C0 |
| 3 | M | Sports | Medium | C0 |
| 4 | M | Sports | Large | C0 |
| 5 | M | Sports | Extra Large | C0 |
| 6 | M | Sports | Extra Large | C0 |
| 7 | F | Sports | Small | C0 |
| 8 | F | Sports | Small | C0 |
| 9 | F | Sports | Medium | C0 |
| 10 | F | Luxury | Large | C0 |
| 11 | M | Family | Large | C1 |
| 12 | M | Family | Extra Large | C1 |
| 13 | M | Family | Medium | C1 |
| 14 | M | Luxury | Extra Large | C1 |
| 15 | F | Luxury | Small | C1 |
| 16 | F | Luxury | Small | C1 |
| 17 | F | Luxury | Medium | C1 |
| 18 | F | Luxury | Medium | C1 |
| 19 | F | Luxury | Medium | C1 |
| 20 | F | Luxury | Large | C1 |
Sklearn tidak bisa untuk memproses data berbentuk karakter, maka kita harus mengubah seluruh fitur dalam bentuk angka dengan bantuan dari modul sklearn yaitu LabelEncoder().
la = LabelEncoder() df['gender_n'] = la.fit_transform(df['Gender']) df['car_type_n'] = la.fit_transform(df['Car Type']) df['shirt_size_n'] = la.fit_transform(df['Shirt Size']) df['class_n'] = la.fit_transform(df['Class']) df.style.hide_index()
Ketika ditampilkan dalam bentuk dataframe maka akan muncul seperti berikut:
| Customer ID | Gender | Car Type | Shirt Size | Class | gender_n | car_type_n | shirt_size_n | class_n |
|---|---|---|---|---|---|---|---|---|
| 1 | M | Family | Small | C0 | 1 | 0 | 3 | 0 |
| 2 | M | Sports | Medium | C0 | 1 | 2 | 2 | 0 |
| 3 | M | Sports | Medium | C0 | 1 | 2 | 2 | 0 |
| 4 | M | Sports | Large | C0 | 1 | 2 | 1 | 0 |
| 5 | M | Sports | Extra Large | C0 | 1 | 2 | 0 | 0 |
| 6 | M | Sports | Extra Large | C0 | 1 | 2 | 0 | 0 |
| 7 | F | Sports | Small | C0 | 0 | 2 | 3 | 0 |
| 8 | F | Sports | Small | C0 | 0 | 2 | 3 | 0 |
| 9 | F | Sports | Medium | C0 | 0 | 2 | 2 | 0 |
| 10 | F | Luxury | Large | C0 | 0 | 1 | 1 | 0 |
| 11 | M | Family | Large | C1 | 1 | 0 | 1 | 1 |
| 12 | M | Family | Extra Large | C1 | 1 | 0 | 0 | 1 |
| 13 | M | Family | Medium | C1 | 1 | 0 | 2 | 1 |
| 14 | M | Luxury | Extra Large | C1 | 1 | 1 | 0 | 1 |
| 15 | F | Luxury | Small | C1 | 0 | 1 | 3 | 1 |
| 16 | F | Luxury | Small | C1 | 0 | 1 | 3 | 1 |
| 17 | F | Luxury | Medium | C1 | 0 | 1 | 2 | 1 |
| 18 | F | Luxury | Medium | C1 | 0 | 1 | 2 | 1 |
| 19 | F | Luxury | Medium | C1 | 0 | 1 | 2 | 1 |
| 20 | F | Luxury | Large | C1 | 0 | 1 | 1 | 1 |
inputs = df.drop(['Customer ID','Gender','Car Type','Shirt Size','Class','class_n'],axis='columns') target = df['class_n']
model = DecisionTreeClassifier(criterion = "entropy", random_state = 100) model.fit(inputs, target)
plot_tree(model.fit(inputs,target), max_depth=None, feature_names=['Customer ID','Gender','Car Type','Shirt Size',], class_names=['C0','C1'], label='all', filled=True, impurity=True, node_ids=False, proportion=True, rotate=True, rounded=True, precision=3, ax=None, fontsize=None)
Maka akan tampil binary tree seperti berikut.

Pada proses encode diatas, kita mendapatkan hasil seperti berikut.
| Gender | Car Type | Shirt Size | Class | ||||
|---|---|---|---|---|---|---|---|
| Value | Encode | Value | Encode | Value | Encode | Value | Encode |
| F | 0 | Family | 1 | Small | 3 | C0 | 0 |
| M | 1 | Sport | 2 | Medium | 2 | C1 | 1 |
| Luxury | 0 | Large | 1 | ||||
| Extra Large | 0 | ||||||
Jika terdapat data baru yang belum memiliki class, maka kita dapat melakukan prediksi.
Contoh :
| Customer ID | Gender | Car Type | Shirt Size | Class |
|---|---|---|---|---|
| 21 | F | Family | Medium | ? |
model.predict([[0,1,2]])
Output : array([1])
Dari prediksi diatas menghasilkan nilai [1] yang artinya data tersebut masuk dalam class C1.