Files
cas-pml/ML/aufgaben/a2/kmeans_iris.ipynb
T

2722 lines
136 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from sklearn import datasets\n",
"iris = datasets.load_iris()\n",
"\n",
"from sklearn.cluster import KMeans\n",
"from sklearn import metrics"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
" 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n",
" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2\n",
" 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\n",
" 2 2]\n"
]
}
],
"source": [
"print(iris.target)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>#sk-container-id-1 {\n",
" /* Definition of color scheme common for light and dark mode */\n",
" --sklearn-color-text: #000;\n",
" --sklearn-color-text-muted: #666;\n",
" --sklearn-color-line: gray;\n",
" /* Definition of color scheme for unfitted estimators */\n",
" --sklearn-color-unfitted-level-0: #fff5e6;\n",
" --sklearn-color-unfitted-level-1: #f6e4d2;\n",
" --sklearn-color-unfitted-level-2: #ffe0b3;\n",
" --sklearn-color-unfitted-level-3: chocolate;\n",
" /* Definition of color scheme for fitted estimators */\n",
" --sklearn-color-fitted-level-0: #f0f8ff;\n",
" --sklearn-color-fitted-level-1: #d4ebff;\n",
" --sklearn-color-fitted-level-2: #b3dbfd;\n",
" --sklearn-color-fitted-level-3: cornflowerblue;\n",
"}\n",
"\n",
"#sk-container-id-1.light {\n",
" /* Specific color for light theme */\n",
" --sklearn-color-text-on-default-background: black;\n",
" --sklearn-color-background: white;\n",
" --sklearn-color-border-box: black;\n",
" --sklearn-color-icon: #696969;\n",
"}\n",
"\n",
"#sk-container-id-1.dark {\n",
" --sklearn-color-text-on-default-background: white;\n",
" --sklearn-color-background: #111;\n",
" --sklearn-color-border-box: white;\n",
" --sklearn-color-icon: #878787;\n",
"}\n",
"\n",
"#sk-container-id-1 {\n",
" color: var(--sklearn-color-text);\n",
"}\n",
"\n",
"#sk-container-id-1 pre {\n",
" padding: 0;\n",
"}\n",
"\n",
"#sk-container-id-1 input.sk-hidden--visually {\n",
" border: 0;\n",
" clip: rect(1px 1px 1px 1px);\n",
" clip: rect(1px, 1px, 1px, 1px);\n",
" height: 1px;\n",
" margin: -1px;\n",
" overflow: hidden;\n",
" padding: 0;\n",
" position: absolute;\n",
" width: 1px;\n",
"}\n",
"\n",
"#sk-container-id-1 div.sk-dashed-wrapped {\n",
" border: 1px dashed var(--sklearn-color-line);\n",
" margin: 0 0.4em 0.5em 0.4em;\n",
" box-sizing: border-box;\n",
" padding-bottom: 0.4em;\n",
" background-color: var(--sklearn-color-background);\n",
"}\n",
"\n",
"#sk-container-id-1 div.sk-container {\n",
" /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n",
" but bootstrap.min.css set `[hidden] { display: none !important; }`\n",
" so we also need the `!important` here to be able to override the\n",
" default hidden behavior on the sphinx rendered scikit-learn.org.\n",
" See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n",
" display: inline-block !important;\n",
" position: relative;\n",
"}\n",
"\n",
"#sk-container-id-1 div.sk-text-repr-fallback {\n",
" display: none;\n",
"}\n",
"\n",
"div.sk-parallel-item,\n",
"div.sk-serial,\n",
"div.sk-item {\n",
" /* draw centered vertical line to link estimators */\n",
" background-image: linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));\n",
" background-size: 2px 100%;\n",
" background-repeat: no-repeat;\n",
" background-position: center center;\n",
"}\n",
"\n",
"/* Parallel-specific style estimator block */\n",
"\n",
"#sk-container-id-1 div.sk-parallel-item::after {\n",
" content: \"\";\n",
" width: 100%;\n",
" border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n",
" flex-grow: 1;\n",
"}\n",
"\n",
"#sk-container-id-1 div.sk-parallel {\n",
" display: flex;\n",
" align-items: stretch;\n",
" justify-content: center;\n",
" background-color: var(--sklearn-color-background);\n",
" position: relative;\n",
"}\n",
"\n",
"#sk-container-id-1 div.sk-parallel-item {\n",
" display: flex;\n",
" flex-direction: column;\n",
"}\n",
"\n",
"#sk-container-id-1 div.sk-parallel-item:first-child::after {\n",
" align-self: flex-end;\n",
" width: 50%;\n",
"}\n",
"\n",
"#sk-container-id-1 div.sk-parallel-item:last-child::after {\n",
" align-self: flex-start;\n",
" width: 50%;\n",
"}\n",
"\n",
"#sk-container-id-1 div.sk-parallel-item:only-child::after {\n",
" width: 0;\n",
"}\n",
"\n",
"/* Serial-specific style estimator block */\n",
"\n",
"#sk-container-id-1 div.sk-serial {\n",
" display: flex;\n",
" flex-direction: column;\n",
" align-items: center;\n",
" background-color: var(--sklearn-color-background);\n",
" padding-right: 1em;\n",
" padding-left: 1em;\n",
"}\n",
"\n",
"\n",
"/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer box that is\n",
"clickable and can be expanded/collapsed.\n",
"- Pipeline and ColumnTransformer use this feature and define the default style\n",
"- Estimators will overwrite some part of the style using the `sk-estimator` class\n",
"*/\n",
"\n",
"/* Pipeline and ColumnTransformer style (default) */\n",
"\n",
"#sk-container-id-1 div.sk-toggleable {\n",
" /* Default theme specific background. It is overwritten whether we have a\n",
" specific estimator or a Pipeline/ColumnTransformer */\n",
" background-color: var(--sklearn-color-background);\n",
"}\n",
"\n",
"/* Toggleable label */\n",
"#sk-container-id-1 label.sk-toggleable__label {\n",
" cursor: pointer;\n",
" display: flex;\n",
" width: 100%;\n",
" margin-bottom: 0;\n",
" padding: 0.5em;\n",
" box-sizing: border-box;\n",
" text-align: center;\n",
" align-items: center;\n",
" justify-content: center;\n",
" gap: 0.5em;\n",
"}\n",
"\n",
"#sk-container-id-1 label.sk-toggleable__label .caption {\n",
" font-size: 0.6rem;\n",
" font-weight: lighter;\n",
" color: var(--sklearn-color-text-muted);\n",
"}\n",
"\n",
"#sk-container-id-1 label.sk-toggleable__label-arrow:before {\n",
" /* Arrow on the left of the label */\n",
" content: \"▸\";\n",
" float: left;\n",
" margin-right: 0.25em;\n",
" color: var(--sklearn-color-icon);\n",
"}\n",
"\n",
"#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {\n",
" color: var(--sklearn-color-text);\n",
"}\n",
"\n",
"/* Toggleable content - dropdown */\n",
"\n",
"#sk-container-id-1 div.sk-toggleable__content {\n",
" display: none;\n",
" text-align: left;\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-1 div.sk-toggleable__content.fitted {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-1 div.sk-toggleable__content pre {\n",
" margin: 0.2em;\n",
" border-radius: 0.25em;\n",
" color: var(--sklearn-color-text);\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-1 div.sk-toggleable__content.fitted pre {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n",
" /* Expand drop-down */\n",
" display: block;\n",
" width: 100%;\n",
" overflow: visible;\n",
"}\n",
"\n",
"#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n",
" content: \"▾\";\n",
"}\n",
"\n",
"/* Pipeline/ColumnTransformer-specific style */\n",
"\n",
"#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
" color: var(--sklearn-color-text);\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-1 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
"\n",
"/* Estimator-specific style */\n",
"\n",
"/* Colorize estimator box */\n",
"#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-1 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-1 div.sk-label label.sk-toggleable__label,\n",
"#sk-container-id-1 div.sk-label label {\n",
" /* The background is the default theme color */\n",
" color: var(--sklearn-color-text-on-default-background);\n",
"}\n",
"\n",
"/* On hover, darken the color of the background */\n",
"#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {\n",
" color: var(--sklearn-color-text);\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"/* Label box, darken color on hover, fitted */\n",
"#sk-container-id-1 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n",
" color: var(--sklearn-color-text);\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
"\n",
"/* Estimator label */\n",
"\n",
"#sk-container-id-1 div.sk-label label {\n",
" font-family: monospace;\n",
" font-weight: bold;\n",
" line-height: 1.2em;\n",
"}\n",
"\n",
"#sk-container-id-1 div.sk-label-container {\n",
" text-align: center;\n",
"}\n",
"\n",
"/* Estimator-specific */\n",
"#sk-container-id-1 div.sk-estimator {\n",
" font-family: monospace;\n",
" border: 1px dotted var(--sklearn-color-border-box);\n",
" border-radius: 0.25em;\n",
" box-sizing: border-box;\n",
" margin-bottom: 0.5em;\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-1 div.sk-estimator.fitted {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
"}\n",
"\n",
"/* on hover */\n",
"#sk-container-id-1 div.sk-estimator:hover {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-1 div.sk-estimator.fitted:hover {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
"\n",
"/* Specification for estimator info (e.g. \"i\" and \"?\") */\n",
"\n",
"/* Common style for \"i\" and \"?\" */\n",
"\n",
".sk-estimator-doc-link,\n",
"a:link.sk-estimator-doc-link,\n",
"a:visited.sk-estimator-doc-link {\n",
" float: right;\n",
" font-size: smaller;\n",
" line-height: 1em;\n",
" font-family: monospace;\n",
" background-color: var(--sklearn-color-unfitted-level-0);\n",
" border-radius: 1em;\n",
" height: 1em;\n",
" width: 1em;\n",
" text-decoration: none !important;\n",
" margin-left: 0.5em;\n",
" text-align: center;\n",
" /* unfitted */\n",
" border: var(--sklearn-color-unfitted-level-3) 1pt solid;\n",
" color: var(--sklearn-color-unfitted-level-3);\n",
"}\n",
"\n",
".sk-estimator-doc-link.fitted,\n",
"a:link.sk-estimator-doc-link.fitted,\n",
"a:visited.sk-estimator-doc-link.fitted {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
" border: var(--sklearn-color-fitted-level-3) 1pt solid;\n",
" color: var(--sklearn-color-fitted-level-3);\n",
"}\n",
"\n",
"/* On hover */\n",
"div.sk-estimator:hover .sk-estimator-doc-link:hover,\n",
".sk-estimator-doc-link:hover,\n",
"div.sk-label-container:hover .sk-estimator-doc-link:hover,\n",
".sk-estimator-doc-link:hover {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-3);\n",
" border: var(--sklearn-color-fitted-level-0) 1pt solid;\n",
" color: var(--sklearn-color-unfitted-level-0);\n",
" text-decoration: none;\n",
"}\n",
"\n",
"div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,\n",
".sk-estimator-doc-link.fitted:hover,\n",
"div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,\n",
".sk-estimator-doc-link.fitted:hover {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-3);\n",
" border: var(--sklearn-color-fitted-level-0) 1pt solid;\n",
" color: var(--sklearn-color-fitted-level-0);\n",
" text-decoration: none;\n",
"}\n",
"\n",
"/* Span, style for the box shown on hovering the info icon */\n",
".sk-estimator-doc-link span {\n",
" display: none;\n",
" z-index: 9999;\n",
" position: relative;\n",
" font-weight: normal;\n",
" right: .2ex;\n",
" padding: .5ex;\n",
" margin: .5ex;\n",
" width: min-content;\n",
" min-width: 20ex;\n",
" max-width: 50ex;\n",
" color: var(--sklearn-color-text);\n",
" box-shadow: 2pt 2pt 4pt #999;\n",
" /* unfitted */\n",
" background: var(--sklearn-color-unfitted-level-0);\n",
" border: .5pt solid var(--sklearn-color-unfitted-level-3);\n",
"}\n",
"\n",
".sk-estimator-doc-link.fitted span {\n",
" /* fitted */\n",
" background: var(--sklearn-color-fitted-level-0);\n",
" border: var(--sklearn-color-fitted-level-3);\n",
"}\n",
"\n",
".sk-estimator-doc-link:hover span {\n",
" display: block;\n",
"}\n",
"\n",
"/* \"?\"-specific style due to the `<a>` HTML tag */\n",
"\n",
"#sk-container-id-1 a.estimator_doc_link {\n",
" float: right;\n",
" font-size: 1rem;\n",
" line-height: 1em;\n",
" font-family: monospace;\n",
" background-color: var(--sklearn-color-unfitted-level-0);\n",
" border-radius: 1rem;\n",
" height: 1rem;\n",
" width: 1rem;\n",
" text-decoration: none;\n",
" /* unfitted */\n",
" color: var(--sklearn-color-unfitted-level-1);\n",
" border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
"}\n",
"\n",
"#sk-container-id-1 a.estimator_doc_link.fitted {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
" border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
" color: var(--sklearn-color-fitted-level-1);\n",
"}\n",
"\n",
"/* On hover */\n",
"#sk-container-id-1 a.estimator_doc_link:hover {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-3);\n",
" color: var(--sklearn-color-background);\n",
" text-decoration: none;\n",
"}\n",
"\n",
"#sk-container-id-1 a.estimator_doc_link.fitted:hover {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-3);\n",
"}\n",
"\n",
".estimator-table {\n",
" font-family: monospace;\n",
"}\n",
"\n",
".estimator-table summary {\n",
" padding: .5rem;\n",
" cursor: pointer;\n",
"}\n",
"\n",
".estimator-table summary::marker {\n",
" font-size: 0.7rem;\n",
"}\n",
"\n",
".estimator-table details[open] {\n",
" padding-left: 0.1rem;\n",
" padding-right: 0.1rem;\n",
" padding-bottom: 0.3rem;\n",
"}\n",
"\n",
".estimator-table .parameters-table {\n",
" margin-left: auto !important;\n",
" margin-right: auto !important;\n",
" margin-top: 0;\n",
"}\n",
"\n",
".estimator-table .parameters-table tr:nth-child(odd) {\n",
" background-color: #fff;\n",
"}\n",
"\n",
".estimator-table .parameters-table tr:nth-child(even) {\n",
" background-color: #f6f6f6;\n",
"}\n",
"\n",
".estimator-table .parameters-table tr:hover {\n",
" background-color: #e0e0e0;\n",
"}\n",
"\n",
".estimator-table table td {\n",
" border: 1px solid rgba(106, 105, 104, 0.232);\n",
"}\n",
"\n",
"/*\n",
" `table td`is set in notebook with right text-align.\n",
" We need to overwrite it.\n",
"*/\n",
".estimator-table table td.param {\n",
" text-align: left;\n",
" position: relative;\n",
" padding: 0;\n",
"}\n",
"\n",
".user-set td {\n",
" color:rgb(255, 94, 0);\n",
" text-align: left !important;\n",
"}\n",
"\n",
".user-set td.value {\n",
" color:rgb(255, 94, 0);\n",
" background-color: transparent;\n",
"}\n",
"\n",
".default td {\n",
" color: black;\n",
" text-align: left !important;\n",
"}\n",
"\n",
".user-set td i,\n",
".default td i {\n",
" color: black;\n",
"}\n",
"\n",
"/*\n",
" Styles for parameter documentation links\n",
" We need styling for visited so jupyter doesn't overwrite it\n",
"*/\n",
"a.param-doc-link,\n",
"a.param-doc-link:link,\n",
"a.param-doc-link:visited {\n",
" text-decoration: underline dashed;\n",
" text-underline-offset: .3em;\n",
" color: inherit;\n",
" display: block;\n",
" padding: .5em;\n",
"}\n",
"\n",
"/* \"hack\" to make the entire area of the cell containing the link clickable */\n",
"a.param-doc-link::before {\n",
" position: absolute;\n",
" content: \"\";\n",
" inset: 0;\n",
"}\n",
"\n",
".param-doc-description {\n",
" display: none;\n",
" position: absolute;\n",
" z-index: 9999;\n",
" left: 0;\n",
" padding: .5ex;\n",
" margin-left: 1.5em;\n",
" color: var(--sklearn-color-text);\n",
" box-shadow: .3em .3em .4em #999;\n",
" width: max-content;\n",
" text-align: left;\n",
" max-height: 10em;\n",
" overflow-y: auto;\n",
"\n",
" /* unfitted */\n",
" background: var(--sklearn-color-unfitted-level-0);\n",
" border: thin solid var(--sklearn-color-unfitted-level-3);\n",
"}\n",
"\n",
"/* Fitted state for parameter tooltips */\n",
".fitted .param-doc-description {\n",
" /* fitted */\n",
" background: var(--sklearn-color-fitted-level-0);\n",
" border: thin solid var(--sklearn-color-fitted-level-3);\n",
"}\n",
"\n",
".param-doc-link:hover .param-doc-description {\n",
" display: block;\n",
"}\n",
"\n",
".copy-paste-icon {\n",
" background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0NDggNTEyIj48IS0tIUZvbnQgQXdlc29tZSBGcmVlIDYuNy4yIGJ5IEBmb250YXdlc29tZSAtIGh0dHBzOi8vZm9udGF3ZXNvbWUuY29tIExpY2Vuc2UgLSBodHRwczovL2ZvbnRhd2Vzb21lLmNvbS9saWNlbnNlL2ZyZWUgQ29weXJpZ2h0IDIwMjUgRm9udGljb25zLCBJbmMuLS0+PHBhdGggZD0iTTIwOCAwTDMzMi4xIDBjMTIuNyAwIDI0LjkgNS4xIDMzLjkgMTQuMWw2Ny45IDY3LjljOSA5IDE0LjEgMjEuMiAxNC4xIDMzLjlMNDQ4IDMzNmMwIDI2LjUtMjEuNSA0OC00OCA0OGwtMTkyIDBjLTI2LjUgMC00OC0yMS41LTQ4LTQ4bDAtMjg4YzAtMjYuNSAyMS41LTQ4IDQ4LTQ4ek00OCAxMjhsODAgMCAwIDY0LTY0IDAgMCAyNTYgMTkyIDAgMC0zMiA2NCAwIDAgNDhjMCAyNi41LTIxLjUgNDgtNDggNDhMNDggNTEyYy0yNi41IDAtNDgtMjEuNS00OC00OEwwIDE3NmMwLTI2LjUgMjEuNS00OCA0OC00OHoiLz48L3N2Zz4=);\n",
" background-repeat: no-repeat;\n",
" background-size: 14px 14px;\n",
" background-position: 0;\n",
" display: inline-block;\n",
" width: 14px;\n",
" height: 14px;\n",
" cursor: pointer;\n",
"}\n",
"</style><body><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>KMeans(init=&#x27;random&#x27;, n_clusters=3, n_init=1)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow\"><div><div>KMeans</div></div><div><a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html\">?<span>Documentation for KMeans</span></a><span class=\"sk-estimator-doc-link fitted\">i<span>Fitted</span></span></div></label><div class=\"sk-toggleable__content fitted\" data-param-prefix=\"\">\n",
" <div class=\"estimator-table\">\n",
" <details>\n",
" <summary>Parameters</summary>\n",
" <table class=\"parameters-table\">\n",
" <tbody>\n",
" \n",
" <tr class=\"user-set\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('n_clusters',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=n_clusters,-int%2C%20default%3D8\">\n",
" n_clusters\n",
" <span class=\"param-doc-description\">n_clusters: int, default=8<br><br>The number of clusters to form as well as the number of<br>centroids to generate.<br><br>For an example of how to choose an optimal value for `n_clusters` refer to<br>:ref:`sphx_glr_auto_examples_cluster_plot_kmeans_silhouette_analysis.py`.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">3</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"user-set\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('init',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=init,-%7B%27k-means%2B%2B%27%2C%20%27random%27%7D%2C%20callable%20or%20array-like%20of%20shape%20%20%20%20%20%20%20%20%20%20%20%20%20%28n_clusters%2C%20n_features%29%2C%20default%3D%27k-means%2B%2B%27\">\n",
" init\n",
" <span class=\"param-doc-description\">init: {'k-means++', 'random'}, callable or array-like of shape (n_clusters, n_features), default='k-means++'<br><br>Method for initialization:<br><br>* 'k-means++' : selects initial cluster centroids using sampling based on an empirical probability distribution of the points' contribution to the overall inertia. This technique speeds up convergence. The algorithm implemented is \"greedy k-means++\". It differs from the vanilla k-means++ by making several trials at each sampling step and choosing the best centroid among them.<br><br>* 'random': choose `n_clusters` observations (rows) at random from data for the initial centroids.<br><br>* If an array is passed, it should be of shape (n_clusters, n_features) and gives the initial centers.<br><br>* If a callable is passed, it should take arguments X, n_clusters and a random state and return an initialization.<br><br>For an example of how to use the different `init` strategies, see<br>:ref:`sphx_glr_auto_examples_cluster_plot_kmeans_digits.py`.<br><br>For an evaluation of the impact of initialization, see the example<br>:ref:`sphx_glr_auto_examples_cluster_plot_kmeans_stability_low_dim_dense.py`.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">&#x27;random&#x27;</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"user-set\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('n_init',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=n_init,-%27auto%27%20or%20int%2C%20default%3D%27auto%27\">\n",
" n_init\n",
" <span class=\"param-doc-description\">n_init: 'auto' or int, default='auto'<br><br>Number of times the k-means algorithm is run with different centroid<br>seeds. The final results is the best output of `n_init` consecutive runs<br>in terms of inertia. Several runs are recommended for sparse<br>high-dimensional problems (see :ref:`kmeans_sparse_high_dim`).<br><br>When `n_init='auto'`, the number of runs depends on the value of init:<br>10 if using `init='random'` or `init` is a callable;<br>1 if using `init='k-means++'` or `init` is an array-like.<br><br>.. versionadded:: 1.2<br> Added 'auto' option for `n_init`.<br><br>.. versionchanged:: 1.4<br> Default value for `n_init` changed to `'auto'`.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">1</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('max_iter',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=max_iter,-int%2C%20default%3D300\">\n",
" max_iter\n",
" <span class=\"param-doc-description\">max_iter: int, default=300<br><br>Maximum number of iterations of the k-means algorithm for a<br>single run.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">300</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('tol',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=tol,-float%2C%20default%3D1e-4\">\n",
" tol\n",
" <span class=\"param-doc-description\">tol: float, default=1e-4<br><br>Relative tolerance with regards to Frobenius norm of the difference<br>in the cluster centers of two consecutive iterations to declare<br>convergence.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">0.0001</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('verbose',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=verbose,-int%2C%20default%3D0\">\n",
" verbose\n",
" <span class=\"param-doc-description\">verbose: int, default=0<br><br>Verbosity mode.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">0</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('random_state',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=random_state,-int%2C%20RandomState%20instance%20or%20None%2C%20default%3DNone\">\n",
" random_state\n",
" <span class=\"param-doc-description\">random_state: int, RandomState instance or None, default=None<br><br>Determines random number generation for centroid initialization. Use<br>an int to make the randomness deterministic.<br>See :term:`Glossary <random_state>`.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">None</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('copy_x',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=copy_x,-bool%2C%20default%3DTrue\">\n",
" copy_x\n",
" <span class=\"param-doc-description\">copy_x: bool, default=True<br><br>When pre-computing distances it is more numerically accurate to center<br>the data first. If copy_x is True (default), then the original data is<br>not modified. If False, the original data is modified, and put back<br>before the function returns, but small numerical differences may be<br>introduced by subtracting and then adding the data mean. Note that if<br>the original data is not C-contiguous, a copy will be made even if<br>copy_x is False. If the original data is sparse, but not in CSR format,<br>a copy will be made even if copy_x is False.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">True</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('algorithm',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=algorithm,-%7B%22lloyd%22%2C%20%22elkan%22%7D%2C%20default%3D%22lloyd%22\">\n",
" algorithm\n",
" <span class=\"param-doc-description\">algorithm: {\"lloyd\", \"elkan\"}, default=\"lloyd\"<br><br>K-means algorithm to use. The classical EM-style algorithm is `\"lloyd\"`.<br>The `\"elkan\"` variation can be more efficient on some datasets with<br>well-defined clusters, by using the triangle inequality. However it's<br>more memory intensive due to the allocation of an extra array of shape<br>`(n_samples, n_clusters)`.<br><br>.. versionchanged:: 0.18<br> Added Elkan algorithm<br><br>.. versionchanged:: 1.1<br> Renamed \"full\" to \"lloyd\", and deprecated \"auto\" and \"full\".<br> Changed \"auto\" to use \"lloyd\" instead of \"elkan\".</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">&#x27;lloyd&#x27;</td>\n",
" </tr>\n",
" \n",
" </tbody>\n",
" </table>\n",
" </details>\n",
" </div>\n",
" </div></div></div></div></div><script>function copyToClipboard(text, element) {\n",
" // Get the parameter prefix from the closest toggleable content\n",
" const toggleableContent = element.closest('.sk-toggleable__content');\n",
" const paramPrefix = toggleableContent ? toggleableContent.dataset.paramPrefix : '';\n",
" const fullParamName = paramPrefix ? `${paramPrefix}${text}` : text;\n",
"\n",
" const originalStyle = element.style;\n",
" const computedStyle = window.getComputedStyle(element);\n",
" const originalWidth = computedStyle.width;\n",
" const originalHTML = element.innerHTML.replace('Copied!', '');\n",
"\n",
" navigator.clipboard.writeText(fullParamName)\n",
" .then(() => {\n",
" element.style.width = originalWidth;\n",
" element.style.color = 'green';\n",
" element.innerHTML = \"Copied!\";\n",
"\n",
" setTimeout(() => {\n",
" element.innerHTML = originalHTML;\n",
" element.style = originalStyle;\n",
" }, 2000);\n",
" })\n",
" .catch(err => {\n",
" console.error('Failed to copy:', err);\n",
" element.style.color = 'red';\n",
" element.innerHTML = \"Failed!\";\n",
" setTimeout(() => {\n",
" element.innerHTML = originalHTML;\n",
" element.style = originalStyle;\n",
" }, 2000);\n",
" });\n",
" return false;\n",
"}\n",
"\n",
"document.querySelectorAll('.copy-paste-icon').forEach(function(element) {\n",
" const toggleableContent = element.closest('.sk-toggleable__content');\n",
" const paramPrefix = toggleableContent ? toggleableContent.dataset.paramPrefix : '';\n",
" const paramName = element.parentElement.nextElementSibling\n",
" .textContent.trim().split(' ')[0];\n",
" const fullParamName = paramPrefix ? `${paramPrefix}${paramName}` : paramName;\n",
"\n",
" element.setAttribute('title', fullParamName);\n",
"});\n",
"\n",
"\n",
"/**\n",
" * Adapted from Skrub\n",
" * https://github.com/skrub-data/skrub/blob/403466d1d5d4dc76a7ef569b3f8228db59a31dc3/skrub/_reporting/_data/templates/report.js#L789\n",
" * @returns \"light\" or \"dark\"\n",
" */\n",
"function detectTheme(element) {\n",
" const body = document.querySelector('body');\n",
"\n",
" // Check VSCode theme\n",
" const themeKindAttr = body.getAttribute('data-vscode-theme-kind');\n",
" const themeNameAttr = body.getAttribute('data-vscode-theme-name');\n",
"\n",
" if (themeKindAttr && themeNameAttr) {\n",
" const themeKind = themeKindAttr.toLowerCase();\n",
" const themeName = themeNameAttr.toLowerCase();\n",
"\n",
" if (themeKind.includes(\"dark\") || themeName.includes(\"dark\")) {\n",
" return \"dark\";\n",
" }\n",
" if (themeKind.includes(\"light\") || themeName.includes(\"light\")) {\n",
" return \"light\";\n",
" }\n",
" }\n",
"\n",
" // Check Jupyter theme\n",
" if (body.getAttribute('data-jp-theme-light') === 'false') {\n",
" return 'dark';\n",
" } else if (body.getAttribute('data-jp-theme-light') === 'true') {\n",
" return 'light';\n",
" }\n",
"\n",
" // Guess based on a parent element's color\n",
" const color = window.getComputedStyle(element.parentNode, null).getPropertyValue('color');\n",
" const match = color.match(/^rgb\\s*\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)\\s*$/i);\n",
" if (match) {\n",
" const [r, g, b] = [\n",
" parseFloat(match[1]),\n",
" parseFloat(match[2]),\n",
" parseFloat(match[3])\n",
" ];\n",
"\n",
" // https://en.wikipedia.org/wiki/HSL_and_HSV#Lightness\n",
" const luma = 0.299 * r + 0.587 * g + 0.114 * b;\n",
"\n",
" if (luma > 180) {\n",
" // If the text is very bright we have a dark theme\n",
" return 'dark';\n",
" }\n",
" if (luma < 75) {\n",
" // If the text is very dark we have a light theme\n",
" return 'light';\n",
" }\n",
" // Otherwise fall back to the next heuristic.\n",
" }\n",
"\n",
" // Fallback to system preference\n",
" return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';\n",
"}\n",
"\n",
"\n",
"function forceTheme(elementId) {\n",
" const estimatorElement = document.querySelector(`#${elementId}`);\n",
" if (estimatorElement === null) {\n",
" console.error(`Element with id ${elementId} not found.`);\n",
" } else {\n",
" const theme = detectTheme(estimatorElement);\n",
" estimatorElement.classList.add(theme);\n",
" }\n",
"}\n",
"\n",
"forceTheme('sk-container-id-1');</script></body>"
],
"text/plain": [
"KMeans(init='random', n_clusters=3, n_init=1)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"kmeans = KMeans(n_clusters=3, init='random', n_init=1)\n",
"kmeans.fit(iris.data)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(0)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(0)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1))]\n"
]
}
],
"source": [
"print(list(zip(iris.target,kmeans.labels_)))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.7514854021988338\n",
"0.7649861514489815\n",
"0.7302382722834697\n",
"0.5528190123564095\n"
]
}
],
"source": [
"print(metrics.homogeneity_score(iris.target, kmeans.labels_))\n",
"print(metrics.completeness_score(iris.target, kmeans.labels_))\n",
"print(metrics.adjusted_rand_score(iris.target, kmeans.labels_))\n",
"print(metrics.silhouette_score(iris.data, kmeans.labels_))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>#sk-container-id-2 {\n",
" /* Definition of color scheme common for light and dark mode */\n",
" --sklearn-color-text: #000;\n",
" --sklearn-color-text-muted: #666;\n",
" --sklearn-color-line: gray;\n",
" /* Definition of color scheme for unfitted estimators */\n",
" --sklearn-color-unfitted-level-0: #fff5e6;\n",
" --sklearn-color-unfitted-level-1: #f6e4d2;\n",
" --sklearn-color-unfitted-level-2: #ffe0b3;\n",
" --sklearn-color-unfitted-level-3: chocolate;\n",
" /* Definition of color scheme for fitted estimators */\n",
" --sklearn-color-fitted-level-0: #f0f8ff;\n",
" --sklearn-color-fitted-level-1: #d4ebff;\n",
" --sklearn-color-fitted-level-2: #b3dbfd;\n",
" --sklearn-color-fitted-level-3: cornflowerblue;\n",
"}\n",
"\n",
"#sk-container-id-2.light {\n",
" /* Specific color for light theme */\n",
" --sklearn-color-text-on-default-background: black;\n",
" --sklearn-color-background: white;\n",
" --sklearn-color-border-box: black;\n",
" --sklearn-color-icon: #696969;\n",
"}\n",
"\n",
"#sk-container-id-2.dark {\n",
" --sklearn-color-text-on-default-background: white;\n",
" --sklearn-color-background: #111;\n",
" --sklearn-color-border-box: white;\n",
" --sklearn-color-icon: #878787;\n",
"}\n",
"\n",
"#sk-container-id-2 {\n",
" color: var(--sklearn-color-text);\n",
"}\n",
"\n",
"#sk-container-id-2 pre {\n",
" padding: 0;\n",
"}\n",
"\n",
"#sk-container-id-2 input.sk-hidden--visually {\n",
" border: 0;\n",
" clip: rect(1px 1px 1px 1px);\n",
" clip: rect(1px, 1px, 1px, 1px);\n",
" height: 1px;\n",
" margin: -1px;\n",
" overflow: hidden;\n",
" padding: 0;\n",
" position: absolute;\n",
" width: 1px;\n",
"}\n",
"\n",
"#sk-container-id-2 div.sk-dashed-wrapped {\n",
" border: 1px dashed var(--sklearn-color-line);\n",
" margin: 0 0.4em 0.5em 0.4em;\n",
" box-sizing: border-box;\n",
" padding-bottom: 0.4em;\n",
" background-color: var(--sklearn-color-background);\n",
"}\n",
"\n",
"#sk-container-id-2 div.sk-container {\n",
" /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n",
" but bootstrap.min.css set `[hidden] { display: none !important; }`\n",
" so we also need the `!important` here to be able to override the\n",
" default hidden behavior on the sphinx rendered scikit-learn.org.\n",
" See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n",
" display: inline-block !important;\n",
" position: relative;\n",
"}\n",
"\n",
"#sk-container-id-2 div.sk-text-repr-fallback {\n",
" display: none;\n",
"}\n",
"\n",
"div.sk-parallel-item,\n",
"div.sk-serial,\n",
"div.sk-item {\n",
" /* draw centered vertical line to link estimators */\n",
" background-image: linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));\n",
" background-size: 2px 100%;\n",
" background-repeat: no-repeat;\n",
" background-position: center center;\n",
"}\n",
"\n",
"/* Parallel-specific style estimator block */\n",
"\n",
"#sk-container-id-2 div.sk-parallel-item::after {\n",
" content: \"\";\n",
" width: 100%;\n",
" border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n",
" flex-grow: 1;\n",
"}\n",
"\n",
"#sk-container-id-2 div.sk-parallel {\n",
" display: flex;\n",
" align-items: stretch;\n",
" justify-content: center;\n",
" background-color: var(--sklearn-color-background);\n",
" position: relative;\n",
"}\n",
"\n",
"#sk-container-id-2 div.sk-parallel-item {\n",
" display: flex;\n",
" flex-direction: column;\n",
"}\n",
"\n",
"#sk-container-id-2 div.sk-parallel-item:first-child::after {\n",
" align-self: flex-end;\n",
" width: 50%;\n",
"}\n",
"\n",
"#sk-container-id-2 div.sk-parallel-item:last-child::after {\n",
" align-self: flex-start;\n",
" width: 50%;\n",
"}\n",
"\n",
"#sk-container-id-2 div.sk-parallel-item:only-child::after {\n",
" width: 0;\n",
"}\n",
"\n",
"/* Serial-specific style estimator block */\n",
"\n",
"#sk-container-id-2 div.sk-serial {\n",
" display: flex;\n",
" flex-direction: column;\n",
" align-items: center;\n",
" background-color: var(--sklearn-color-background);\n",
" padding-right: 1em;\n",
" padding-left: 1em;\n",
"}\n",
"\n",
"\n",
"/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer box that is\n",
"clickable and can be expanded/collapsed.\n",
"- Pipeline and ColumnTransformer use this feature and define the default style\n",
"- Estimators will overwrite some part of the style using the `sk-estimator` class\n",
"*/\n",
"\n",
"/* Pipeline and ColumnTransformer style (default) */\n",
"\n",
"#sk-container-id-2 div.sk-toggleable {\n",
" /* Default theme specific background. It is overwritten whether we have a\n",
" specific estimator or a Pipeline/ColumnTransformer */\n",
" background-color: var(--sklearn-color-background);\n",
"}\n",
"\n",
"/* Toggleable label */\n",
"#sk-container-id-2 label.sk-toggleable__label {\n",
" cursor: pointer;\n",
" display: flex;\n",
" width: 100%;\n",
" margin-bottom: 0;\n",
" padding: 0.5em;\n",
" box-sizing: border-box;\n",
" text-align: center;\n",
" align-items: center;\n",
" justify-content: center;\n",
" gap: 0.5em;\n",
"}\n",
"\n",
"#sk-container-id-2 label.sk-toggleable__label .caption {\n",
" font-size: 0.6rem;\n",
" font-weight: lighter;\n",
" color: var(--sklearn-color-text-muted);\n",
"}\n",
"\n",
"#sk-container-id-2 label.sk-toggleable__label-arrow:before {\n",
" /* Arrow on the left of the label */\n",
" content: \"▸\";\n",
" float: left;\n",
" margin-right: 0.25em;\n",
" color: var(--sklearn-color-icon);\n",
"}\n",
"\n",
"#sk-container-id-2 label.sk-toggleable__label-arrow:hover:before {\n",
" color: var(--sklearn-color-text);\n",
"}\n",
"\n",
"/* Toggleable content - dropdown */\n",
"\n",
"#sk-container-id-2 div.sk-toggleable__content {\n",
" display: none;\n",
" text-align: left;\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-2 div.sk-toggleable__content.fitted {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-2 div.sk-toggleable__content pre {\n",
" margin: 0.2em;\n",
" border-radius: 0.25em;\n",
" color: var(--sklearn-color-text);\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-2 div.sk-toggleable__content.fitted pre {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-2 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n",
" /* Expand drop-down */\n",
" display: block;\n",
" width: 100%;\n",
" overflow: visible;\n",
"}\n",
"\n",
"#sk-container-id-2 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n",
" content: \"▾\";\n",
"}\n",
"\n",
"/* Pipeline/ColumnTransformer-specific style */\n",
"\n",
"#sk-container-id-2 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
" color: var(--sklearn-color-text);\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-2 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
"\n",
"/* Estimator-specific style */\n",
"\n",
"/* Colorize estimator box */\n",
"#sk-container-id-2 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-2 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-2 div.sk-label label.sk-toggleable__label,\n",
"#sk-container-id-2 div.sk-label label {\n",
" /* The background is the default theme color */\n",
" color: var(--sklearn-color-text-on-default-background);\n",
"}\n",
"\n",
"/* On hover, darken the color of the background */\n",
"#sk-container-id-2 div.sk-label:hover label.sk-toggleable__label {\n",
" color: var(--sklearn-color-text);\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"/* Label box, darken color on hover, fitted */\n",
"#sk-container-id-2 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n",
" color: var(--sklearn-color-text);\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
"\n",
"/* Estimator label */\n",
"\n",
"#sk-container-id-2 div.sk-label label {\n",
" font-family: monospace;\n",
" font-weight: bold;\n",
" line-height: 1.2em;\n",
"}\n",
"\n",
"#sk-container-id-2 div.sk-label-container {\n",
" text-align: center;\n",
"}\n",
"\n",
"/* Estimator-specific */\n",
"#sk-container-id-2 div.sk-estimator {\n",
" font-family: monospace;\n",
" border: 1px dotted var(--sklearn-color-border-box);\n",
" border-radius: 0.25em;\n",
" box-sizing: border-box;\n",
" margin-bottom: 0.5em;\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-2 div.sk-estimator.fitted {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
"}\n",
"\n",
"/* on hover */\n",
"#sk-container-id-2 div.sk-estimator:hover {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-2 div.sk-estimator.fitted:hover {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
"\n",
"/* Specification for estimator info (e.g. \"i\" and \"?\") */\n",
"\n",
"/* Common style for \"i\" and \"?\" */\n",
"\n",
".sk-estimator-doc-link,\n",
"a:link.sk-estimator-doc-link,\n",
"a:visited.sk-estimator-doc-link {\n",
" float: right;\n",
" font-size: smaller;\n",
" line-height: 1em;\n",
" font-family: monospace;\n",
" background-color: var(--sklearn-color-unfitted-level-0);\n",
" border-radius: 1em;\n",
" height: 1em;\n",
" width: 1em;\n",
" text-decoration: none !important;\n",
" margin-left: 0.5em;\n",
" text-align: center;\n",
" /* unfitted */\n",
" border: var(--sklearn-color-unfitted-level-3) 1pt solid;\n",
" color: var(--sklearn-color-unfitted-level-3);\n",
"}\n",
"\n",
".sk-estimator-doc-link.fitted,\n",
"a:link.sk-estimator-doc-link.fitted,\n",
"a:visited.sk-estimator-doc-link.fitted {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
" border: var(--sklearn-color-fitted-level-3) 1pt solid;\n",
" color: var(--sklearn-color-fitted-level-3);\n",
"}\n",
"\n",
"/* On hover */\n",
"div.sk-estimator:hover .sk-estimator-doc-link:hover,\n",
".sk-estimator-doc-link:hover,\n",
"div.sk-label-container:hover .sk-estimator-doc-link:hover,\n",
".sk-estimator-doc-link:hover {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-3);\n",
" border: var(--sklearn-color-fitted-level-0) 1pt solid;\n",
" color: var(--sklearn-color-unfitted-level-0);\n",
" text-decoration: none;\n",
"}\n",
"\n",
"div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,\n",
".sk-estimator-doc-link.fitted:hover,\n",
"div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,\n",
".sk-estimator-doc-link.fitted:hover {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-3);\n",
" border: var(--sklearn-color-fitted-level-0) 1pt solid;\n",
" color: var(--sklearn-color-fitted-level-0);\n",
" text-decoration: none;\n",
"}\n",
"\n",
"/* Span, style for the box shown on hovering the info icon */\n",
".sk-estimator-doc-link span {\n",
" display: none;\n",
" z-index: 9999;\n",
" position: relative;\n",
" font-weight: normal;\n",
" right: .2ex;\n",
" padding: .5ex;\n",
" margin: .5ex;\n",
" width: min-content;\n",
" min-width: 20ex;\n",
" max-width: 50ex;\n",
" color: var(--sklearn-color-text);\n",
" box-shadow: 2pt 2pt 4pt #999;\n",
" /* unfitted */\n",
" background: var(--sklearn-color-unfitted-level-0);\n",
" border: .5pt solid var(--sklearn-color-unfitted-level-3);\n",
"}\n",
"\n",
".sk-estimator-doc-link.fitted span {\n",
" /* fitted */\n",
" background: var(--sklearn-color-fitted-level-0);\n",
" border: var(--sklearn-color-fitted-level-3);\n",
"}\n",
"\n",
".sk-estimator-doc-link:hover span {\n",
" display: block;\n",
"}\n",
"\n",
"/* \"?\"-specific style due to the `<a>` HTML tag */\n",
"\n",
"#sk-container-id-2 a.estimator_doc_link {\n",
" float: right;\n",
" font-size: 1rem;\n",
" line-height: 1em;\n",
" font-family: monospace;\n",
" background-color: var(--sklearn-color-unfitted-level-0);\n",
" border-radius: 1rem;\n",
" height: 1rem;\n",
" width: 1rem;\n",
" text-decoration: none;\n",
" /* unfitted */\n",
" color: var(--sklearn-color-unfitted-level-1);\n",
" border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
"}\n",
"\n",
"#sk-container-id-2 a.estimator_doc_link.fitted {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
" border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
" color: var(--sklearn-color-fitted-level-1);\n",
"}\n",
"\n",
"/* On hover */\n",
"#sk-container-id-2 a.estimator_doc_link:hover {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-3);\n",
" color: var(--sklearn-color-background);\n",
" text-decoration: none;\n",
"}\n",
"\n",
"#sk-container-id-2 a.estimator_doc_link.fitted:hover {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-3);\n",
"}\n",
"\n",
".estimator-table {\n",
" font-family: monospace;\n",
"}\n",
"\n",
".estimator-table summary {\n",
" padding: .5rem;\n",
" cursor: pointer;\n",
"}\n",
"\n",
".estimator-table summary::marker {\n",
" font-size: 0.7rem;\n",
"}\n",
"\n",
".estimator-table details[open] {\n",
" padding-left: 0.1rem;\n",
" padding-right: 0.1rem;\n",
" padding-bottom: 0.3rem;\n",
"}\n",
"\n",
".estimator-table .parameters-table {\n",
" margin-left: auto !important;\n",
" margin-right: auto !important;\n",
" margin-top: 0;\n",
"}\n",
"\n",
".estimator-table .parameters-table tr:nth-child(odd) {\n",
" background-color: #fff;\n",
"}\n",
"\n",
".estimator-table .parameters-table tr:nth-child(even) {\n",
" background-color: #f6f6f6;\n",
"}\n",
"\n",
".estimator-table .parameters-table tr:hover {\n",
" background-color: #e0e0e0;\n",
"}\n",
"\n",
".estimator-table table td {\n",
" border: 1px solid rgba(106, 105, 104, 0.232);\n",
"}\n",
"\n",
"/*\n",
" `table td`is set in notebook with right text-align.\n",
" We need to overwrite it.\n",
"*/\n",
".estimator-table table td.param {\n",
" text-align: left;\n",
" position: relative;\n",
" padding: 0;\n",
"}\n",
"\n",
".user-set td {\n",
" color:rgb(255, 94, 0);\n",
" text-align: left !important;\n",
"}\n",
"\n",
".user-set td.value {\n",
" color:rgb(255, 94, 0);\n",
" background-color: transparent;\n",
"}\n",
"\n",
".default td {\n",
" color: black;\n",
" text-align: left !important;\n",
"}\n",
"\n",
".user-set td i,\n",
".default td i {\n",
" color: black;\n",
"}\n",
"\n",
"/*\n",
" Styles for parameter documentation links\n",
" We need styling for visited so jupyter doesn't overwrite it\n",
"*/\n",
"a.param-doc-link,\n",
"a.param-doc-link:link,\n",
"a.param-doc-link:visited {\n",
" text-decoration: underline dashed;\n",
" text-underline-offset: .3em;\n",
" color: inherit;\n",
" display: block;\n",
" padding: .5em;\n",
"}\n",
"\n",
"/* \"hack\" to make the entire area of the cell containing the link clickable */\n",
"a.param-doc-link::before {\n",
" position: absolute;\n",
" content: \"\";\n",
" inset: 0;\n",
"}\n",
"\n",
".param-doc-description {\n",
" display: none;\n",
" position: absolute;\n",
" z-index: 9999;\n",
" left: 0;\n",
" padding: .5ex;\n",
" margin-left: 1.5em;\n",
" color: var(--sklearn-color-text);\n",
" box-shadow: .3em .3em .4em #999;\n",
" width: max-content;\n",
" text-align: left;\n",
" max-height: 10em;\n",
" overflow-y: auto;\n",
"\n",
" /* unfitted */\n",
" background: var(--sklearn-color-unfitted-level-0);\n",
" border: thin solid var(--sklearn-color-unfitted-level-3);\n",
"}\n",
"\n",
"/* Fitted state for parameter tooltips */\n",
".fitted .param-doc-description {\n",
" /* fitted */\n",
" background: var(--sklearn-color-fitted-level-0);\n",
" border: thin solid var(--sklearn-color-fitted-level-3);\n",
"}\n",
"\n",
".param-doc-link:hover .param-doc-description {\n",
" display: block;\n",
"}\n",
"\n",
".copy-paste-icon {\n",
" background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0NDggNTEyIj48IS0tIUZvbnQgQXdlc29tZSBGcmVlIDYuNy4yIGJ5IEBmb250YXdlc29tZSAtIGh0dHBzOi8vZm9udGF3ZXNvbWUuY29tIExpY2Vuc2UgLSBodHRwczovL2ZvbnRhd2Vzb21lLmNvbS9saWNlbnNlL2ZyZWUgQ29weXJpZ2h0IDIwMjUgRm9udGljb25zLCBJbmMuLS0+PHBhdGggZD0iTTIwOCAwTDMzMi4xIDBjMTIuNyAwIDI0LjkgNS4xIDMzLjkgMTQuMWw2Ny45IDY3LjljOSA5IDE0LjEgMjEuMiAxNC4xIDMzLjlMNDQ4IDMzNmMwIDI2LjUtMjEuNSA0OC00OCA0OGwtMTkyIDBjLTI2LjUgMC00OC0yMS41LTQ4LTQ4bDAtMjg4YzAtMjYuNSAyMS41LTQ4IDQ4LTQ4ek00OCAxMjhsODAgMCAwIDY0LTY0IDAgMCAyNTYgMTkyIDAgMC0zMiA2NCAwIDAgNDhjMCAyNi41LTIxLjUgNDgtNDggNDhMNDggNTEyYy0yNi41IDAtNDgtMjEuNS00OC00OEwwIDE3NmMwLTI2LjUgMjEuNS00OCA0OC00OHoiLz48L3N2Zz4=);\n",
" background-repeat: no-repeat;\n",
" background-size: 14px 14px;\n",
" background-position: 0;\n",
" display: inline-block;\n",
" width: 14px;\n",
" height: 14px;\n",
" cursor: pointer;\n",
"}\n",
"</style><body><div id=\"sk-container-id-2\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>KMeans(init=&#x27;random&#x27;, n_clusters=3, n_init=10)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-2\" type=\"checkbox\" checked><label for=\"sk-estimator-id-2\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow\"><div><div>KMeans</div></div><div><a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html\">?<span>Documentation for KMeans</span></a><span class=\"sk-estimator-doc-link fitted\">i<span>Fitted</span></span></div></label><div class=\"sk-toggleable__content fitted\" data-param-prefix=\"\">\n",
" <div class=\"estimator-table\">\n",
" <details>\n",
" <summary>Parameters</summary>\n",
" <table class=\"parameters-table\">\n",
" <tbody>\n",
" \n",
" <tr class=\"user-set\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('n_clusters',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=n_clusters,-int%2C%20default%3D8\">\n",
" n_clusters\n",
" <span class=\"param-doc-description\">n_clusters: int, default=8<br><br>The number of clusters to form as well as the number of<br>centroids to generate.<br><br>For an example of how to choose an optimal value for `n_clusters` refer to<br>:ref:`sphx_glr_auto_examples_cluster_plot_kmeans_silhouette_analysis.py`.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">3</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"user-set\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('init',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=init,-%7B%27k-means%2B%2B%27%2C%20%27random%27%7D%2C%20callable%20or%20array-like%20of%20shape%20%20%20%20%20%20%20%20%20%20%20%20%20%28n_clusters%2C%20n_features%29%2C%20default%3D%27k-means%2B%2B%27\">\n",
" init\n",
" <span class=\"param-doc-description\">init: {'k-means++', 'random'}, callable or array-like of shape (n_clusters, n_features), default='k-means++'<br><br>Method for initialization:<br><br>* 'k-means++' : selects initial cluster centroids using sampling based on an empirical probability distribution of the points' contribution to the overall inertia. This technique speeds up convergence. The algorithm implemented is \"greedy k-means++\". It differs from the vanilla k-means++ by making several trials at each sampling step and choosing the best centroid among them.<br><br>* 'random': choose `n_clusters` observations (rows) at random from data for the initial centroids.<br><br>* If an array is passed, it should be of shape (n_clusters, n_features) and gives the initial centers.<br><br>* If a callable is passed, it should take arguments X, n_clusters and a random state and return an initialization.<br><br>For an example of how to use the different `init` strategies, see<br>:ref:`sphx_glr_auto_examples_cluster_plot_kmeans_digits.py`.<br><br>For an evaluation of the impact of initialization, see the example<br>:ref:`sphx_glr_auto_examples_cluster_plot_kmeans_stability_low_dim_dense.py`.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">&#x27;random&#x27;</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"user-set\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('n_init',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=n_init,-%27auto%27%20or%20int%2C%20default%3D%27auto%27\">\n",
" n_init\n",
" <span class=\"param-doc-description\">n_init: 'auto' or int, default='auto'<br><br>Number of times the k-means algorithm is run with different centroid<br>seeds. The final results is the best output of `n_init` consecutive runs<br>in terms of inertia. Several runs are recommended for sparse<br>high-dimensional problems (see :ref:`kmeans_sparse_high_dim`).<br><br>When `n_init='auto'`, the number of runs depends on the value of init:<br>10 if using `init='random'` or `init` is a callable;<br>1 if using `init='k-means++'` or `init` is an array-like.<br><br>.. versionadded:: 1.2<br> Added 'auto' option for `n_init`.<br><br>.. versionchanged:: 1.4<br> Default value for `n_init` changed to `'auto'`.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">10</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('max_iter',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=max_iter,-int%2C%20default%3D300\">\n",
" max_iter\n",
" <span class=\"param-doc-description\">max_iter: int, default=300<br><br>Maximum number of iterations of the k-means algorithm for a<br>single run.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">300</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('tol',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=tol,-float%2C%20default%3D1e-4\">\n",
" tol\n",
" <span class=\"param-doc-description\">tol: float, default=1e-4<br><br>Relative tolerance with regards to Frobenius norm of the difference<br>in the cluster centers of two consecutive iterations to declare<br>convergence.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">0.0001</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('verbose',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=verbose,-int%2C%20default%3D0\">\n",
" verbose\n",
" <span class=\"param-doc-description\">verbose: int, default=0<br><br>Verbosity mode.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">0</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('random_state',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=random_state,-int%2C%20RandomState%20instance%20or%20None%2C%20default%3DNone\">\n",
" random_state\n",
" <span class=\"param-doc-description\">random_state: int, RandomState instance or None, default=None<br><br>Determines random number generation for centroid initialization. Use<br>an int to make the randomness deterministic.<br>See :term:`Glossary <random_state>`.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">None</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('copy_x',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=copy_x,-bool%2C%20default%3DTrue\">\n",
" copy_x\n",
" <span class=\"param-doc-description\">copy_x: bool, default=True<br><br>When pre-computing distances it is more numerically accurate to center<br>the data first. If copy_x is True (default), then the original data is<br>not modified. If False, the original data is modified, and put back<br>before the function returns, but small numerical differences may be<br>introduced by subtracting and then adding the data mean. Note that if<br>the original data is not C-contiguous, a copy will be made even if<br>copy_x is False. If the original data is sparse, but not in CSR format,<br>a copy will be made even if copy_x is False.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">True</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('algorithm',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=algorithm,-%7B%22lloyd%22%2C%20%22elkan%22%7D%2C%20default%3D%22lloyd%22\">\n",
" algorithm\n",
" <span class=\"param-doc-description\">algorithm: {\"lloyd\", \"elkan\"}, default=\"lloyd\"<br><br>K-means algorithm to use. The classical EM-style algorithm is `\"lloyd\"`.<br>The `\"elkan\"` variation can be more efficient on some datasets with<br>well-defined clusters, by using the triangle inequality. However it's<br>more memory intensive due to the allocation of an extra array of shape<br>`(n_samples, n_clusters)`.<br><br>.. versionchanged:: 0.18<br> Added Elkan algorithm<br><br>.. versionchanged:: 1.1<br> Renamed \"full\" to \"lloyd\", and deprecated \"auto\" and \"full\".<br> Changed \"auto\" to use \"lloyd\" instead of \"elkan\".</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">&#x27;lloyd&#x27;</td>\n",
" </tr>\n",
" \n",
" </tbody>\n",
" </table>\n",
" </details>\n",
" </div>\n",
" </div></div></div></div></div><script>function copyToClipboard(text, element) {\n",
" // Get the parameter prefix from the closest toggleable content\n",
" const toggleableContent = element.closest('.sk-toggleable__content');\n",
" const paramPrefix = toggleableContent ? toggleableContent.dataset.paramPrefix : '';\n",
" const fullParamName = paramPrefix ? `${paramPrefix}${text}` : text;\n",
"\n",
" const originalStyle = element.style;\n",
" const computedStyle = window.getComputedStyle(element);\n",
" const originalWidth = computedStyle.width;\n",
" const originalHTML = element.innerHTML.replace('Copied!', '');\n",
"\n",
" navigator.clipboard.writeText(fullParamName)\n",
" .then(() => {\n",
" element.style.width = originalWidth;\n",
" element.style.color = 'green';\n",
" element.innerHTML = \"Copied!\";\n",
"\n",
" setTimeout(() => {\n",
" element.innerHTML = originalHTML;\n",
" element.style = originalStyle;\n",
" }, 2000);\n",
" })\n",
" .catch(err => {\n",
" console.error('Failed to copy:', err);\n",
" element.style.color = 'red';\n",
" element.innerHTML = \"Failed!\";\n",
" setTimeout(() => {\n",
" element.innerHTML = originalHTML;\n",
" element.style = originalStyle;\n",
" }, 2000);\n",
" });\n",
" return false;\n",
"}\n",
"\n",
"document.querySelectorAll('.copy-paste-icon').forEach(function(element) {\n",
" const toggleableContent = element.closest('.sk-toggleable__content');\n",
" const paramPrefix = toggleableContent ? toggleableContent.dataset.paramPrefix : '';\n",
" const paramName = element.parentElement.nextElementSibling\n",
" .textContent.trim().split(' ')[0];\n",
" const fullParamName = paramPrefix ? `${paramPrefix}${paramName}` : paramName;\n",
"\n",
" element.setAttribute('title', fullParamName);\n",
"});\n",
"\n",
"\n",
"/**\n",
" * Adapted from Skrub\n",
" * https://github.com/skrub-data/skrub/blob/403466d1d5d4dc76a7ef569b3f8228db59a31dc3/skrub/_reporting/_data/templates/report.js#L789\n",
" * @returns \"light\" or \"dark\"\n",
" */\n",
"function detectTheme(element) {\n",
" const body = document.querySelector('body');\n",
"\n",
" // Check VSCode theme\n",
" const themeKindAttr = body.getAttribute('data-vscode-theme-kind');\n",
" const themeNameAttr = body.getAttribute('data-vscode-theme-name');\n",
"\n",
" if (themeKindAttr && themeNameAttr) {\n",
" const themeKind = themeKindAttr.toLowerCase();\n",
" const themeName = themeNameAttr.toLowerCase();\n",
"\n",
" if (themeKind.includes(\"dark\") || themeName.includes(\"dark\")) {\n",
" return \"dark\";\n",
" }\n",
" if (themeKind.includes(\"light\") || themeName.includes(\"light\")) {\n",
" return \"light\";\n",
" }\n",
" }\n",
"\n",
" // Check Jupyter theme\n",
" if (body.getAttribute('data-jp-theme-light') === 'false') {\n",
" return 'dark';\n",
" } else if (body.getAttribute('data-jp-theme-light') === 'true') {\n",
" return 'light';\n",
" }\n",
"\n",
" // Guess based on a parent element's color\n",
" const color = window.getComputedStyle(element.parentNode, null).getPropertyValue('color');\n",
" const match = color.match(/^rgb\\s*\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)\\s*$/i);\n",
" if (match) {\n",
" const [r, g, b] = [\n",
" parseFloat(match[1]),\n",
" parseFloat(match[2]),\n",
" parseFloat(match[3])\n",
" ];\n",
"\n",
" // https://en.wikipedia.org/wiki/HSL_and_HSV#Lightness\n",
" const luma = 0.299 * r + 0.587 * g + 0.114 * b;\n",
"\n",
" if (luma > 180) {\n",
" // If the text is very bright we have a dark theme\n",
" return 'dark';\n",
" }\n",
" if (luma < 75) {\n",
" // If the text is very dark we have a light theme\n",
" return 'light';\n",
" }\n",
" // Otherwise fall back to the next heuristic.\n",
" }\n",
"\n",
" // Fallback to system preference\n",
" return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';\n",
"}\n",
"\n",
"\n",
"function forceTheme(elementId) {\n",
" const estimatorElement = document.querySelector(`#${elementId}`);\n",
" if (estimatorElement === null) {\n",
" console.error(`Element with id ${elementId} not found.`);\n",
" } else {\n",
" const theme = detectTheme(estimatorElement);\n",
" estimatorElement.classList.add(theme);\n",
" }\n",
"}\n",
"\n",
"forceTheme('sk-container-id-2');</script></body>"
],
"text/plain": [
"KMeans(init='random', n_clusters=3, n_init=10)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"kmeans_init = KMeans(n_clusters=3, init='random', n_init=10)\n",
"kmeans_init.fit(iris.data)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(0)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(0)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1))]\n"
]
}
],
"source": [
"print(list(zip(iris.target,kmeans.labels_)))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.7514854021988338\n",
"0.7649861514489815\n",
"0.7302382722834697\n",
"0.5528190123564095\n"
]
}
],
"source": [
"print(metrics.homogeneity_score(iris.target, kmeans_init.labels_))\n",
"print(metrics.completeness_score(iris.target, kmeans_init.labels_))\n",
"print(metrics.adjusted_rand_score(iris.target, kmeans_init.labels_))\n",
"print(metrics.silhouette_score(iris.data, kmeans_init.labels_))"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>#sk-container-id-3 {\n",
" /* Definition of color scheme common for light and dark mode */\n",
" --sklearn-color-text: #000;\n",
" --sklearn-color-text-muted: #666;\n",
" --sklearn-color-line: gray;\n",
" /* Definition of color scheme for unfitted estimators */\n",
" --sklearn-color-unfitted-level-0: #fff5e6;\n",
" --sklearn-color-unfitted-level-1: #f6e4d2;\n",
" --sklearn-color-unfitted-level-2: #ffe0b3;\n",
" --sklearn-color-unfitted-level-3: chocolate;\n",
" /* Definition of color scheme for fitted estimators */\n",
" --sklearn-color-fitted-level-0: #f0f8ff;\n",
" --sklearn-color-fitted-level-1: #d4ebff;\n",
" --sklearn-color-fitted-level-2: #b3dbfd;\n",
" --sklearn-color-fitted-level-3: cornflowerblue;\n",
"}\n",
"\n",
"#sk-container-id-3.light {\n",
" /* Specific color for light theme */\n",
" --sklearn-color-text-on-default-background: black;\n",
" --sklearn-color-background: white;\n",
" --sklearn-color-border-box: black;\n",
" --sklearn-color-icon: #696969;\n",
"}\n",
"\n",
"#sk-container-id-3.dark {\n",
" --sklearn-color-text-on-default-background: white;\n",
" --sklearn-color-background: #111;\n",
" --sklearn-color-border-box: white;\n",
" --sklearn-color-icon: #878787;\n",
"}\n",
"\n",
"#sk-container-id-3 {\n",
" color: var(--sklearn-color-text);\n",
"}\n",
"\n",
"#sk-container-id-3 pre {\n",
" padding: 0;\n",
"}\n",
"\n",
"#sk-container-id-3 input.sk-hidden--visually {\n",
" border: 0;\n",
" clip: rect(1px 1px 1px 1px);\n",
" clip: rect(1px, 1px, 1px, 1px);\n",
" height: 1px;\n",
" margin: -1px;\n",
" overflow: hidden;\n",
" padding: 0;\n",
" position: absolute;\n",
" width: 1px;\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-dashed-wrapped {\n",
" border: 1px dashed var(--sklearn-color-line);\n",
" margin: 0 0.4em 0.5em 0.4em;\n",
" box-sizing: border-box;\n",
" padding-bottom: 0.4em;\n",
" background-color: var(--sklearn-color-background);\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-container {\n",
" /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n",
" but bootstrap.min.css set `[hidden] { display: none !important; }`\n",
" so we also need the `!important` here to be able to override the\n",
" default hidden behavior on the sphinx rendered scikit-learn.org.\n",
" See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n",
" display: inline-block !important;\n",
" position: relative;\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-text-repr-fallback {\n",
" display: none;\n",
"}\n",
"\n",
"div.sk-parallel-item,\n",
"div.sk-serial,\n",
"div.sk-item {\n",
" /* draw centered vertical line to link estimators */\n",
" background-image: linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));\n",
" background-size: 2px 100%;\n",
" background-repeat: no-repeat;\n",
" background-position: center center;\n",
"}\n",
"\n",
"/* Parallel-specific style estimator block */\n",
"\n",
"#sk-container-id-3 div.sk-parallel-item::after {\n",
" content: \"\";\n",
" width: 100%;\n",
" border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n",
" flex-grow: 1;\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-parallel {\n",
" display: flex;\n",
" align-items: stretch;\n",
" justify-content: center;\n",
" background-color: var(--sklearn-color-background);\n",
" position: relative;\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-parallel-item {\n",
" display: flex;\n",
" flex-direction: column;\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-parallel-item:first-child::after {\n",
" align-self: flex-end;\n",
" width: 50%;\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-parallel-item:last-child::after {\n",
" align-self: flex-start;\n",
" width: 50%;\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-parallel-item:only-child::after {\n",
" width: 0;\n",
"}\n",
"\n",
"/* Serial-specific style estimator block */\n",
"\n",
"#sk-container-id-3 div.sk-serial {\n",
" display: flex;\n",
" flex-direction: column;\n",
" align-items: center;\n",
" background-color: var(--sklearn-color-background);\n",
" padding-right: 1em;\n",
" padding-left: 1em;\n",
"}\n",
"\n",
"\n",
"/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer box that is\n",
"clickable and can be expanded/collapsed.\n",
"- Pipeline and ColumnTransformer use this feature and define the default style\n",
"- Estimators will overwrite some part of the style using the `sk-estimator` class\n",
"*/\n",
"\n",
"/* Pipeline and ColumnTransformer style (default) */\n",
"\n",
"#sk-container-id-3 div.sk-toggleable {\n",
" /* Default theme specific background. It is overwritten whether we have a\n",
" specific estimator or a Pipeline/ColumnTransformer */\n",
" background-color: var(--sklearn-color-background);\n",
"}\n",
"\n",
"/* Toggleable label */\n",
"#sk-container-id-3 label.sk-toggleable__label {\n",
" cursor: pointer;\n",
" display: flex;\n",
" width: 100%;\n",
" margin-bottom: 0;\n",
" padding: 0.5em;\n",
" box-sizing: border-box;\n",
" text-align: center;\n",
" align-items: center;\n",
" justify-content: center;\n",
" gap: 0.5em;\n",
"}\n",
"\n",
"#sk-container-id-3 label.sk-toggleable__label .caption {\n",
" font-size: 0.6rem;\n",
" font-weight: lighter;\n",
" color: var(--sklearn-color-text-muted);\n",
"}\n",
"\n",
"#sk-container-id-3 label.sk-toggleable__label-arrow:before {\n",
" /* Arrow on the left of the label */\n",
" content: \"▸\";\n",
" float: left;\n",
" margin-right: 0.25em;\n",
" color: var(--sklearn-color-icon);\n",
"}\n",
"\n",
"#sk-container-id-3 label.sk-toggleable__label-arrow:hover:before {\n",
" color: var(--sklearn-color-text);\n",
"}\n",
"\n",
"/* Toggleable content - dropdown */\n",
"\n",
"#sk-container-id-3 div.sk-toggleable__content {\n",
" display: none;\n",
" text-align: left;\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-toggleable__content.fitted {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-toggleable__content pre {\n",
" margin: 0.2em;\n",
" border-radius: 0.25em;\n",
" color: var(--sklearn-color-text);\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-toggleable__content.fitted pre {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-3 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n",
" /* Expand drop-down */\n",
" display: block;\n",
" width: 100%;\n",
" overflow: visible;\n",
"}\n",
"\n",
"#sk-container-id-3 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n",
" content: \"▾\";\n",
"}\n",
"\n",
"/* Pipeline/ColumnTransformer-specific style */\n",
"\n",
"#sk-container-id-3 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
" color: var(--sklearn-color-text);\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
"\n",
"/* Estimator-specific style */\n",
"\n",
"/* Colorize estimator box */\n",
"#sk-container-id-3 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-label label.sk-toggleable__label,\n",
"#sk-container-id-3 div.sk-label label {\n",
" /* The background is the default theme color */\n",
" color: var(--sklearn-color-text-on-default-background);\n",
"}\n",
"\n",
"/* On hover, darken the color of the background */\n",
"#sk-container-id-3 div.sk-label:hover label.sk-toggleable__label {\n",
" color: var(--sklearn-color-text);\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"/* Label box, darken color on hover, fitted */\n",
"#sk-container-id-3 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n",
" color: var(--sklearn-color-text);\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
"\n",
"/* Estimator label */\n",
"\n",
"#sk-container-id-3 div.sk-label label {\n",
" font-family: monospace;\n",
" font-weight: bold;\n",
" line-height: 1.2em;\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-label-container {\n",
" text-align: center;\n",
"}\n",
"\n",
"/* Estimator-specific */\n",
"#sk-container-id-3 div.sk-estimator {\n",
" font-family: monospace;\n",
" border: 1px dotted var(--sklearn-color-border-box);\n",
" border-radius: 0.25em;\n",
" box-sizing: border-box;\n",
" margin-bottom: 0.5em;\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-estimator.fitted {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
"}\n",
"\n",
"/* on hover */\n",
"#sk-container-id-3 div.sk-estimator:hover {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-estimator.fitted:hover {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
"\n",
"/* Specification for estimator info (e.g. \"i\" and \"?\") */\n",
"\n",
"/* Common style for \"i\" and \"?\" */\n",
"\n",
".sk-estimator-doc-link,\n",
"a:link.sk-estimator-doc-link,\n",
"a:visited.sk-estimator-doc-link {\n",
" float: right;\n",
" font-size: smaller;\n",
" line-height: 1em;\n",
" font-family: monospace;\n",
" background-color: var(--sklearn-color-unfitted-level-0);\n",
" border-radius: 1em;\n",
" height: 1em;\n",
" width: 1em;\n",
" text-decoration: none !important;\n",
" margin-left: 0.5em;\n",
" text-align: center;\n",
" /* unfitted */\n",
" border: var(--sklearn-color-unfitted-level-3) 1pt solid;\n",
" color: var(--sklearn-color-unfitted-level-3);\n",
"}\n",
"\n",
".sk-estimator-doc-link.fitted,\n",
"a:link.sk-estimator-doc-link.fitted,\n",
"a:visited.sk-estimator-doc-link.fitted {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
" border: var(--sklearn-color-fitted-level-3) 1pt solid;\n",
" color: var(--sklearn-color-fitted-level-3);\n",
"}\n",
"\n",
"/* On hover */\n",
"div.sk-estimator:hover .sk-estimator-doc-link:hover,\n",
".sk-estimator-doc-link:hover,\n",
"div.sk-label-container:hover .sk-estimator-doc-link:hover,\n",
".sk-estimator-doc-link:hover {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-3);\n",
" border: var(--sklearn-color-fitted-level-0) 1pt solid;\n",
" color: var(--sklearn-color-unfitted-level-0);\n",
" text-decoration: none;\n",
"}\n",
"\n",
"div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,\n",
".sk-estimator-doc-link.fitted:hover,\n",
"div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,\n",
".sk-estimator-doc-link.fitted:hover {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-3);\n",
" border: var(--sklearn-color-fitted-level-0) 1pt solid;\n",
" color: var(--sklearn-color-fitted-level-0);\n",
" text-decoration: none;\n",
"}\n",
"\n",
"/* Span, style for the box shown on hovering the info icon */\n",
".sk-estimator-doc-link span {\n",
" display: none;\n",
" z-index: 9999;\n",
" position: relative;\n",
" font-weight: normal;\n",
" right: .2ex;\n",
" padding: .5ex;\n",
" margin: .5ex;\n",
" width: min-content;\n",
" min-width: 20ex;\n",
" max-width: 50ex;\n",
" color: var(--sklearn-color-text);\n",
" box-shadow: 2pt 2pt 4pt #999;\n",
" /* unfitted */\n",
" background: var(--sklearn-color-unfitted-level-0);\n",
" border: .5pt solid var(--sklearn-color-unfitted-level-3);\n",
"}\n",
"\n",
".sk-estimator-doc-link.fitted span {\n",
" /* fitted */\n",
" background: var(--sklearn-color-fitted-level-0);\n",
" border: var(--sklearn-color-fitted-level-3);\n",
"}\n",
"\n",
".sk-estimator-doc-link:hover span {\n",
" display: block;\n",
"}\n",
"\n",
"/* \"?\"-specific style due to the `<a>` HTML tag */\n",
"\n",
"#sk-container-id-3 a.estimator_doc_link {\n",
" float: right;\n",
" font-size: 1rem;\n",
" line-height: 1em;\n",
" font-family: monospace;\n",
" background-color: var(--sklearn-color-unfitted-level-0);\n",
" border-radius: 1rem;\n",
" height: 1rem;\n",
" width: 1rem;\n",
" text-decoration: none;\n",
" /* unfitted */\n",
" color: var(--sklearn-color-unfitted-level-1);\n",
" border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
"}\n",
"\n",
"#sk-container-id-3 a.estimator_doc_link.fitted {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
" border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
" color: var(--sklearn-color-fitted-level-1);\n",
"}\n",
"\n",
"/* On hover */\n",
"#sk-container-id-3 a.estimator_doc_link:hover {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-3);\n",
" color: var(--sklearn-color-background);\n",
" text-decoration: none;\n",
"}\n",
"\n",
"#sk-container-id-3 a.estimator_doc_link.fitted:hover {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-3);\n",
"}\n",
"\n",
".estimator-table {\n",
" font-family: monospace;\n",
"}\n",
"\n",
".estimator-table summary {\n",
" padding: .5rem;\n",
" cursor: pointer;\n",
"}\n",
"\n",
".estimator-table summary::marker {\n",
" font-size: 0.7rem;\n",
"}\n",
"\n",
".estimator-table details[open] {\n",
" padding-left: 0.1rem;\n",
" padding-right: 0.1rem;\n",
" padding-bottom: 0.3rem;\n",
"}\n",
"\n",
".estimator-table .parameters-table {\n",
" margin-left: auto !important;\n",
" margin-right: auto !important;\n",
" margin-top: 0;\n",
"}\n",
"\n",
".estimator-table .parameters-table tr:nth-child(odd) {\n",
" background-color: #fff;\n",
"}\n",
"\n",
".estimator-table .parameters-table tr:nth-child(even) {\n",
" background-color: #f6f6f6;\n",
"}\n",
"\n",
".estimator-table .parameters-table tr:hover {\n",
" background-color: #e0e0e0;\n",
"}\n",
"\n",
".estimator-table table td {\n",
" border: 1px solid rgba(106, 105, 104, 0.232);\n",
"}\n",
"\n",
"/*\n",
" `table td`is set in notebook with right text-align.\n",
" We need to overwrite it.\n",
"*/\n",
".estimator-table table td.param {\n",
" text-align: left;\n",
" position: relative;\n",
" padding: 0;\n",
"}\n",
"\n",
".user-set td {\n",
" color:rgb(255, 94, 0);\n",
" text-align: left !important;\n",
"}\n",
"\n",
".user-set td.value {\n",
" color:rgb(255, 94, 0);\n",
" background-color: transparent;\n",
"}\n",
"\n",
".default td {\n",
" color: black;\n",
" text-align: left !important;\n",
"}\n",
"\n",
".user-set td i,\n",
".default td i {\n",
" color: black;\n",
"}\n",
"\n",
"/*\n",
" Styles for parameter documentation links\n",
" We need styling for visited so jupyter doesn't overwrite it\n",
"*/\n",
"a.param-doc-link,\n",
"a.param-doc-link:link,\n",
"a.param-doc-link:visited {\n",
" text-decoration: underline dashed;\n",
" text-underline-offset: .3em;\n",
" color: inherit;\n",
" display: block;\n",
" padding: .5em;\n",
"}\n",
"\n",
"/* \"hack\" to make the entire area of the cell containing the link clickable */\n",
"a.param-doc-link::before {\n",
" position: absolute;\n",
" content: \"\";\n",
" inset: 0;\n",
"}\n",
"\n",
".param-doc-description {\n",
" display: none;\n",
" position: absolute;\n",
" z-index: 9999;\n",
" left: 0;\n",
" padding: .5ex;\n",
" margin-left: 1.5em;\n",
" color: var(--sklearn-color-text);\n",
" box-shadow: .3em .3em .4em #999;\n",
" width: max-content;\n",
" text-align: left;\n",
" max-height: 10em;\n",
" overflow-y: auto;\n",
"\n",
" /* unfitted */\n",
" background: var(--sklearn-color-unfitted-level-0);\n",
" border: thin solid var(--sklearn-color-unfitted-level-3);\n",
"}\n",
"\n",
"/* Fitted state for parameter tooltips */\n",
".fitted .param-doc-description {\n",
" /* fitted */\n",
" background: var(--sklearn-color-fitted-level-0);\n",
" border: thin solid var(--sklearn-color-fitted-level-3);\n",
"}\n",
"\n",
".param-doc-link:hover .param-doc-description {\n",
" display: block;\n",
"}\n",
"\n",
".copy-paste-icon {\n",
" background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0NDggNTEyIj48IS0tIUZvbnQgQXdlc29tZSBGcmVlIDYuNy4yIGJ5IEBmb250YXdlc29tZSAtIGh0dHBzOi8vZm9udGF3ZXNvbWUuY29tIExpY2Vuc2UgLSBodHRwczovL2ZvbnRhd2Vzb21lLmNvbS9saWNlbnNlL2ZyZWUgQ29weXJpZ2h0IDIwMjUgRm9udGljb25zLCBJbmMuLS0+PHBhdGggZD0iTTIwOCAwTDMzMi4xIDBjMTIuNyAwIDI0LjkgNS4xIDMzLjkgMTQuMWw2Ny45IDY3LjljOSA5IDE0LjEgMjEuMiAxNC4xIDMzLjlMNDQ4IDMzNmMwIDI2LjUtMjEuNSA0OC00OCA0OGwtMTkyIDBjLTI2LjUgMC00OC0yMS41LTQ4LTQ4bDAtMjg4YzAtMjYuNSAyMS41LTQ4IDQ4LTQ4ek00OCAxMjhsODAgMCAwIDY0LTY0IDAgMCAyNTYgMTkyIDAgMC0zMiA2NCAwIDAgNDhjMCAyNi41LTIxLjUgNDgtNDggNDhMNDggNTEyYy0yNi41IDAtNDgtMjEuNS00OC00OEwwIDE3NmMwLTI2LjUgMjEuNS00OCA0OC00OHoiLz48L3N2Zz4=);\n",
" background-repeat: no-repeat;\n",
" background-size: 14px 14px;\n",
" background-position: 0;\n",
" display: inline-block;\n",
" width: 14px;\n",
" height: 14px;\n",
" cursor: pointer;\n",
"}\n",
"</style><body><div id=\"sk-container-id-3\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>KMeans(n_clusters=3)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-3\" type=\"checkbox\" checked><label for=\"sk-estimator-id-3\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow\"><div><div>KMeans</div></div><div><a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html\">?<span>Documentation for KMeans</span></a><span class=\"sk-estimator-doc-link fitted\">i<span>Fitted</span></span></div></label><div class=\"sk-toggleable__content fitted\" data-param-prefix=\"\">\n",
" <div class=\"estimator-table\">\n",
" <details>\n",
" <summary>Parameters</summary>\n",
" <table class=\"parameters-table\">\n",
" <tbody>\n",
" \n",
" <tr class=\"user-set\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('n_clusters',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=n_clusters,-int%2C%20default%3D8\">\n",
" n_clusters\n",
" <span class=\"param-doc-description\">n_clusters: int, default=8<br><br>The number of clusters to form as well as the number of<br>centroids to generate.<br><br>For an example of how to choose an optimal value for `n_clusters` refer to<br>:ref:`sphx_glr_auto_examples_cluster_plot_kmeans_silhouette_analysis.py`.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">3</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('init',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=init,-%7B%27k-means%2B%2B%27%2C%20%27random%27%7D%2C%20callable%20or%20array-like%20of%20shape%20%20%20%20%20%20%20%20%20%20%20%20%20%28n_clusters%2C%20n_features%29%2C%20default%3D%27k-means%2B%2B%27\">\n",
" init\n",
" <span class=\"param-doc-description\">init: {'k-means++', 'random'}, callable or array-like of shape (n_clusters, n_features), default='k-means++'<br><br>Method for initialization:<br><br>* 'k-means++' : selects initial cluster centroids using sampling based on an empirical probability distribution of the points' contribution to the overall inertia. This technique speeds up convergence. The algorithm implemented is \"greedy k-means++\". It differs from the vanilla k-means++ by making several trials at each sampling step and choosing the best centroid among them.<br><br>* 'random': choose `n_clusters` observations (rows) at random from data for the initial centroids.<br><br>* If an array is passed, it should be of shape (n_clusters, n_features) and gives the initial centers.<br><br>* If a callable is passed, it should take arguments X, n_clusters and a random state and return an initialization.<br><br>For an example of how to use the different `init` strategies, see<br>:ref:`sphx_glr_auto_examples_cluster_plot_kmeans_digits.py`.<br><br>For an evaluation of the impact of initialization, see the example<br>:ref:`sphx_glr_auto_examples_cluster_plot_kmeans_stability_low_dim_dense.py`.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">&#x27;k-means++&#x27;</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('n_init',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=n_init,-%27auto%27%20or%20int%2C%20default%3D%27auto%27\">\n",
" n_init\n",
" <span class=\"param-doc-description\">n_init: 'auto' or int, default='auto'<br><br>Number of times the k-means algorithm is run with different centroid<br>seeds. The final results is the best output of `n_init` consecutive runs<br>in terms of inertia. Several runs are recommended for sparse<br>high-dimensional problems (see :ref:`kmeans_sparse_high_dim`).<br><br>When `n_init='auto'`, the number of runs depends on the value of init:<br>10 if using `init='random'` or `init` is a callable;<br>1 if using `init='k-means++'` or `init` is an array-like.<br><br>.. versionadded:: 1.2<br> Added 'auto' option for `n_init`.<br><br>.. versionchanged:: 1.4<br> Default value for `n_init` changed to `'auto'`.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">&#x27;auto&#x27;</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('max_iter',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=max_iter,-int%2C%20default%3D300\">\n",
" max_iter\n",
" <span class=\"param-doc-description\">max_iter: int, default=300<br><br>Maximum number of iterations of the k-means algorithm for a<br>single run.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">300</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('tol',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=tol,-float%2C%20default%3D1e-4\">\n",
" tol\n",
" <span class=\"param-doc-description\">tol: float, default=1e-4<br><br>Relative tolerance with regards to Frobenius norm of the difference<br>in the cluster centers of two consecutive iterations to declare<br>convergence.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">0.0001</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('verbose',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=verbose,-int%2C%20default%3D0\">\n",
" verbose\n",
" <span class=\"param-doc-description\">verbose: int, default=0<br><br>Verbosity mode.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">0</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('random_state',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=random_state,-int%2C%20RandomState%20instance%20or%20None%2C%20default%3DNone\">\n",
" random_state\n",
" <span class=\"param-doc-description\">random_state: int, RandomState instance or None, default=None<br><br>Determines random number generation for centroid initialization. Use<br>an int to make the randomness deterministic.<br>See :term:`Glossary <random_state>`.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">None</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('copy_x',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=copy_x,-bool%2C%20default%3DTrue\">\n",
" copy_x\n",
" <span class=\"param-doc-description\">copy_x: bool, default=True<br><br>When pre-computing distances it is more numerically accurate to center<br>the data first. If copy_x is True (default), then the original data is<br>not modified. If False, the original data is modified, and put back<br>before the function returns, but small numerical differences may be<br>introduced by subtracting and then adding the data mean. Note that if<br>the original data is not C-contiguous, a copy will be made even if<br>copy_x is False. If the original data is sparse, but not in CSR format,<br>a copy will be made even if copy_x is False.</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">True</td>\n",
" </tr>\n",
" \n",
"\n",
" <tr class=\"default\">\n",
" <td><i class=\"copy-paste-icon\"\n",
" onclick=\"copyToClipboard('algorithm',\n",
" this.parentElement.nextElementSibling)\"\n",
" ></i></td>\n",
" <td class=\"param\">\n",
" <a class=\"param-doc-link\"\n",
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.cluster.KMeans.html#:~:text=algorithm,-%7B%22lloyd%22%2C%20%22elkan%22%7D%2C%20default%3D%22lloyd%22\">\n",
" algorithm\n",
" <span class=\"param-doc-description\">algorithm: {\"lloyd\", \"elkan\"}, default=\"lloyd\"<br><br>K-means algorithm to use. The classical EM-style algorithm is `\"lloyd\"`.<br>The `\"elkan\"` variation can be more efficient on some datasets with<br>well-defined clusters, by using the triangle inequality. However it's<br>more memory intensive due to the allocation of an extra array of shape<br>`(n_samples, n_clusters)`.<br><br>.. versionchanged:: 0.18<br> Added Elkan algorithm<br><br>.. versionchanged:: 1.1<br> Renamed \"full\" to \"lloyd\", and deprecated \"auto\" and \"full\".<br> Changed \"auto\" to use \"lloyd\" instead of \"elkan\".</span>\n",
" </a>\n",
" </td>\n",
" <td class=\"value\">&#x27;lloyd&#x27;</td>\n",
" </tr>\n",
" \n",
" </tbody>\n",
" </table>\n",
" </details>\n",
" </div>\n",
" </div></div></div></div></div><script>function copyToClipboard(text, element) {\n",
" // Get the parameter prefix from the closest toggleable content\n",
" const toggleableContent = element.closest('.sk-toggleable__content');\n",
" const paramPrefix = toggleableContent ? toggleableContent.dataset.paramPrefix : '';\n",
" const fullParamName = paramPrefix ? `${paramPrefix}${text}` : text;\n",
"\n",
" const originalStyle = element.style;\n",
" const computedStyle = window.getComputedStyle(element);\n",
" const originalWidth = computedStyle.width;\n",
" const originalHTML = element.innerHTML.replace('Copied!', '');\n",
"\n",
" navigator.clipboard.writeText(fullParamName)\n",
" .then(() => {\n",
" element.style.width = originalWidth;\n",
" element.style.color = 'green';\n",
" element.innerHTML = \"Copied!\";\n",
"\n",
" setTimeout(() => {\n",
" element.innerHTML = originalHTML;\n",
" element.style = originalStyle;\n",
" }, 2000);\n",
" })\n",
" .catch(err => {\n",
" console.error('Failed to copy:', err);\n",
" element.style.color = 'red';\n",
" element.innerHTML = \"Failed!\";\n",
" setTimeout(() => {\n",
" element.innerHTML = originalHTML;\n",
" element.style = originalStyle;\n",
" }, 2000);\n",
" });\n",
" return false;\n",
"}\n",
"\n",
"document.querySelectorAll('.copy-paste-icon').forEach(function(element) {\n",
" const toggleableContent = element.closest('.sk-toggleable__content');\n",
" const paramPrefix = toggleableContent ? toggleableContent.dataset.paramPrefix : '';\n",
" const paramName = element.parentElement.nextElementSibling\n",
" .textContent.trim().split(' ')[0];\n",
" const fullParamName = paramPrefix ? `${paramPrefix}${paramName}` : paramName;\n",
"\n",
" element.setAttribute('title', fullParamName);\n",
"});\n",
"\n",
"\n",
"/**\n",
" * Adapted from Skrub\n",
" * https://github.com/skrub-data/skrub/blob/403466d1d5d4dc76a7ef569b3f8228db59a31dc3/skrub/_reporting/_data/templates/report.js#L789\n",
" * @returns \"light\" or \"dark\"\n",
" */\n",
"function detectTheme(element) {\n",
" const body = document.querySelector('body');\n",
"\n",
" // Check VSCode theme\n",
" const themeKindAttr = body.getAttribute('data-vscode-theme-kind');\n",
" const themeNameAttr = body.getAttribute('data-vscode-theme-name');\n",
"\n",
" if (themeKindAttr && themeNameAttr) {\n",
" const themeKind = themeKindAttr.toLowerCase();\n",
" const themeName = themeNameAttr.toLowerCase();\n",
"\n",
" if (themeKind.includes(\"dark\") || themeName.includes(\"dark\")) {\n",
" return \"dark\";\n",
" }\n",
" if (themeKind.includes(\"light\") || themeName.includes(\"light\")) {\n",
" return \"light\";\n",
" }\n",
" }\n",
"\n",
" // Check Jupyter theme\n",
" if (body.getAttribute('data-jp-theme-light') === 'false') {\n",
" return 'dark';\n",
" } else if (body.getAttribute('data-jp-theme-light') === 'true') {\n",
" return 'light';\n",
" }\n",
"\n",
" // Guess based on a parent element's color\n",
" const color = window.getComputedStyle(element.parentNode, null).getPropertyValue('color');\n",
" const match = color.match(/^rgb\\s*\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)\\s*$/i);\n",
" if (match) {\n",
" const [r, g, b] = [\n",
" parseFloat(match[1]),\n",
" parseFloat(match[2]),\n",
" parseFloat(match[3])\n",
" ];\n",
"\n",
" // https://en.wikipedia.org/wiki/HSL_and_HSV#Lightness\n",
" const luma = 0.299 * r + 0.587 * g + 0.114 * b;\n",
"\n",
" if (luma > 180) {\n",
" // If the text is very bright we have a dark theme\n",
" return 'dark';\n",
" }\n",
" if (luma < 75) {\n",
" // If the text is very dark we have a light theme\n",
" return 'light';\n",
" }\n",
" // Otherwise fall back to the next heuristic.\n",
" }\n",
"\n",
" // Fallback to system preference\n",
" return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';\n",
"}\n",
"\n",
"\n",
"function forceTheme(elementId) {\n",
" const estimatorElement = document.querySelector(`#${elementId}`);\n",
" if (estimatorElement === null) {\n",
" console.error(`Element with id ${elementId} not found.`);\n",
" } else {\n",
" const theme = detectTheme(estimatorElement);\n",
" estimatorElement.classList.add(theme);\n",
" }\n",
"}\n",
"\n",
"forceTheme('sk-container-id-3');</script></body>"
],
"text/plain": [
"KMeans(n_clusters=3)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"kmeans2 = KMeans(n_clusters=3)\n",
"kmeans2.fit(iris.data)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(0), np.int32(2)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(0)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(0)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(1), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(0)), (np.int64(2), np.int32(1))]\n"
]
}
],
"source": [
"print(list(zip(iris.target,kmeans.labels_)))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.7364192881252849\n",
"0.7474865805095324\n",
"0.7163421126838476\n",
"0.551191604619592\n"
]
}
],
"source": [
"print(metrics.homogeneity_score(iris.target, kmeans2.labels_))\n",
"print(metrics.completeness_score(iris.target, kmeans2.labels_))\n",
"print(metrics.adjusted_rand_score(iris.target, kmeans2.labels_))\n",
"print(metrics.silhouette_score(iris.data, kmeans2.labels_))"
]
},
{
"cell_type": "code",
"execution_count": null,
"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.13.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}