-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn_diabetes.py
133 lines (91 loc) · 4.33 KB
/
knn_diabetes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import seaborn as sns
from time import time
# ============================================================================
df = pd.read_csv('rotated_diabetes.csv', delimiter = ',')
X = df.drop('Outcome', axis = 1).values
y = df['Outcome'].values
# ============================================================================
pd.options.display.max_columns = 10
print(df.describe())
print()
# ----------------------------------------------------------------------------
plt.title("Jumlah Kasus Diabetes")
sns.countplot(x = "Outcome", data = df)
plt.savefig('distribusi_label_diabetes.png', dpi=300)
plt.show()
# ----------------------------------------------------------------------------
g = sns.pairplot(df, hue = "Outcome", vars = df.columns[:-1])
g.fig.suptitle("Pairplot Seluruh Fitur Dataset Diacak", y=1.05, fontsize=75)
plt.savefig('distribusi_kolom_diabetes_diacak.png', dpi=300)
plt.show()
# ----------------------------------------------------------------------------
plt.title("Heatmap Korelasi Antar Variabel Dataset Diacak")
ax = sns.heatmap(df.corr(), annot = True, cmap = 'RdYlGn')
ax.set_xticklabels(ax.get_xticklabels(), rotation = 23, ha="right")
ax.set_yticklabels(ax.get_yticklabels(), rotation = 60, ha="right")
plt.tight_layout()
plt.savefig('heatmap_diabetes_diacak.png', dpi=300)
plt.show()
# ============================================================================
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=42, stratify = y)
# ----------------------------------------------------------------------------
neighbors = np.arange(1, 11)
train_accuracy = np.empty(len(neighbors))
test_accuracy = np.empty(len(neighbors))
for i, k in enumerate(neighbors):
knn = KNeighborsClassifier(n_neighbors = k)
knn.fit(X_train, y_train)
#Compute accuracy on the training set
train_accuracy[i] = knn.score(X_train, y_train)
#Compute accuracy on the test set
test_accuracy[i] = knn.score(X_test, y_test)
# ----------------------------------------------------------------------------
plt.title('Akurasi Model KNN Dataset Diacak')
plt.plot(neighbors, test_accuracy, label = 'Akurasi Test Set')
plt.plot(neighbors, train_accuracy, label = 'Akurasi Training Set')
plt.legend()
plt.xlabel('Jumlah Tetangga (k)')
plt.ylabel('Akurasi')
plt.savefig('plot_akurasi_diabetes_diacak.png', dpi=300)
plt.show()
# ----------------------------------------------------------------------------
print("Akurasi setiap K pada training set dataset diacak: ")
for i, accuracy in enumerate(train_accuracy):
print(str(i + 1) + ": " + str(accuracy))
print()
# ----------------------------------------------------------------------------
print("Akurasi setiap K pada test set dataset diacak: ")
for i, accuracy in enumerate(test_accuracy):
print(str(i + 1) + ": " + str(accuracy))
print()
# ----------------------------------------------------------------------------
highest_test_accuracy = test_accuracy.max()
k_highest_test_accuracy = np.argmax(test_accuracy) + 1
print("K terbaik adalah " + str(k_highest_test_accuracy) + " dengan akurasi test set sebesar " + str(highest_test_accuracy))
print()
# ============================================================================
start_time = time()
knn = KNeighborsClassifier(n_neighbors = k_highest_test_accuracy)
knn.fit(X_train, y_train)
print("--- Waktu yang dibutuhkan untuk melatih model adalah %s detik ---" % (time() - start_time))
print()
print("Akurasi pada model KNN yang digunakan: " + str(knn.score(X_test, y_test)))
print()
# ----------------------------------------------------------------------------
start_time = time()
y_pred = knn.predict(X_test)
print("--- Waktu yang dibutuhkan untuk melakukan prediksi adalah %s detik ---" % (time() - start_time))
print()
plt.title('Confusion Matrix pada Test Set Dataset Diacak')
sns.heatmap(confusion_matrix(y_pred, y_test), annot = True, annot_kws={"size": 16}, fmt='g')
plt.savefig('confusion_diabetes_diacak.png', dpi=300)
plt.show()
# ============================================================================