<jupyter_start><jupyter_text>Clustering Watch the video below:<jupyter_code>%%HTML
<iframe width="560" height="315" src="https://www.youtube.com/embed/4b5d3muPQmA" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe><jupyter_output><empty_output><jupyter_text>Now, let's try it on scatter plot!   Let's create two lists(Age and Income) and generate values as follow:<jupyter_code>import numpy as np
from sklearn.cluster import KMeans
from sklearn.preprocessing import scale
from matplotlib import pyplot as plt

age = np.random.randint(20,80,80)
income = np.random.randint(20000,100000,80)<jupyter_output><empty_output><jupyter_text>Plotting the data with matplotlib package<jupyter_code>plt.scatter(age,income)
plt.xlabel("Age")
plt.ylabel("Income")
plt.show()<jupyter_output><empty_output><jupyter_text>First line of code: "numpy.c_" converts lists(Age&Income) into one arraySecond line of code: If you do not specify number of clusters while creating KMeans, it will go with default(Clusters=8)**Fit** is a function of KMeans package which computes all CLUSTERS in the dataScale means standardization of the data. Sometimes we need to standartize datasetIf you feel confused about scaling, go to the [following link](https://medium.com/greyatom/why-how-and-when-to-scale-your-features-4b30ab09db5e)<jupyter_code>data=np.c_[age,income]
model=KMeans(n_clusters=5).fit(scale(data))
print(model.labels_)<jupyter_output>C:\Users\User\Anaconda3\lib\site-packages\sklearn\utils\validation.py:595: DataConversionWarning: Data with input dtype int32 was converted to float64 by the scale function.
  warnings.warn(msg, DataConversionWarning)<jupyter_text>Let's plot the scatters again according to the clusters they belong to.<jupyter_code>plt.scatter(data[:,0],data[:,1],c=model.labels_)
plt.show()<jupyter_output><empty_output><jupyter_text>Following are the coordinates of the centroids<jupyter_code>centroid=model.cluster_centers_
centroid<jupyter_output><empty_output><jupyter_text>Following is a function returns inverse of SCALING<jupyter_code>def invf(u,v):
    import numpy as np
    return np.c_[[i*np.std(age)+np.mean(age) for i in u],[i*np.std(income)+np.mean(income) for i in v]]
ppp=invf(centroid[:,0].tolist(),centroid[:,1].tolist())
print(ppp)
plt.scatter(data[:,0],data[:,1],c=model.labels_)
plt.scatter(ppp[:,0],ppp[:,1],marker='+',s=50)
plt.show()<jupyter_output><empty_output><jupyter_text>You might feel confused about couple of things upto now:(Possible questions you might be thinking) 1)How many Centroids  to use?  Answer: Elbow method 2)Where(coordinates) to place those Centroids in the scatter plot? Answer: Kmeans++ algorithm (Python will take care of this) However, if you are intersted to know about the Kmeans++ algorithm, follow the [link](http://www.real-statistics.com/multivariate-statistics/cluster-analysis/initializing-clusters-k-means/) There will be one more example to explain Elbow method<jupyter_code>#Let's import the necessary libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#Importing the dataset with pandas
dataset = pd.read_csv("Mall_Customers.csv") #There will be a csv file provided with this ipython file
dataset.head() #print the head to get insight about the data<jupyter_output><empty_output><jupyter_text>Analysing Data:It is a data of custormers in a mall. And we would like to categorize customers for marketing team. Spending score is calculated depending on how much money certain customer has spent in the past and its measure scale is 1 to 100 where 1 - (buys products very rarely) and 100 - (buys products very frequently).However, we are going to need to use only "Annual Income" and "Spending Score"<jupyter_code>X = dataset.iloc[:, [3,4]].values #Counting starts from 0 so Annual Income is on 3rd column and Spending Score is on 4th.
X #now X contains only Annual Income and Spending Score of the Customers<jupyter_output><empty_output><jupyter_text>Plotting the data<jupyter_code>plt.scatter(X[:, 0], X[:, 1])
plt.xlabel("Income")
plt.ylabel("Spending Score(1-100)")
plt.show()<jupyter_output><empty_output><jupyter_text>Now, let's use **Elbow method** to compute required number of Clusters<jupyter_code>#First, import KMeans from sklearn library
from sklearn.cluster import KMeans<jupyter_output><empty_output><jupyter_text>Elbow method computes WCSS(Within-Cluster-Sum-of-Squares)Basically, WCSS is sum of distances of points from CLUSTER CENTROID[Elbow method explanation](http://www.nbertagnolli.com/jekyll/update/2015/12/10/Elbow.html)<jupyter_code>#Let's create a list called "wcss" which will contain sum of distances from centroid(can be 1 upto 10)
wcss = [] #
for i in range(1, 11): #Let's loop through the cases when we can have upto 10 centroids
    kmeans = KMeans(n_clusters=i, init='k-means++')
    #n_cluster-number of cluster(or centroid). init defines which algorith to use to place the centroid(s)
    kmeans.fit(X) #Fitting the data 
    wcss.append(kmeans.inertia_) #Inertia function computes sum of distances from the centroid(s)
wcss<jupyter_output><empty_output><jupyter_text>Let's plot the Elbow method to determine how many centroids are optimal to use<jupyter_code>fig, ax1 = plt.subplots()   
ax1.plot(range(1, 11), wcss)
plt.title("The Elbow method")
plt.xlabel("Number of Clusters")
plt.ylabel("WCSS")<jupyter_output><empty_output><jupyter_text>As we can see, 5 centroids are what we need Now, let's apply k-means with 5 centroids to our dataset<jupyter_code>#Applying k-means to the dataset
kmeans = KMeans(n_clusters=5, init='k-means++')
y_kmeans = kmeans.fit_predict(X)
X[y_kmeans==0,0]<jupyter_output><empty_output><jupyter_text>Now, let's plot what we have done so far<jupyter_code>#Visualizing the clusters
fig, ax2 = plt.subplots()
ax2.scatter(X[y_kmeans==0,0], X[y_kmeans==0,1], s=100, c='r', label='Cluster1')
ax2.scatter(X[y_kmeans==1,0], X[y_kmeans==1,1], s=100, c='b', label='Cluster2')
ax2.scatter(X[y_kmeans==2,0], X[y_kmeans==2,1], s=100, c='g', label='Cluster3')
ax2.scatter(X[y_kmeans==3,0], X[y_kmeans==3,1], s=100, c='cyan', label='Cluster4')
ax2.scatter(X[y_kmeans==4,0], X[y_kmeans==4,1], s=100, c='magenta', label='Cluster5')
ax2.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=300, c="y", label='Centroids')
plt.title("Cluster of clients")
plt.xlabel("Annual Income(k$)")
plt.ylabel("Spending Score(1-100)")
ax2.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Now let's judge the data1) people with low income and they do not spend much money in the mall either, therefore let's categorize them under "Sensible"2) people with average income and they spend average amount of money, therefore they fall under category called "Standard"3) people with high income, but they do not spend much unfortunately. Therefore let's label them as "Careful"4) people with high income and high expenses are our TARGET. So, let's label them as "Target"5) people with low income and high expenses fall under "Careless".<jupyter_code>#Let's also change the marker type of Centroids
#Cluster number might change when you apply k-means each time
fig, ax2 = plt.subplots()
ax2.scatter(X[y_kmeans==0,0], X[y_kmeans==0,1], s=100, c='r', label='Standard')
ax2.scatter(X[y_kmeans==1,0], X[y_kmeans==1,1], s=100, c='b', label='Careful')
ax2.scatter(X[y_kmeans==2,0], X[y_kmeans==2,1], s=100, c='g', label='Sensible')
ax2.scatter(X[y_kmeans==3,0], X[y_kmeans==3,1], s=100, c='cyan', label='Target')
ax2.scatter(X[y_kmeans==4,0], X[y_kmeans==4,1], s=100, c='magenta', label='Careless')
ax2.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=300, c="y", marker='*', label='Centroids')
plt.title("Cluster of clients")
plt.xlabel("Annual Income(k$)")
plt.ylabel("Spending Score(1-100)")
ax2.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12872542802790107, 'fraction_numerical': 0.027393785668991758, 'mean_word_length': 7.096509240246407, 'pattern_counts': {'":': 0, '<': 66, '<?xml version=': 0, '>': 66, 'https://': 2, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '366195', 'license': 'MIT', 'n_tokens_mistral': 2941, 'n_tokens_neox': 2738, 'n_words': 762, 'path': 'Introduction_to_K-means(Clustering)/Clustering.ipynb', 'repo_name': 'rasulrukhilloev/ML_tutorials', 'size': '7885'}