{"id":2147,"date":"2019-09-03T21:26:19","date_gmt":"2019-09-03T12:26:19","guid":{"rendered":"https:\/\/tnishimaki.com\/?p=2147"},"modified":"2025-06-12T18:53:45","modified_gmt":"2025-06-12T09:53:45","slug":"python%e3%81%ab%e3%82%88%e3%82%8b%e9%9d%9e%e9%9a%8e%e5%b1%a4%e5%9e%8b%e3%82%af%e3%83%a9%e3%82%b9%e3%82%bf%e3%83%aa%e3%83%b3%e3%82%b0k-means%e6%b3%95%e3%81%ae%e5%ae%9f%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/analysis-navi.com\/?p=2147","title":{"rendered":"Python\u306b\u3088\u308b\u975e\u968e\u5c64\u578b\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0(k-means\u6cd5)"},"content":{"rendered":"\n<p>\u975e\u968e\u5c64\u578b\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u306e\u4ee3\u8868\u7684\u624b\u6cd5\u3067\u3042\u308b<strong>\u300ck-means\u6cd5\u300d<\/strong>\u3092Python\u3067\u5b9f\u884c\u3057\u3066\u307f\u307e\u3059\u3002<br>k-means\u6cd5\u306e\u7406\u8ad6\u306b\u3064\u3044\u3066\u306f<a href=\"https:\/\/analysis-navi.com\/?p=618\">\u3053\u3061\u3089<\/a>\u306e\u8a18\u4e8b\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/analysis-navi.com\/?p=618\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u8fbc\u307f\u3001k-means\u6cd5\u306e\u5b9f\u884c<\/h2>\n\n\n\n<p>\u4eca\u56de\u306f\u7406\u8ad6\u7de8\u3067\u4f7f\u7528\u3057\u305f\u30c7\u30fc\u30bf\u3068\u307b\u307c\u540c\u69d8\u306e\u3001\u4ee5\u4e0b\u306e\u30c7\u30fc\u30bf\u3092\u7528\u3044\u307e\u3059\u3002<br><img decoding=\"async\" src=\"https:\/\/analysis-navi.com\/wp-content\/uploads\/2019\/09\/34b776bbc86711e1631e06031f9bce89.png\" alt=\"\" width=\"271\" height=\"255\" class=\"alignnone size-full wp-image-2229\" srcset=\"https:\/\/analysis-navi.com\/wp-content\/uploads\/2019\/09\/34b776bbc86711e1631e06031f9bce89.png 542w, https:\/\/analysis-navi.com\/wp-content\/uploads\/2019\/09\/34b776bbc86711e1631e06031f9bce89-425x400.png 425w, https:\/\/analysis-navi.com\/wp-content\/uploads\/2019\/09\/34b776bbc86711e1631e06031f9bce89-300x282.png 300w\" sizes=\"(max-width: 271px) 100vw, 271px\" \/><\/p>\n\n\n\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306b\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u307f\u3001sklearn.clustar\u5185\u306eKMeans\u30e1\u30bd\u30c3\u30c9\u3092\u7528\u3044\u308c\u3070\u975e\u5e38\u306b\u7c21\u5358\u306b\u5b9f\u884c\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import pandas as pd\ndf = pd.read_csv(\"test_score.csv\",index_col=0)\n\nfrom sklearn.cluster import KMeans\nvec = KMeans(n_clusters=3)\n\nvec.fit(df)\nprint(vec.labels_)<\/pre>\n\n\n\n<p>KMeans\u30e1\u30bd\u30c3\u30c9\u306e\u5f15\u6570\u300cn_clusters\u300d\u306f\u30af\u30e9\u30b9\u6570\u3067\u3059\u3002<br><strong><span style=\"background-color: #87cefa;\">\u30af\u30e9\u30b9\u6570\u306f\u4eba\u9593\u304c\u6c7a\u3081\u306a\u3051\u308c\u3070\u3044\u3051\u307e\u305b\u3093\u3002<\/span><\/strong><br>\u4eca\u56de\u306f\u7406\u8ad6\u7de8\u3068\u306e\u7d50\u679c\u6bd4\u8f03\u306e\u305f\u3081\u30013\u306b\u3057\u3066\u3044\u307e\u3059\u3002<br>\u7d50\u679c\u306f\u3002\u3002\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[1 1 2 2 0 1 2 0 0 2]<\/pre>\n\n\n\n<p>\u3082\u3046\u51fa\u3066\u304d\u307e\u3057\u305f\u306d\u3002<br>\u300cvec.fit(\u30c7\u30fc\u30bf)\u300d\u3067K-means\u304c\u5b9f\u884c\u3055\u308c\u3001\u300cvec.labels_\u300d\u306b\u4e88\u6e2c\u30af\u30e9\u30b9\u30bf\u30fc\u756a\u53f7\u304c\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002<br>\u3061\u306a\u307f\u306b\u300cvec.fit_predict(\u30c7\u30fc\u30bf)\u300d\u3068\u3059\u308b\u3068\u3001\u4e00\u6c17\u306b\u30af\u30e9\u30b9\u306e\u4e88\u6e2c\u307e\u3067\u5b8c\u4e86\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u7d50\u679c\u306b\u306f\u8aad\u307f\u8fbc\u3093\u3060\u30c7\u30fc\u30bf\u306e\u4e0a\u304b\u3089\u9806\u756a\u306b\u30af\u30e9\u30b9\u30bf\u30fc\u756a\u53f7\u304c\u632f\u3089\u308c\u3066\u3044\u307e\u3059\u304c\u3001\u3053\u308c\u3067\u306f\u4f55\u304c\u3069\u306e\u30af\u30e9\u30b9\u30bf\u30fc\u306b\u306a\u306e\u304b\u826f\u304f\u5206\u304b\u308a\u307e\u305b\u3093\u3002<br>\u3082\u3046\u5c11\u3057\u5206\u304b\u308a\u3084\u3059\u304f\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<div class=\"concept-box5\"><span class=\"box-title\">\u88dc\u8db3<\/span>\n<p>\u5b9f\u884c\u306e\u5ea6\u306b\u30af\u30e9\u30b9\u30bf\u30fc\u756a\u53f7\u306f\u5909\u308f\u308a\u307e\u3059\u3002<br>\u307e\u305f\u3001\u305d\u308c\u3060\u3051\u3067\u306a\u304f\u3001\u7a00\u306b\u7d50\u679c\u3082\u7570\u306a\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002<br>K-means\u6cd5\u306f\u521d\u671f\u5024\u3092\u30e9\u30f3\u30c0\u30e0\u306b\u9078\u3076\u306e\u3067\u305d\u306e\u5f71\u97ff\u3067\u3059\u304c\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f<strong>\u300cK-means++\u300d<\/strong>\u3068\u3044\u3046\u3001\u901a\u5e38\u306eK-means\u6cd5\u3088\u308a\u30d1\u30ef\u30fc\u30a2\u30c3\u30d7\u3057\u305f\u624b\u6cd5\u3067\u8a08\u7b97\u304c\u884c\u308f\u308c\u3066\u304a\u308a\u3001\u5b9f\u884c\u306e\u5ea6\u306b\u7d50\u679c\u304c\u5909\u308f\u308b\u554f\u984c\u306f\u305d\u3053\u307e\u3067\u8d77\u3053\u308a\u307e\u305b\u3093\u3002<\/p>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u30af\u30e9\u30b9\u30bf\u30fc\u3054\u3068\u306e\u7d50\u679c\u96c6\u8a08<\/h2>\n\n\n\n<p>\u307e\u305a\u306f\u4e0a\u8a18\u306e\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u7d50\u679c\u3092\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u306b\u683c\u7d0d\u3057\u307e\u3059\u3002<br><strong><span style=\"background-color: #87cefa;\">\u30c7\u30fc\u30bf\u5206\u6790\u3092\u884c\u3046\u6642\u306f\u3001\u3069\u3093\u306a\u6642\u3067\u3082\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u306b\u5024\u3092\u7a81\u3063\u8fbc\u3093\u3067\u304b\u3089\u51e6\u7406\u3059\u308b\u306e\u304c\u7d50\u679c\u7684\u306b\u697d<\/span><\/strong>\u304b\u306a\u3068\u3044\u3046\u6c17\u304c\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4eca\u56de\u306e\u5834\u5408\u306f\u975e\u5e38\u306b\u7c21\u5358\u3067\u3001<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df[\"class\"]=vec.fit_predict(df)<\/pre>\n\n\n\n<p>\u3068\u3059\u308b\u3060\u3051\u3067\u3059\u3002\u3059\u308b\u3068\u3001\u8aad\u307f\u8fbc\u3093\u3060\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u306b\u884c\u304c\u8ffd\u52a0\u3055\u308c\u3001<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    \u7b97\u6570  \u56fd\u8a9e  class\nA   80  30      1\nB   87  32      1\nC   70  60      2\nD   52  50      2\nE   22  75      0\nF  100  40      1\nG   75  82      2\nH   25  91      0\nI   40  88      0\nJ   80  65      2<\/pre>\n\n\n\n<p>\u3053\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002\u3053\u3046\u306a\u308c\u3070\u30af\u30e9\u30b9\u30bf\u3054\u3068\u306e\u50be\u5411\u306a\u3069\u304c\u3059\u3050\u306b\u8a08\u7b97\u3067\u304d\u307e\u3059\u3002<br>\u4f8b\u3048\u3070\u3001\u30af\u30e9\u30b9\u30bf\u3054\u3068\u306e\u79d1\u76ee\u5225\u5e73\u5747\u70b9\u3092\u51fa\u3057\u305f\u3051\u308c\u3070\u3001groupby\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u3066<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(df.groupby(\"class\").mean())<\/pre>\n\n\n\n<p>\u3068\u3059\u308c\u3070\u3001<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">          \u7b97\u6570         \u56fd\u8a9e\nclass                  \n0      29.00  84.666667\n1      89.00  34.000000\n2      69.25  64.250000<\/pre>\n\n\n\n<p>\u3068\u96c6\u8a08\u3067\u304d\u307e\u3059\u3002<br>\u7d50\u679c\u3092\u898b\u308b\u3068\u3001<strong><span style=\"background-color: #87cefa;\">\u300c\u30af\u30e9\u30b90\u300d\u306f\u56fd\u8a9e\u304c\u5f97\u610f\u306a\u30b0\u30eb\u30fc\u30d7\u3001\u300c\u30af\u30e9\u30b91\u300d\u306f\u7b97\u6570\u304c\u5f97\u610f\u306a\u30b0\u30eb\u30fc\u30d7\u3001\u300c\u30af\u30e9\u30b92\u300d\u306f\u30d0\u30e9\u30f3\u30b9\u304c\u826f\u3044\u30b0\u30eb\u30fc\u30d7<\/span><\/strong>\u3001\u3068\u8003\u3048\u3089\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7d50\u679c\u306e\u30b0\u30e9\u30d5\u5316<\/h2>\n\n\n\n<p>\u30b0\u30e9\u30d5\u5316\u3082\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#\u30b0\u30e9\u30d5\u5316\nimport matplotlib.pyplot as plt\n\n#\u30b0\u30e9\u30d5\u306e\u30c7\u30b6\u30a4\u30f3\u6307\u5b9a\nplt.figure(figsize=(6, 6))\nplt.rcParams[\"font.size\"] = 15\nplt.xlim(0,100)\nplt.ylim(0,100)\n\n#\u30d7\u30ed\u30c3\u30c8\nX = df[\"\u7b97\u6570\"]\nY = df[\"\u56fd\u8a9e\"]\nplt.scatter(X,Y,color=colors)\n\n#\u70b9\u306b\u8272\u3092\u4ed8\u4e0e\ncolor_codes = {0:\"red\", 1:\"blue\", 2:\"green\"}\ncolors = [color_codes[x] for x in vec.labels_]\n\n#\u70b9\u306b\u540d\u79f0\u3092\u4ed8\u4e0e\nfor i,(x_name,y_name) in enumerate(zip(X,Y)):\n    plt.annotate(df.index[i],(x_name,y_name))\n    \nplt.show()<\/pre>\n\n\n\n<p>\u30c7\u30b6\u30a4\u30f3\u306f\u304a\u597d\u307f\u3067\u3002<br>\u7d50\u679c\u306f\u30fb\u30fb\u30fb<br><img decoding=\"async\" src=\"https:\/\/analysis-navi.com\/wp-content\/uploads\/2019\/09\/2eb8a17e2a0b164988c048c9c0e42987.png\" alt=\"\" width=\"411\" height=\"377\" class=\"alignnone size-full wp-image-2230\" srcset=\"https:\/\/analysis-navi.com\/wp-content\/uploads\/2019\/09\/2eb8a17e2a0b164988c048c9c0e42987.png 822w, https:\/\/analysis-navi.com\/wp-content\/uploads\/2019\/09\/2eb8a17e2a0b164988c048c9c0e42987-436x400.png 436w, https:\/\/analysis-navi.com\/wp-content\/uploads\/2019\/09\/2eb8a17e2a0b164988c048c9c0e42987-300x275.png 300w, https:\/\/analysis-navi.com\/wp-content\/uploads\/2019\/09\/2eb8a17e2a0b164988c048c9c0e42987-768x704.png 768w\" sizes=\"(max-width: 411px) 100vw, 411px\" \/><\/p>\n\n\n\n<p>\u304d\u3061\u3093\u3068\u30af\u30e9\u30b9\u30bf\u5206\u3051\u3055\u308c\u3066\u3044\u308b\u307f\u305f\u3044\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u3061\u306a\u307f\u306b\u3001\u300c\u793e\u4f1a\u300d\u300c\u7406\u79d1\u300d\u306a\u3069\u9805\u76ee\u6570\u304c3\u4ee5\u4e0a\u3042\u308b\u5834\u5408\u306f\u3001\u307e\u305a<a href=\"https:\/\/analysis-navi.com\/?p=2175\">\u6b21\u5143\u524a\u6e1b<\/a>\u3092\u4f7f\u3063\u3066\u3001\u9805\u76ee\u6570\u30922\u306b\u843d\u3068\u3057\u3066\u304b\u3089\u30b0\u30e9\u30d5\u5316\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u307e\u3068\u3081<\/h2>\n\n\n\n<p>K-means\u6cd5\u306f\u975e\u968e\u5c64\u578b\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u306e\u4ee3\u8868\u683c\u7684\u624b\u6cd5\u3067\u3059\u3002<br>Excel\u3067\u5b9f\u884c\u3059\u308b\u306e\u306f\u96e3\u3057\u3044\u3067\u3059\u304c\u3001Python\u3067\u306f\u975e\u5e38\u306b\u7c21\u5358\u306b\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002<br>\u30c7\u30fc\u30bf\u3092\u30b0\u30eb\u30fc\u30d7\u5206\u3051\u3057\u305f\u3044\u6642\u306b\u306f\u4f7f\u3063\u3066\u307f\u3066\u4e0b\u3055\u3044\u3002<\/p>\n\n\n\n<p>\u307e\u305f\u3001\u968e\u5c64\u578b\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3067\u306f\u5b9f\u884c\u65b9\u6cd5\u304c\u7570\u306a\u308a\u307e\u3059\u306e\u3067\u3001\u5225\u306e\u8a18\u4e8b\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/analysis-navi.com\/?p=1884\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u968e\u5c64\u578b\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u306e\u4ee3\u8868\u7684\u624b\u6cd5\u3067\u3042\u308b\u300ck-means\u6cd5\u300d\u3092Python\u3067\u5b9f\u884c\u3057\u3066\u307f\u307e\u3059\u3002k-means\u6cd5\u306e\u7406\u8ad6\u306b\u3064\u3044\u3066\u306f\u3053\u3061\u3089\u306e\u8a18\u4e8b\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002 \u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u8fbc\u307f\u3001k-means\u6cd5\u306e\u5b9f\u884c \u4eca\u56de\u306f\u7406\u8ad6\u7de8\u3067\u4f7f\u7528\u3057\u305f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5050,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"swell_btn_cv_data":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[129],"tags":[101,134],"class_list":["post-2147","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-study","tag-ml","tag-python"],"_links":{"self":[{"href":"https:\/\/analysis-navi.com\/index.php?rest_route=\/wp\/v2\/posts\/2147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/analysis-navi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/analysis-navi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/analysis-navi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/analysis-navi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2147"}],"version-history":[{"count":13,"href":"https:\/\/analysis-navi.com\/index.php?rest_route=\/wp\/v2\/posts\/2147\/revisions"}],"predecessor-version":[{"id":5150,"href":"https:\/\/analysis-navi.com\/index.php?rest_route=\/wp\/v2\/posts\/2147\/revisions\/5150"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/analysis-navi.com\/index.php?rest_route=\/wp\/v2\/media\/5050"}],"wp:attachment":[{"href":"https:\/\/analysis-navi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/analysis-navi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/analysis-navi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}