{ "cells": [ { "cell_type": "markdown", "id": "26ec93c9-8072-497a-bd5d-ed54e5e2fd6d", "metadata": {}, "source": [ "# 教師なし学習\n", "\n", "## クラスタリング\n", "### K-means" ] }, { "cell_type": "markdown", "id": "acbaddaf-1427-439f-899d-80d32c0c6c01", "metadata": {}, "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": {}, "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": {}, "outputs": [ { "data": { "text/html": [ "
\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", "