# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="6ct4Gt3VpXyR"
# ## Importing Libraries

# + id="kopt_WV2pU3V"
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import sklearn

# + id="TNjkXYZ3plmy"
from sklearn import datasets
from sklearn.datasets import load_iris
from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split
from sklearn.cluster import KMeans
from sklearn.mixture import GaussianMixture
from scipy.spatial import Voronoi, voronoi_plot_2d

# + [markdown] id="4uLq_RJSqpyn"
# ## Loading the dataset

# + id="A40mLvTjqoMv"
iris = load_iris()

# + colab={"base_uri": "https://localhost:8080/"} id="ZffcvFjbqwd9" outputId="5f56e946-85e3-48c2-8447-d9dd8d0fb7d9"
type(iris)

# + colab={"base_uri": "https://localhost:8080/"} id="U5aPAYX0qyH7" outputId="67f4f3b5-45c6-4923-f33d-a5b94afabfd6"
iris.data

# + colab={"base_uri": "https://localhost:8080/"} id="NFWmZ-cTq752" outputId="b0622502-050b-4b55-932f-4794079bbd28"
iris.data.shape

# + colab={"base_uri": "https://localhost:8080/"} id="LG89dkZqq9fp" outputId="dbe12f64-1661-4895-b271-fd7c9d057e66"
iris.feature_names

# + colab={"base_uri": "https://localhost:8080/"} id="FtKAgZ7-rAMW" outputId="e0f16b35-9304-45a2-b8ab-b6ed0706db39"
all_features = []
features = iris.data[:, [0,1,2,3]]
features.shape

# + colab={"base_uri": "https://localhost:8080/"} id="-tt3cz_JIXfr" outputId="270b67ec-5623-4461-f743-05f83f26326e"
for observation in features:
  all_features.append([observation[0] + observation[1] + observation[2] + observation[3]])
print(all_features)

# + colab={"base_uri": "https://localhost:8080/"} id="gbXhJI1Zrgf1" outputId="6bc55a5f-2bb8-4121-fb72-b055b52cb020"
# reshaping the target variable after assigning the target from the dataset
targets = iris.target
targets.reshape(targets.shape[0],-1)
targets.shape

# + id="caQQ7s03sczd"
# copying this raw data into a dataframe
df = pd.DataFrame(iris.data, columns = iris.feature_names)
df["species"] = iris.target

# + colab={"base_uri": "https://localhost:8080/", "height": 203} id="9ZRmpcDIEzo9" outputId="5a9cc174-ee62-4676-e9fe-0b0c7b6845e0"
df.corr()

# + colab={"base_uri": "https://localhost:8080/", "height": 203} id="kgF_XeuaEvoQ" outputId="993c2bdc-4cfa-43b8-f91b-4b7aba351f96"
df.head()

# + colab={"base_uri": "https://localhost:8080/"} id="q80fqXiYE32m" outputId="15f80b33-0f17-4f18-a0ab-278ea44303e8"
df.info

# + colab={"base_uri": "https://localhost:8080/", "height": 295} id="ibsLV4yrEyNA" outputId="ec2e62de-551d-4531-b328-db55b3cd8c27"
df.describe()

# + [markdown] id="MhHmBtD1xpAU"
# # K-Means Clustering

# + id="XILjAIbpv8bf"
X_new1 = np.asanyarray(all_features)
X_new1 = X_new1.reshape(-1,1)

# + colab={"base_uri": "https://localhost:8080/"} id="ppqr2RdvKcSp" outputId="4526d042-56cb-4fd5-95db-2f698e849200"
X_new1.shape

# + colab={"base_uri": "https://localhost:8080/"} id="TUexC00vKhBI" outputId="2c2c4fc6-ce9c-446b-a09a-ff9ed3cfc065"
y = iris.target
y.shape

# + id="Pu7pP7eGMLN0"
X_new2 = df.iloc[:, 0:5]
Y_new2 = df["species"]

