-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmeans_accuracy.py
42 lines (32 loc) · 1.2 KB
/
kmeans_accuracy.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
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 4 11:15:38 2019
@author: aj-nok
"""
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 22 13:20:37 2019
@author: ajas
"""
import pandas as pd
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn import metrics
import matplotlib.pyplot as plt
pca = PCA(n_components=2)
model = KMeans(n_clusters=3)
df=pd.read_csv('iris.csv',usecols=['SepalLengthCm','SepalWidthCm','PetalLengthCm','PetalWidthCm'])
df_lab=pd.read_csv('iris.csv',usecols=['Species'])
df_lab.loc[df_lab['Species'] == 'Iris-setosa', 'Species'] = 0
df_lab.loc[df_lab['Species'] == 'Iris-versicolor', 'Species'] = 1
df_lab.loc[df_lab['Species'] == 'Iris-virginica', 'Species'] = 2
df = StandardScaler().fit_transform(df)
principalComponents = pca.fit_transform(df)
df_new = pd.DataFrame(data = principalComponents , columns = ['pc1', 'pc2'])
model.fit(df_new)
predictions = model.predict(df_new)
#score = metrics.accuracy_score(df_lab,predictions)
#print('Accuracy:{0:f}'.format(score))
fpr, tpr, thresholds = metrics.roc_curve(df_lab, predictions, pos_label=2)
print(metrics.auc(fpr, tpr))