<jupyter_start><jupyter_text><NAME> Exploring Unsupervised Machine LearningTo predict the accurate number of clusters Importing libraries:<jupyter_code>from sklearn.cluster import KMeans
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Loading the dataset:<jupyter_code>df=pd.read_csv('Iris.csv')
df.drop(['Id'],axis=1,inplace=True)<jupyter_output><empty_output><jupyter_text>Reading the dataset:<jupyter_code>df.head() 
df.shape
df.info()
df.describe()
df.isnull().sum()
df.drop_duplicates(inplace=True)<jupyter_output><empty_output><jupyter_text>Label Encoding:<jupyter_code>from sklearn.preprocessing import LabelEncoder
le=LabelEncoder()
df['Species']=le.fit_transform(df['Species'])
df['Species'].value_counts()<jupyter_output><empty_output><jupyter_text>Comparing PetalLengthCm and PetalWidthCm:<jupyter_code>plt.scatter(df['PetalLengthCm'],df['PetalWidthCm'],c=df.Species.values)
df.corr()<jupyter_output><empty_output><jupyter_text>Visualising the data:<jupyter_code>fig=plt.figure(figsize=(15,12))
sns.heatmap(df.corr(),linewidths=1,annot=True)
sns.pairplot(df)

df=df.iloc[:,[0,1,2,3]].values<jupyter_output><empty_output><jupyter_text>Elbow method with wcss:<jupyter_code>from sklearn.cluster import KMeans
wcss = []
for i in range(1, 11):
    kmeans = KMeans(n_clusters = i, init = 'k-means++', random_state = 42)
    kmeans.fit(df)
    wcss.append(kmeans.inertia_)
    
wcss<jupyter_output><empty_output><jupyter_text>To find the Number of Clusters:<jupyter_code>plt.figure(figsize=(10,5))
sns.set(style='whitegrid')
sns.lineplot(range(1, 11), wcss,marker='o',color='red')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')
plt.show()<jupyter_output>/opt/anaconda3/lib/python3.8/site-packages/seaborn/_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  warnings.warn(<jupyter_text>K=3.  K-means<jupyter_code>kmeans = KMeans(n_clusters = 3, init = 'k-means++', random_state = 5)
y_kmeans = kmeans.fit_predict(df)
y_kmeans<jupyter_output><empty_output><jupyter_text>Visualization:<jupyter_code>fig = plt.figure(figsize=(10, 7))
plt.title('Clusters with Centroids',fontweight ='bold', fontsize=20)
plt.scatter(df[y_kmeans == 0, 2], df[y_kmeans == 0, 3], s = 100, c = 'blue', label = 'Iris-versicolour')
plt.scatter(df[y_kmeans == 1, 2], df[y_kmeans == 1, 3], s = 100, c = 'green', label = 'Iris-setosa')
plt.scatter(df[y_kmeans == 2, 2], df[y_kmeans == 2, 3],s = 100, c = 'yellow', label = 'Iris-virginica')
plt.scatter(kmeans.cluster_centers_[:, 2], kmeans.cluster_centers_[:,3], s = 300, c = 'red',marker='*', 
            label = 'Centroids')
plt.title('Iris Flower Clusters')
plt.ylabel('Petal Width in cm')
plt.xlabel('Petal Length in cm')
plt.legend()
And there you go, we see an identical graph.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15182385803483406, 'fraction_numerical': 0.02070325336838646, 'mean_word_length': 7.772334293948127, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '55805', 'license': 'MIT', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1164, 'n_words': 213, 'path': 'Unsupervised Machine Learning.ipynb', 'repo_name': 'Varadmurty-mohod/To-explore-Unsupervised-Machine-Learning', 'size': '3053'}