# + colab={"base_uri": "https://localhost:8080/"} id="wraMV0hGMlgI" outputId="faaa365f-e9e6-4af7-eb9f-4d84cb3988fe"
X2_train, X2_test, y_train, y_test = train_test_split(X_new2, Y_new2, test_size=0.4, random_state=0)
print(" X2_train",X2_train)
print("X2_test",X2_test)
print("y_train",y_train)
print("y_test",y_test)

# + colab={"base_uri": "https://localhost:8080/"} id="o-RuPDBwMtgc" outputId="ca7f948b-7053-4a87-beff-4ce3d16c7502"
kmeans = KMeans(n_clusters=3, max_iter =1000)
kmeans.fit(iris.data)

# + colab={"base_uri": "https://localhost:8080/"} id="G834VoCrNNNW" outputId="1766e95f-47e6-43d3-8d4f-508b3351c3d9"
kmeans.cluster_centers_

# + colab={"base_uri": "https://localhost:8080/", "height": 203} id="siJD9daMNdQv" outputId="8ba4805c-f2ea-4559-d927-b024b1f3d39d"
df[' K Mean predicted label'] = kmeans.labels_
df.head()

# + [markdown] id="mFsdUp1PPJ6q"
# # Plotting Blobs using the Iris dataset

# + colab={"base_uri": "https://localhost:8080/", "height": 282} id="sZGMj3xtOTG7" outputId="ace49fb7-0fc4-4c5b-fb38-e47e528f1c8e"
X_new1, y_true = make_blobs(n_samples=300, centers=3,
                       cluster_std=0.60, random_state=0)
plt.scatter(X_new1[:, 0], X_new1[:, 1], s=50)

# + colab={"base_uri": "https://localhost:8080/"} id="5U6QI1ydS6k3" outputId="2c0011f3-42fd-4f0a-8f79-6f66ca105a18"
kmeans.inertia_

# + id="tbZ52UjIPYTv"
km8 = KMeans(n_clusters=8, max_iter =1000)

# + colab={"base_uri": "https://localhost:8080/"} id="hlCqiin3PuiW" outputId="532cb5ec-c526-420d-b9c1-b3ae27af07e6"
km8.fit(iris.data)

# + colab={"base_uri": "https://localhost:8080/"} id="Vz2aNCX3PwKh" outputId="92bef80c-3cbf-490d-b51e-9c9cf1a690ac"
km8.cluster_centers_

# + colab={"base_uri": "https://localhost:8080/", "height": 203} id="9p9hyBR0PyNC" outputId="b215003a-b44a-48b8-af7e-c07d24a139f6"
df[' K Mean predicted label'] = km8.labels_
df.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 265} id="SR48qt0lP3nU" outputId="89818134-01f4-4cdf-bbd7-fabea05b289a"
X_new8, y_true = make_blobs(n_samples=300, centers=8,
                       cluster_std=0.60, random_state=0)
plt.scatter(X_new8[:, 0], X_new8[:, 1], s=50);

# + colab={"base_uri": "https://localhost:8080/"} id="Z9pNdW2XTGMV" outputId="d76f3b01-fed8-4088-e0f7-1bfb6988c00d"
km8.inertia_

# + id="IGhSmJ_2QBRw"
x = df.iloc[:, [0, 1, 2, 3]].values
wcss = [] #within cluster sum of squares

for i in range(1, 11):
    kmeans = KMeans(n_clusters = i, init = 'k-means++', max_iter = 300, n_init = 10, random_state = 0)
    kmeans.fit(x)
    wcss.append(kmeans.inertia_)

kmeans = KMeans(n_clusters = 3, init = 'k-means++', max_iter = 300, n_init = 10, random_state = 0)
y_kmeans = kmeans.fit_predict(x)

