You are on page 1of 3

Nama : Juan Candy Prima

NIM : G.211.17.0010

1. # Decision Tree Classifier


Line 1 berisikan komentar (# Decision Tree Classifier).
2. # Importing the libraries
Line 2 berisikan komentar (#Importing the libraries).
3. import numpy as np
4. import matplotlib.pyplot as plt
5. import pandas as pd
Line 3-5 mengimpor library yang diperlukan (numpy, matplotib.pyplot,pandas).
6. # Importing the datasets
Line 6 berisikan komentar (# Importing the datasets).

7. datasets = pd.read_csv('Social_Network_Ads.csv')
Line 7 mengimpor datasets.

8. X = datasets.iloc[:, [2,3]].values
Line 8 mendefinisikan variabel X dengan melakukan slicing.

9. Y = datasets.iloc[:, 4].values
Line 9 mendefinisikan variabel Y dengan melakukan slicing.

10. # Splitting the dataset into the Training set and Test set
Line 10 berisikan komentar (# Splitting the dataset into the Training set and Test set).

11. from sklearn.model_selection import train_test_split


Line 11 mengimpor library train_test_split dari sklearn.model_selection.

12. X_Train, X_Test, Y_Train, Y_Test = train_test_split(X, Y, test_size = 0.25, random_state = 0)


Line 12 membagi dataset ke dalam training dan test set.

13. # Feature Scaling


Line 13 berisikan komentar (# Feature Scaling).

14. from sklearn.preprocessing import StandardScaler


Line 14 mengimpor library StandardScaler untuk melakukan feature scaling.

15. sc_X = StandardScaler()


Line 15 mendefinisikan variabel sc untuk melakukan feature scaling.

16. X_Train = sc_X.fit_transform(X_Train)


17. X_Test = sc_X.transform(X_Test)
Line 16-17 melakukan feature scaling.

18. # Fitting the classifier into the Training set


Line 18 berisikan komentar (# Fitting the classifier into the Training set).

19. from sklearn.tree import DecisionTreeClassifier


Line 19 mengimpor library DecisionTreeClassifier dari sklearn.tree untuk membuat model DTC.

20. classifier = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)


Line 20 mendefinisikan variabel classifier untuk proses DTC. ‘entropy’ pembagiannya bersifat
homogen (menggunakan menggunakan maximum entropy).

21. # Predicting the test set results


Line 21 berisikan komentar (# Predicting the test set results).

22. Y_Pred = classifier.predict(X_Test)


Line 22 membuat model DTC untuk training set.

23. # Making the Confusion Matrix


Line 23 berisikan komentar (# Making the Confusion Matrix).

24. from sklearn.metrics import confusion_matrix


Line 24 mengimpor library confusion_matrix untuk melihat performa modelnya (membandingkan
training dan test set).

25. cm = confusion_matrix(Y_Test, Y_Pred)


Line 25 mendefinisikan y_pred untuk memprediksi hasil model DTC ke test set.

26. # Visualising the Training set results


Line 26 berisikan komentar (# Visualising the Training set results).

27. from matplotlib.colors import ListedColormap


28. X_Set, Y_Set = X_Train, Y_Train
29. X1, X2 = np.meshgrid(np.arange(start = X_Set[:, 0].min() - 1, stop = X_Set[:, 0].max() + 1, step = 0.01),
a. np.arange(start = X_Set[:, 1].min() - 1, stop = X_Set[:, 1].max() + 1, step = 0.01))
30. plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
31. alpha = 0.75, cmap = ListedColormap(('red', 'green')))
32. plt.xlim(X1.min(), X1.max())
33. plt.ylim(X2.min(), X2.max())
34. for i, j in enumerate(np.unique(Y_Set)):
35. plt.scatter(X_Set[Y_Set == j, 0], X_Set[Y_Set == j, 1],
a. c = ListedColormap(('red', 'green'))(i), label = j)
36. plt.title('Decision Tree Classifier (Training set)')
37. plt.xlabel('Age')
38. plt.ylabel('Estimated Salary')
39. plt.legend()
40. plt.show()
Line 28-40 perintah untuk visualisasi hasil model training set.
41. # Visualising the Test set results
Line 41 berisikan komentar (# Visualising the Test set result).

42. from matplotlib.colors import ListedColormap


43. X_Set, Y_Set = X_Test, Y_Test
44. X1, X2 = np.meshgrid(np.arange(start = X_Set[:, 0].min() - 1, stop = X_Set[:, 0].max() + 1, step = 0.01),
a. np.arange(start = X_Set[:, 1].min() - 1, stop = X_Set[:, 1].max() + 1, step = 0.01))
45. plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
46. alpha = 0.75, cmap = ListedColormap(('red', 'green')))
47. plt.xlim(X1.min(), X1.max())
48. plt.ylim(X2.min(), X2.max())
49. for i, j in enumerate(np.unique(Y_Set)):
50. plt.scatter(X_Set[Y_Set == j, 0], X_Set[Y_Set == j, 1],
a. c = ListedColormap(('red', 'green'))(i), label = j)
51. plt.title('Decision Tree Classifier (Test set)')
52. plt.xlabel('Age')
53. plt.ylabel('Estimated Salary')
54. plt.legend()
55. plt.show()
Line 42-55 adalah perintah untuk visualisasi hasil model test set.

You might also like