{"id":783,"date":"2018-07-07T09:08:27","date_gmt":"2018-07-07T09:08:27","guid":{"rendered":"http:\/\/muthu.co\/?p=783"},"modified":"2021-05-24T03:35:19","modified_gmt":"2021-05-24T03:35:19","slug":"k-means-on-iris-dataset","status":"publish","type":"post","link":"http:\/\/write.muthu.co\/k-means-on-iris-dataset\/","title":{"rendered":"K-Means on Iris Dataset"},"content":{"rendered":"\n

Read my previous post<\/a> to understand how K-Means algorithm works. In this post I will try to run the K-Means on Iris dataset to classify our 3 classes of flowers, Iris setosa, Iris versicolor, Iris virginica (our classess) using the flowers sepal-length, sepal-width, petal-length and petal-width (our features)<\/p>\n\n\n\n

Getting data:<\/p>\n\n\n\n

import numpy as np  \nimport matplotlib.pyplot as plt  \nimport pandas as pd\n\nurl = \"https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data\"\n#the imported dataset does not have the required column names so lets add it\ncolnames = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'Class']\nirisdata = pd.read_csv(url, names=colnames)<\/code><\/pre>\n\n\n\n

describe the data:<\/p>\n\n\n\n

irisdata.head()<\/code><\/pre>\n\n\n\n
\"\"<\/a><\/figure><\/div>\n\n\n\n

Converting the class names into numerical categories for analysis.<\/p>\n\n\n\n

irisdata['Class'] = pd.Categorical(irisdata[\"Class\"])\nirisdata[\"Class\"] = irisdata[\"Class\"].cat.codes<\/code><\/pre>\n\n\n\n

Preparing our dataset:<\/p>\n\n\n\n

X = irisdata.values[:, 0:4]\ny = irisdata.values[:, 4]<\/code><\/pre>\n\n\n\n

Running K-Means on it:<\/p>\n\n\n\n

from sklearn.cluster import KMeans\n\n# Number of clusters\nkmeans = KMeans(n_clusters=3)\n# Fitting the input data\nkmeans = kmeans.fit(X)\n# Getting the cluster labels\nlabels = kmeans.predict(X)\n# Centroid values\ncentroids = kmeans.cluster_centers_<\/code><\/pre>\n\n\n\n

Checking the clustering accuracy of our program:<\/p>\n\n\n\n

from sklearn.metrics import classification_report\n\ntarget_names = ['Iris-setosa', 'Iris-versicolor', 'Iris-virginica']\n\nprint(classification_report(irisdata['Class'],kmeans.labels_,target_names=target_names))<\/code><\/pre>\n\n\n\n

The output of classification looks like this:<\/p>\n\n\n\n

\"\"<\/a><\/figure><\/div>\n\n\n\n

You can see in the classification report that, 91% of our data was predicted accurately. That’s pretty good for an unsupervised algorithm.<\/p>\n","protected":false},"excerpt":{"rendered":"

Read my previous post to understand how K-Means algorithm works. In this post I will try to run the K-Means on Iris dataset to classify our 3 classes of flowers, Iris setosa, Iris versicolor, Iris virginica (our classess) using the flowers sepal-length, sepal-width, petal-length and petal-width (our features) Getting data: describe the data: Converting the class […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,37],"tags":[46,49],"_links":{"self":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/783"}],"collection":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/comments?post=783"}],"version-history":[{"count":2,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/783\/revisions"}],"predecessor-version":[{"id":1890,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/783\/revisions\/1890"}],"wp:attachment":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/media?parent=783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/categories?post=783"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/tags?post=783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}