# + colab={"base_uri": "https://localhost:8080/", "height": 285} id="oJaJwrwJQKt2" outputId="28895861-692b-4270-970c-f172b4246d60"
#Visualising all the clusters
plt.scatter(x[y_kmeans == 0, 0], x[y_kmeans == 0, 1], s = 100, c = 'skyblue', label = 'Iris-setosa')
plt.scatter(x[y_kmeans == 1, 0], x[y_kmeans == 1, 1], s = 100, c = 'yellow', label = 'Iris-versicolour')
plt.scatter(x[y_kmeans == 2, 0], x[y_kmeans == 2, 1], s = 100, c = 'lime', label = 'Iris-virginica')

# Plotting the cluster centers
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:,1], s = 100, c = 'black', label = 'Centroids')
plt.legend()

# + colab={"base_uri": "https://localhost:8080/"} id="JvJSpMEYQNyk" outputId="4455434f-d5ca-4630-ad3f-e345c73fabc5"
kmeans2 = KMeans(n_clusters = 8, init = 'k-means++', max_iter = 300, n_init = 10, random_state = 0)
y_kmeans2 = kmeans2.fit_predict(x)
kmeans2.cluster_centers_

# + colab={"base_uri": "https://localhost:8080/", "height": 203} id="sgQh0SPQRhso" outputId="a34e70d3-0d0d-43e2-a3c1-95e7242b869c"
df[' K Mean predicted label'] = kmeans2.labels_
df.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 295} id="dPzFnk91Rmy6" outputId="3a6bb522-88ae-4719-c117-b98dbffff277"
wcss = []

for i in range(1, 11):
    kmeans = KMeans(n_clusters = i, init = 'k-means++', max_iter = 300, n_init = 10, random_state = 0)
    kmeans.fit(iris.data)
    wcss.append(kmeans.inertia_)
    
# Plotting the results on a line graph which could help in visualizing and figuring out the elbow
plt.plot(range(1, 11), wcss)
plt.title('The elbow method')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS') 
plt.show()

# + [markdown] id="O-iCkD6XSAWb"
# The elbow-k value is 3

# + [markdown] id="enRErNx9TQM5"
# # Gaussian Mixture Model Classifier

# + colab={"base_uri": "https://localhost:8080/"} id="RYyM3devR-G5" outputId="62288f78-ae56-463f-a30b-f9dd17676901"
num_clusters = 4;
gmm = GaussianMixture(n_components = num_clusters)
gmm.fit(x)

# + colab={"base_uri": "https://localhost:8080/"} id="HoMAbQWoTn9D" outputId="8099dad3-918e-4fbf-8105-72c5d49b6cf4"
y_cluster_gmm = gmm.predict(x)
y_cluster_gmm

# + id="q3fgeaJsUFZN"
wh1 = df[['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)']]

# + colab={"base_uri": "https://localhost:8080/", "height": 364} id="Lo2OB02EUPtK" outputId="b5545614-9923-4196-8766-ffc3b63b654f"
# To calculate the correlation among the above variables
corrr = wh1.corr()
sns.heatmap(corr, square = True)


# + id="OPgPRGNSUXuv"
def doGMM(x, nclust=2):
    model = GaussianMixture(n_components=nclust,init_params='kmeans')
    model.fit(x)
    cluster_labels3 = model.predict(x)
    return (cluster_labels3)

cluster_labels3 = doGMM(wh1,2)
gmm = pd.DataFrame(cluster_labels3)

# + [markdown] id="djKcroQSWKFO"
# ## Voronoi plots

# + id="LOCeOtfDinCU"
km8 = KMeans(n_clusters=8, max_iter =1000)

# + colab={"base_uri": "https://localhost:8080/"} id="iUfJGmK7im-v" outputId="bb0e7ce8-1c8e-4032-8d25-143a29a5ad7b"
km8.fit(X_vor)

# + id="8V_ji_SHim8O"
centers2= km8.cluster_centers_

# + colab={"base_uri": "https://localhost:8080/", "height": 282} id="pdTEUXQ-im5w" outputId="32ac9f45-239f-4c62-fd4f-9f86b3c11112"
vor = Voronoi(centers2) 
voronoi_plot_2d(vor) 
plt.scatter(X_vor[:, 0], X_vor[:, 1], c=y_vor, s=5, cmap='summer')

