Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scipy
- from sklearn.datasets import load_iris
- from sklearn.model_selection import train_test_split
- from sklearn.cluster import KMeans
- from sklearn.metrics import accuracy_score
- def find_permutation(n_clusters, real_labels, labels):
- permutation=[]
- for i in range(n_clusters):
- idx = labels == i
- # Choose the most common label among data points in the cluster
- new_label=scipy.stats.mode(real_labels[idx])[0][0]
- permutation.append(new_label)
- return permutation
- def plant_clustering():
- X , y = load_iris(return_X_y=True)
- cluss = KMeans( 3 , random_state = 0 )
- cluss.fit( X )
- y_pred = cluss.predict( X )
- #print( y_pred )
- #print( y )
- perm = find_permutation( 3 , y , y_pred )
- #print(perm )
- acc = accuracy_score( y , [ perm[label] for label in cluss.labels_])
- #print(acc)
- return acc
- def main():
- print(plant_clustering())
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement