<jupyter_start><jupyter_text><NAME> The sparks Foundation Task 2 Prediction using Unsupervised ML predict the optimum number of clusters and represent it visually.<jupyter_code>### we will use elbow method for finding optimum number of clusters 
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Getting familiar with data<jupyter_code>df=pd.read_csv('Ta`sk2.csv')
df=df.drop(columns='Id')
df
df.info()
df.describe()
sns.pairplot(df, hue='Species')<jupyter_output><empty_output><jupyter_text>Nomralizing<jupyter_code>from sklearn.preprocessing import MinMaxScaler
mms=MinMaxScaler()
df_trans=mms.fit_transform(df.iloc[:,:4])
df_trans=pd.DataFrame(df_trans,columns=['SepalLengthCm','SepalWidthCm','PetalLengthCm','PetalWidthCm'])
df_trans
df_convr=pd.merge(df_trans,df['Species'],how='left',left_index=True,right_index=True)
df_convr
sns.pairplot(df_convr,hue='Species')<jupyter_output><empty_output><jupyter_text>Elbow method Implementation<jupyter_code>from sklearn.cluster import KMeans
Sum_of_squared_distances=[]
K=range(1,15)
for k in K:
    km=KMeans(n_clusters=k)
    km=km.fit(df_trans)
    Sum_of_squared_distances.append(km.inertia_)
Sum_of_squared_distances<jupyter_output><empty_output><jupyter_text>Graphical Representation<jupyter_code>plt.plot(K,Sum_of_squared_distances,'bx-')
plt.xlabel('k')
plt.ylabel('Sum_of_squared_distances')
plt.title('Elbow Method for Optimal k')
###the optimum is 3 because elbow is at 3

###showing 3 clusters in a 2d feature graph and we w
##will be taking PetalLengthCm	PetalWidthCm
##because through pairplot we can see they are fit to distinguish<jupyter_output><empty_output><jupyter_text>Confirming elbow method result with graphical representation<jupyter_code>km=KMeans(n_clusters=3)
km.fit(df_convr.iloc[:,2:4])
km.cluster_centers_
plt.figure(figsize=(10,6))
sns.scatterplot(x=df_convr.iloc[:,2], y=df_convr.iloc[:,3],
              hue=df_convr["Species"])
plt.scatter(0.07864407, 0.06,s=200,c='r')
plt.scatter(0.55867014, 0.51041667,s=200,c='b')
plt.scatter(0.7740113 , 0.81510417,s=200,c='black')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.127365020766036, 'fraction_numerical': 0.03414859252422704, 'mean_word_length': 9.373205741626794, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '326052', 'license': 'Apache-2.0', 'n_tokens_mistral': 900, 'n_tokens_neox': 833, 'n_words': 141, 'path': 'GRIPTASK2.ipynb', 'repo_name': 'harshit-wq/The-Sparks-Foundation', 'size': '2174'}