<jupyter_start><jupyter_text>ClusteringClustering is an unsupervised learning technique useful to find similarity amongst data.<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Load and plot data<jupyter_code>df = pd.read_csv('../data/iris.csv')
df.head()
import seaborn as sns
sns.pairplot(df, hue='species');<jupyter_output><empty_output><jupyter_text>Encode labels<jupyter_code>df.species.unique()
df['target_n'] = df['species'].map({'setosa':0, 'versicolor':1, 'virginica':2})<jupyter_output><empty_output><jupyter_text>Kmeans clustering<jupyter_code>from sklearn.cluster import KMeans

X = df[['sepal_length','sepal_width','petal_length','petal_width']]
km = KMeans(3)
km.fit(X)
centers = km.cluster_centers_
centers
plt.figure(figsize = (14,6))
plt.subplot(121)
plt.scatter(df.sepal_length, df.petal_length, c=df['target_n'])
plt.xlabel('Sepal Length (cm)')
plt.ylabel('Petal length (cm)')
plt.title('True Labels')

plt.subplot(122)
plt.scatter(df.sepal_length, df.petal_length, c=km.labels_)
plt.scatter(centers[:,0], centers[:,2], marker='o', c='r', s=100)
plt.xlabel('Sepal Length (cm)')
plt.ylabel('Petal length (cm)')
plt.title('K-Means Clusters');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1625194401244168, 'fraction_numerical': 0.013996889580093312, 'mean_word_length': 9.214285714285714, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '370905', 'license': 'MIT', 'n_tokens_mistral': 531, 'n_tokens_neox': 501, 'n_words': 84, 'path': 'day_1/Lab_07_ML Scikit Learn Clustering.ipynb', 'repo_name': 'sekharhanumanthu/deeplearning', 'size': '1286'}