From 4c5b2c39d125ba015c534a2581d643433a6842c6 Mon Sep 17 00:00:00 2001 From: Abhinav Singh Date: Sat, 31 Oct 2020 17:35:22 +0530 Subject: [PATCH] Add k-means code in Python --- Python/ML Algorithms/k-means.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Python/ML Algorithms/k-means.py diff --git a/Python/ML Algorithms/k-means.py b/Python/ML Algorithms/k-means.py new file mode 100644 index 000000000..2dca71d88 --- /dev/null +++ b/Python/ML Algorithms/k-means.py @@ -0,0 +1,21 @@ +from sklearn.cluster import KMeans +import pandas as pd + +#reading the csv file having fees and experience as two columns +df = pd.read_csv('file_name.csv', sep=',', header=None) + +#checking the values read in a numpy array +print(df.values) + +# making two cluster for k-means +kmeans = KMeans(n_clusters=2) + +#passing the cluster values to the k-means +kmeans.fit(df.values) + +#checking the centres of the two clusters formed +print(kmeans.cluster_centers_) + +#checking the cluster that is formed +#here 1 signifies that the element is present in the cluster and 0 specifies it is not. +print(kmeans.labels_)