<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
from sklearn.cluster import KMeans
import numpy as nd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
url = "https://raw.githubusercontent.com/XaviPat01/ML_TechAnalogy_Workshop/main/Minor%20Project%203/income.csv"

data = pd.read_csv(url)
data.head()
data.rename(columns={'Income($)':'income','Age':'age'}, inplace = True)

plt.scatter(data.age,data.income,marker=',')
plt.xlabel('Age')
plt.ylabel('Income in $')
plt.show()<jupyter_output><empty_output><jupyter_text>We can clearly see three clusters should be performed. We'll use the elbow plot method later to confirm our inference.<jupyter_code>data.describe()<jupyter_output><empty_output><jupyter_text>As we can clearly see, their is a considerable difference in the range of age(of the order of 10) v/s range of income (of the order of 10^5).This suggests that we should use MinMaxScaler to normalize them.<jupyter_code>scaler = MinMaxScaler()

scaler.fit(data[['income']])
data.income = scaler.transform(data[['income']])

scaler.fit(data[['age']])
data.age = scaler.transform(data[['age']])
data.head()
plt.scatter(data.age,data.income,marker=',')
plt.xlabel("Age")
plt.ylabel("Income in $")
plt.show()
inertias=[]
range_k = range(1,15)
for k in range_k:
  kmeans = KMeans(n_clusters=k)
  kmeans.fit(data[['age','income']])
  inertias.append(kmeans.inertia_)
plt.plot(range_k,inertias)
plt.xticks(range_k)
plt.show()<jupyter_output><empty_output><jupyter_text>Hence, our inference from the scatter plot was correct. We'll take number of clusters = 3.<jupyter_code>kmeans = KMeans(n_clusters=3)
pred_Y = kmeans.fit_predict(data[['age','income']])
data['clusternum']=pred_Y
data.head()
centroid = kmeans.cluster_centers_

data_1 = data[data.clusternum==0]
data_2 = data[data.clusternum==1]
data_3 = data[data.clusternum==2]
plt.scatter(data_1.age,data_1.income, marker=',',color='yellow')
plt.scatter(data_2.age,data_2.income,marker=',',color='blue')
plt.scatter(data_3.age,data_3.income,marker=',',color='red')

plt.scatter(centroid[:,0],centroid[:,1],color='green',label='Centroid')

plt.xlabel("Age")
plt.ylabel("Income in $")
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16248880931065354, 'fraction_numerical': 0.01387645478961504, 'mean_word_length': 7.869047619047619, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '541454', 'license': 'MIT', 'n_tokens_mistral': 900, 'n_tokens_neox': 840, 'n_words': 152, 'path': 'Minor Project 3/K_Means_Clustering.ipynb', 'repo_name': 'XaviPat01/ML-Models', 'size': '2234'}