Skip to content

Instantly share code, notes, and snippets.

@ijpulidos
Created October 24, 2019 16:21
Show Gist options
  • Save ijpulidos/45702a52645af8a0dc4136dc5acc958f to your computer and use it in GitHub Desktop.
Save ijpulidos/45702a52645af8a0dc4136dc5acc958f to your computer and use it in GitHub Desktop.
from sklearn import datasets
from sklearn.metrics import accuracy_score
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import numpy as np
# Use iris dataset included in sklearn
iris = datasets.load_iris
k = 3 # Number of clusters
km = KMeans(k, random_state=0) # make the randomness deterministic for centroid (reproducibility)
X = iris()["data"]
Y = iris()["target"]
y_kmeans = km.fit(X)
print(Y)
print(km.labels_)
acc = accuracy_score(Y, km.labels_)
print("accuracy before transforming:", acc)
# Transforming data, where Y is 1 put 0 and viceversa.
# This always works because of determinism in KMeans by random_state parameter.
ind1 = np.where(Y==1)
ind0 = np.where(Y==0)
Y[ind1] = 0
Y[ind0] = 1
acc = accuracy_score(Y, km.labels_)
print("accuracy after transforming:", acc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment