<jupyter_start><jupyter_text>Hmm, so first thing import libraries<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sns
from sklearn.cluster import KMeans
from sklearn.preprocessing import MinMaxScaler,RobustScaler,StandardScaler
from sklearn import datasets
import pandas as pd
np.random.seed(5) # Set random seed to 5 for reproduceability
iris = datasets.load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df.info()
df.describe()
sns.displot(df['sepal length (cm)'],kde=True)
sns.displot(df['sepal width (cm)'],kde=True)
sns.displot(df['petal width (cm)'],kde=True)
sns.displot(df['petal length (cm)'],kde=True)
y=iris.target
X=iris.data<jupyter_output><empty_output><jupyter_text>With MinMaxScaling<jupyter_code>sc=MinMaxScaler()
X_scaled=sc.fit_transform(X)
km=KMeans(init='k-means++',n_clusters=4)
y_pred=km.fit_predict(X_scaled)
 fig = plt.figure(figsize=(5, 5))
ax = Axes3D(fig, rect=[0, 0, 0.95, 1], elev=48, azim=134)
ax.scatter(X_scaled[:, 3], X_scaled[:, 0], X_scaled[:, 2], c=y_pred, edgecolor="k")
ax.scatter(km.cluster_centers_[:,0],km.cluster_centers_[:,1],km.cluster_centers_[:,2],km.cluster_centers_[:,3],c='red',marker='*')
ax.w_xaxis.set_ticklabels([])
ax.w_yaxis.set_ticklabels([])
ax.w_zaxis.set_ticklabels([])
ax.set_xlabel("Petal width")
ax.set_ylabel("Sepal length")
ax.set_zlabel("Petal length")
ax.dist = 12<jupyter_output>/usr/local/lib/python3.7/dist-packages/mpl_toolkits/mplot3d/art3d.py:794: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  if zdir == 'x':
/usr/local/lib/python3.7/dist-packages/mpl_toolkits/mplot3d/art3d.py:796: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  elif zdir == 'y':<jupyter_text>Without Scaling<jupyter_code>km=KMeans(init='k-means++',n_clusters=4)
y_pred=km.fit_predict(X)
fig = plt.figure(figsize=(5, 5))
ax = Axes3D(fig, rect=[0, 0, 0.95, 1], elev=48, azim=134)
ax.scatter(X[:, 3], X[:, 0], X[:, 2], c=y_pred, edgecolor="k")
ax.scatter(km.cluster_centers_[:,0],km.cluster_centers_[:,1],km.cluster_centers_[:,2],km.cluster_centers_[:,3],c='red',marker='*')
ax.w_xaxis.set_ticklabels([])
ax.w_yaxis.set_ticklabels([])
ax.w_zaxis.set_ticklabels([])
ax.set_xlabel("Petal width")
ax.set_ylabel("Sepal length")
ax.set_zlabel("Petal length")
ax.dist = 12<jupyter_output>/usr/local/lib/python3.7/dist-packages/mpl_toolkits/mplot3d/art3d.py:794: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  if zdir == 'x':
/usr/local/lib/python3.7/dist-packages/mpl_toolkits/mplot3d/art3d.py:796: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  elif zdir == 'y':<jupyter_text>It is clear we need scale the data as is evident from the features of petal width and petal lengyth distribution plots! as well<jupyter_code>sse=[]
for k in range(1,10):
  km=KMeans(n_clusters=k)
  km.fit(X_scaled)
  sse.append(km.inertia_)

plt.xlabel("K")
plt.ylabel("Sum of Squared Error")
plt.plot(range(1,10),sse)<jupyter_output><empty_output><jupyter_text>Using n_clusters=3<jupyter_code>km=KMeans(init='k-means++',n_clusters=3,verbose=1,algorithm='full') # Full uses expectation maximization
y_pred=km.fit_predict(X_scaled)
km.n_clusters
km.n_iter_
km.cluster_centers_
km.inertia_
 fig = plt.figure(figsize=(5, 5))
ax = Axes3D(fig, rect=[0, 0, 0.95, 1], elev=48, azim=134)
ax.scatter(X_scaled[:, 3], X_scaled[:, 0], X_scaled[:, 2], c=y_pred, edgecolor="k")
ax.scatter(km.cluster_centers_[:,0],km.cluster_centers_[:,1],km.cluster_centers_[:,2],km.cluster_centers_[:,3],c='red',marker='x')
ax.w_xaxis.set_ticklabels([])
ax.w_yaxis.set_ticklabels([])
ax.w_zaxis.set_ticklabels([])
ax.set_xlabel("Petal width")
ax.set_ylabel("Sepal length")
ax.set_zlabel("Petal length")
ax.dist = 12<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1599901088031652, 'fraction_numerical': 0.027695351137487636, 'mean_word_length': 7.949115044247788, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '386454', 'license': 'MIT', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1625, 'n_words': 285, 'path': 'KMeans_Iris.ipynb', 'repo_name': 'Agrover112/CC1702-ML-Lab', 'size': '4044'}