{ "cells": [ { "cell_type": "markdown", "id": "26ec93c9-8072-497a-bd5d-ed54e5e2fd6d", "metadata": { "id": "26ec93c9-8072-497a-bd5d-ed54e5e2fd6d" }, "source": [ "# 教師なし学習\n", "\n", "## クラスタリング\n", "### K-means" ] }, { "cell_type": "markdown", "id": "acbaddaf-1427-439f-899d-80d32c0c6c01", "metadata": { "id": "acbaddaf-1427-439f-899d-80d32c0c6c01" }, "source": [ "irisの特徴量`petal_length`と`petal_width`を用いて、クラスタリング手法の一つであるK-meansを使ってクラスタリングします\n", "\n", "K-meansではクラスタの数を指定する必要があります。ここでは3つのクラスタを生成します。" ] }, { "cell_type": "code", "execution_count": 1, "id": "3e536482-3d6d-42a4-955a-3690dc3959dd", "metadata": { "id": "3e536482-3d6d-42a4-955a-3690dc3959dd" }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import pandas as pd\n", "from sklearn import datasets\n", "from sklearn.cluster import KMeans\n", "from sklearn.decomposition import PCA\n", "from sklearn.preprocessing import StandardScaler\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 2, "id": "08abe37e-eef7-4fc8-aa65-086a94132258", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 206 }, "id": "08abe37e-eef7-4fc8-aa65-086a94132258", "outputId": "467767ff-18b1-469a-c49f-67ee23373fd8" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) \\\n", "0 5.1 3.5 1.4 0.2 \n", "1 4.9 3.0 1.4 0.2 \n", "2 4.7 3.2 1.3 0.2 \n", "3 4.6 3.1 1.5 0.2 \n", "4 5.0 3.6 1.4 0.2 \n", "\n", " species \n", "0 0 \n", "1 0 \n", "2 0 \n", "3 0 \n", "4 0 " ], "text/html": [ "\n", "
\n", " | sepal length (cm) | \n", "sepal width (cm) | \n", "petal length (cm) | \n", "petal width (cm) | \n", "species | \n", "
---|---|---|---|---|---|
0 | \n", "5.1 | \n", "3.5 | \n", "1.4 | \n", "0.2 | \n", "0 | \n", "
1 | \n", "4.9 | \n", "3.0 | \n", "1.4 | \n", "0.2 | \n", "0 | \n", "
2 | \n", "4.7 | \n", "3.2 | \n", "1.3 | \n", "0.2 | \n", "0 | \n", "
3 | \n", "4.6 | \n", "3.1 | \n", "1.5 | \n", "0.2 | \n", "0 | \n", "
4 | \n", "5.0 | \n", "3.6 | \n", "1.4 | \n", "0.2 | \n", "0 | \n", "