<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ankit Prasad on Medium]]></title>
        <description><![CDATA[Stories by Ankit Prasad on Medium]]></description>
        <link>https://medium.com/@ankitprasad903?source=rss-766bc8b24e13------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Z01KAYHMB_5yo_E3.jpg</url>
            <title>Stories by Ankit Prasad on Medium</title>
            <link>https://medium.com/@ankitprasad903?source=rss-766bc8b24e13------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 10:01:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ankitprasad903/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[K-Means Clustering for Beginners using Python from scratch.]]></title>
            <link>https://medium.com/code-to-express/k-means-clustering-for-beginners-using-python-from-scratch-f20e79c8ad00?source=rss-766bc8b24e13------2</link>
            <guid isPermaLink="false">https://medium.com/p/f20e79c8ad00</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[writer]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[blogger]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Ankit Prasad]]></dc:creator>
            <pubDate>Wed, 20 Mar 2019 23:37:06 GMT</pubDate>
            <atom:updated>2019-03-22T06:33:59.248Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, we will take a real-world problem and try to solve it using clustering. So let&#39;s get our hands dirty with clustering.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3_I040YCogbEE2DscLtSqw.jpeg" /></figure><p><strong>Introduction:</strong><br>Cluster analysis is a multivariate statistical technique that groups observations on the basis of features or variables they are described by.<br>Was that too boring ok let&#39;s try to understand this with an example.<br>In the example given below there two figure, one on the left side has three clusters and this is done on the basis of geographic proximity, the first cluster shows the countries in North America and the second and third cluster shows the countries in Europe and Australia respectively.<br>In the figure to the right, the clustering is done on the basis of the official language of the country.<br>In simple terms we can understand this as observations in a dataset is divided<br>into different groups and this is very useful.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eOkTjVkcLK5jWUU3k45xtA.jpeg" /><figcaption>EXAMPLE</figcaption></figure><p><strong>K-Means Algorithm:<br></strong>The algorithm is very simple given data we first initialize seeds randomly. Then we go on calculating the euclidean distance of every point with every seeds. The one with the minimum distance becomes the part of the given seed. After each and every data is covered we place the seeds into the centroid of the clusters formed. And now that centroid is the representative of that cluster.</p><p><strong>When to use Cluster Analysis?<br></strong>This is one of the decision we have to take while dealing with problems. Taking decision is not a tedious task as it solely depends upon the type of data we are using. If we are using a labeled data we can use classification technique whereas in case when the data is not labeled we can cluster the data based on certain feature and try to label it on our own.<br>So when we use cluster analysis we don’t have labels(ie..data is not labeled)<br>in the context of machine learning this is called as unsupervised learning.</p><p><strong>Final Goal:<br></strong>The goal of clustering is to maximize the similarity of observation within the cluster and maximize the dissimilarity between the clusters.<br>We will be achieving this goal very soon. Let’s dive into it.</p><p><strong>Choosing a problem<br></strong>Well to start of choosing a problem is the first and the foremost step of a data scientist. We should always choose a problem such that after solving it, the solution should benefit the end user.<br>In this article we will take an example of market segmentation. There will be certain features due to which the market is segmented. We will try to analyse the the type of customers in the market based on the features.<br>The data set consist of 30 samples and features are satisfaction and loyalty respectively.<br>You can download the data set through this link: <a href="https://drive.google.com/file/d/1nxr5XYg4JrwB_EtdWdyIwCT4gr85xfzb/view?usp=sharing">https://drive.google.com/file/d/1nxr5XYg4JrwB_EtdWdyIwCT4gr85xfzb/view?usp=sharing</a></p><p><strong>Lets Begin:<br></strong>Well I hope you have downloaded the data set from the link given above. Before beginning make sure you have jupyter notebook installed in you pc with anaconda package manager as we will be importing certain libraries.<br>To install certain package through anaconda command prompt you just have to type the following command:<br><strong>pip install</strong> <strong>the package name </strong>you want to install. If you already have the package installed you are ready to go.<br>Command to check the packages installed : <strong>conda list</strong></p><p><strong>#1. Importing relevant libraries:<br></strong>First we will import certain libraries required for performing K-means Clustering. We will also look into these packages in details.<br>1. Numpy: It is a third party package that helps us to deal with multidimensional arrays.<br>2. Pandas: Allows us to organize data in tabular form.<br>3. Matplotlib: Helps in visualizing the numpy computation.<br>4. Seaborn: This also adds to the visualization of Matplotlib<br>5. Scikit-learn: This is the most widely used machine learning library. It<br> has various functionality as in this case we are importing KMeans from it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JEsFNbbsbdpuTjRwimQF5Q.jpeg" /><figcaption>Importing Libraries</figcaption></figure><p><strong>#2. Reading the data:<br></strong>In this step we will load the data set into the variable data using pandas data frame. There are thirty observations with features satisfaction and Loyalty .The data here is in .csv format. Remember pandas helps us to organize data in tabular form.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/945/1*3mNisr5QsnKHKyAxBfy1hQ.jpeg" /><figcaption>READING DATA</figcaption></figure><p><strong>#3. Plotting the data:<br></strong>Now we will simply plot the scatter plot of the given data using. These are simple python code we will get accustomed to it once we start using it regularly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/785/1*ZkTr54_LORXu8-MWf5lhZg.jpeg" /><figcaption>PLOTTING</figcaption></figure><p><strong>#4. Clustering:<br></strong>For the first section in Selecting Feature just ignore the title for now we will see it later. We are just creating a copy of our data and storing it in variable x.<br>So now we will create a variable kmeans and by passing the argument 2 in KMeans we just said that we want to create 2 clusters. We don’t have to do anything this simple line of code will perform the kmeans algorithm and will create two clusters. It will classify our data into two clusters.<br>You can see below the number of iterations that has been perfomed, here 300.<br>This all runs behind the scenes. Python will take care of everything.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M2Vnk8HC9kVQu2_Hn15RTA.jpeg" /><figcaption>CLUSTERING</figcaption></figure><p><strong>#5. Clustering Result:<br></strong>In this step we will again create a copy of x and store it in clusters. We will create a new column called cluster_predict which will have the value as predicted by our kmeans algorithm. Things will become more clear as we move ahead.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AUSyek8VRk8HivKVPs8Sdg.jpeg" /><figcaption>CLUSTERING RESULT</figcaption></figure><p><strong>#6. Plot:<br></strong>Now we will plot the clustered data, note here we have two parameters/features here ‘Satisfaction’ and ‘Loyalty’. We can easily see the two clusters the one with all the red and the other with all the blues. But there is a problem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WpR-S7J2PU7lENfrhjBaWA.jpeg" /><figcaption>PLOTTING</figcaption></figure><p><strong>The Problem<br></strong>The biggest problem here is that Satisfaction is choosen as a feature and loyalty has been neglected. We can see in the figure that all the element to the right of 6 forms one cluster and the other on the left forms another. This is a bias result because our algorithm has discarded the Loyalty feature. It has done the clustering only on the basis of satisfaction. This does not give an appropriate result through which we can analyze things. <br>Satisfaction was choosen as the feature because it had large values. <br>So here is the problem both the data are not scaled. First we have to standardize the data, so that both the data have equal weights in our clustering. <br>We can’t neglect loyalty as it has an important role in the analyses of market segmentation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/1*hzSePKsLWdb4lblpgsqlSA.jpeg" /><figcaption>PROBLEM (FEATURE SELECTION)</figcaption></figure><p><strong>#7. Standardizing the variables:<br></strong>We will not go in depth of this as sklearn helps us to scale the data.<br>The data is scaled around zero mean. Now we can see that both the data are equally scaled and now both will have equal chance of being selected as feature.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/947/1*WiTyuXPh5SgucZh4CcTmag.jpeg" /><figcaption>STANDARDIZING</figcaption></figure><p><strong>#8. The Elbow Method:<br></strong>Have we ever wondered why we initialized kmeans with 2 clusters only.<br>Yes, we could have initialized it with any value we wanted we could have got any number of clusters. But the analyses becomes difficult when there are a large number of clusters. So how we will know the exact number of cluster to start off. Note there are no such exact number as it changes with the problem in hand.<br>Here the elbow method comes handy when we are confused as to how may clusters do we need. What elbow method does is it starts of with making one cluster to the number of clusters in our sample and with the kmeans inertia value we determine what would be the appropriate number of clusters.<br>Remember our goal- Our final goal was to minimize the within the cluster sum of square and maximize the distance between clusters. <br>With this simple line of code we get all the inertia value or the within the cluster sum of square.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p_ksb2HfqgLfUVV13rxzww.jpeg" /><figcaption>ELBOW METHOD</figcaption></figure><p><strong>#9. Visualizing the Elbow Method:<br></strong>This graph looks like elbow and we have to determine that elbow point.<br>Here the elbow point comes at around 4 and this our optimal number of clusters for the above data which we should choose.<br>If we look at the figure carefully after 4 when we go on increasing the number of cluster there is no big change in the wcss and it remains constant.<br>Hurrah..!! we have got the optimal number of clusters for our problem. <br>We will now quickly perform the kmeans clustering with the new number of clusters which is 4 and then dive into some analysis.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/919/1*j6baxGHM3R9PTllJ5S_tdw.jpeg" /><figcaption>ELBOW METHOD VISUALIZATION</figcaption></figure><p><strong>#10. Stronger Clustering:<br></strong>This is a simple code which perform clustering with 4 clusters. Here there are four clusters so our whole data is categorized into either 0,1,2 or 3.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/685/1*WXW3o3fF8FfysZkXpquEng.jpeg" /></figure><p><strong>#11. Plotting the newly cluster:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zp_yTYGJCUGsqHhsOb1OPQ.jpeg" /></figure><p><strong>#12. Analysis (The final step):<br></strong>Through the given<strong> </strong>figure following things can be interpreted:<br>1. The purple dots are the people who are less satisfied and less loyal and therefore can be termed as alienated.<br>2. The red dots are people with high loyalty and less satisfaction.<br>3. The yellow dots are the people with high loyalty and high satisfaction and they are the fans.<br>4. The sky blue dots are the people who are in the midst of things.<br>The ultimate goal of any businessman would be to have as many people up there in the fans category. We are ready with a solution and we can target the audience as per our analysis. For example, the crowd who are supporters can easily be turned into fans by fulfilling their satisfaction level.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1023/1*uH4qCk-GuI7Pz8k0KOCaTw.png" /><figcaption>ANALYSIS</figcaption></figure><p><strong>Footnotes<br></strong>I hope I was successful in writing my first blog it took me around 4 long hours. Also please forgive me if I have missed something. I am not that good in writing I am just getting used to it. Hopefully, I will be back with another one.<br>Thanks for reading..!!</p><h3>Get your own blog published on C2E Blog</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ONkkzu1AYRCweHBuAMmDig.png" /></figure><p>Do you want to write for CodeToExpress? We would love to have you as a technical writer. Send us an email with a link to your draft at <a href="/service/mailto:codetoexpress@gmail.com">codetoexpress@gmail.com</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f20e79c8ad00" width="1" height="1" alt=""><hr><p><a href="https://medium.com/code-to-express/k-means-clustering-for-beginners-using-python-from-scratch-f20e79c8ad00">K-Means Clustering for Beginners using Python from scratch.</a> was originally published in <a href="https://medium.com/code-to-express">Code To Express</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>