{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8532741f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "For n_clusters=2, The Silhouette Coefficient is 0.6810461692117462\n",
      "For n_clusters=3, The Silhouette Coefficient is 0.5528190123564095\n",
      "For n_clusters=4, The Silhouette Coefficient is 0.49805050499728737\n",
      "For n_clusters=5, The Silhouette Coefficient is 0.4887488870931056\n",
      "For n_clusters=6, The Silhouette Coefficient is 0.36483400396700255\n",
      "For n_clusters=7, The Silhouette Coefficient is 0.3561786628965541\n",
      "For n_clusters=8, The Silhouette Coefficient is 0.3390450925992619\n",
      "For n_clusters=9, The Silhouette Coefficient is 0.3401464883891838\n",
      "For n_clusters=10, The Silhouette Coefficient is 0.3075769006794615\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import silhouette_score\n",
    "from sklearn.datasets import load_iris\n",
    "from sklearn.cluster import KMeans\n",
    "\n",
    "X = load_iris().data\n",
    "y = load_iris().target\n",
    "   \n",
    "for n_cluster in range(2, 11):\n",
    "    kmeans = KMeans(n_clusters=n_cluster).fit(X)\n",
    "    label = kmeans.labels_\n",
    "    sil_coeff = silhouette_score(X, label, metric='euclidean')\n",
    "    print(\"For n_clusters={}, The Silhouette Coefficient is {}\".format(n_cluster, sil_coeff))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "207598ad",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