# + colab={"base_uri": "https://localhost:8080/", "height": 265} id="pHgvRZsIim3H" outputId="70242bbe-b786-4057-f788-58b98efc3715"
X_vor, y_vor = make_blobs(n_samples=2000, centers= np.array(
    [[ 0.2,  2.3],
     [-1.5 ,  2.3],
     [-2.8,  1.8],
     [-2.8,  2.8],
     [-2.8,  1.3]]), cluster_std= np.array([0.4, 0.3, 0.1, 0.1, 0.1]), random_state=0)
plt.scatter(X_vor[:, 0], X_vor[:, 1], s=50);

# + id="aVQM-qdLim0Z"
km8 = KMeans(n_clusters=8, max_iter =1000)

# + colab={"base_uri": "https://localhost:8080/"} id="RoE7SZjtimxP" outputId="e018f29c-8ff1-420f-c94e-5d08a669133d"
km8.fit(X_vor)

# + id="j_FVdYd7imtw"
centers2= km8.cluster_centers_

# + colab={"base_uri": "https://localhost:8080/", "height": 282} id="jiDxfRgqimmk" outputId="dce46c0b-0402-4169-cd69-c3282ac2fad0"
vor = Voronoi(centers2) 
voronoi_plot_2d(vor) 
plt.scatter(X_vor[:, 0], X_vor[:, 1], c=y_vor, s=5, cmap='summer')

# + [markdown] id="z-40h4TVonT0"
# Reporting the value of np.sum(y_pred==y) / len(y_pred)

# + colab={"base_uri": "https://localhost:8080/"} id="HeesxfhjkuLl" outputId="7141ff83-b412-48ce-da1c-80d24cf9a7d9"
df_data = pd.DataFrame(data=iris.data, columns=["sepal_length", "sepal_width", "petal_length", "petal_width"])
df_target = pd.DataFrame(data=iris.target, columns=["class"])
df_target = np.array(df_target).reshape(150,)
np.sum(y_cluster_gmm==df_target) / len(y_cluster_gmm)

# + [markdown] id="GpwPymlhpd53"
# Predicting labels for X_new

# + colab={"base_uri": "https://localhost:8080/"} id="Glf6iiK8pmQz" outputId="574c03cd-b56e-424a-e114-4935470d0486"
X_new = np.array([[0, 2], [3, 2], [-3, 3], [-3, 2.5]])
X_new

# + colab={"base_uri": "https://localhost:8080/"} id="yaM9r4T2r62m" outputId="288e8d7d-c0ee-4b50-a999-f312988ee185"
km_new = KMeans(n_clusters=3,random_state=0)
km_new.fit(X2_train)

print(y_new_result)

# + id="ciboWWKbwfXg"
y_new_result = km_new.fit_predict(X_new)

# + colab={"base_uri": "https://localhost:8080/"} id="r9XYlC53xLjJ" outputId="bcb2030e-75da-48d1-a2d0-18f0e173a61d"
y_new_result

# + [markdown] id="xYHqCcL_x0lI"
# Inertia comparision for k = 3 and k = 8

# + colab={"base_uri": "https://localhost:8080/"} id="YzTLv0L2xNi6" outputId="51d29ab0-9f78-42f2-debb-03130dbaecf0"
print("For k = 3: ", kmeans.inertia_)
print("For k = 8: ", km8.inertia_)

# + [markdown] id="sF-qukvLyZBj"
# Since the inertia for k = 3 has a lower value, k = 3 seems to be a better model, and also since it has lesser number of clusters

# + colab={"base_uri": "https://localhost:8080/"} id="trpEzkqSySMA" outputId="44ac2942-dae3-42e6-f544-97e2cef9c553"
from scipy import stats
from scipy.stats import mstats
stats.mode(y_cluster_gmm)
mstats.mode(y_cluster_gmm)

# + id="UFeG2tXfzYTg"


StarcoderdataJupyterScriptsDedupFiltered