{"id":4672,"date":"2024-02-27T18:44:29","date_gmt":"2024-02-27T09:44:29","guid":{"rendered":"https:\/\/blog.since2020.jp\/?p=4672"},"modified":"2024-02-27T18:44:29","modified_gmt":"2024-02-27T09:44:29","slug":"time-series-k-means%e3%81%ae%e3%83%87%e3%83%bc%e3%82%bf%e5%8a%a0%e5%b7%a5%e3%81%a8%ef%bc%93%e7%a8%ae%e9%a1%9e%e3%81%ae%e3%82%af%e3%83%a9%e3%82%b9%e3%82%bf%e3%83%aa%e3%83%b3%e3%82%b0%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/since2020.jp\/media\/time-series-k-means%e3%81%ae%e3%83%87%e3%83%bc%e3%82%bf%e5%8a%a0%e5%b7%a5%e3%81%a8%ef%bc%93%e7%a8%ae%e9%a1%9e%e3%81%ae%e3%82%af%e3%83%a9%e3%82%b9%e3%82%bf%e3%83%aa%e3%83%b3%e3%82%b0%e6%96%b9%e6%b3%95\/","title":{"rendered":"Time Series K-means\u306e\u30c7\u30fc\u30bf\u52a0\u5de5\u3068\uff13\u7a2e\u985e\u306e\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u65b9\u6cd5\u3092\u5fb9\u5e95\u89e3\u8aac\u3010\u6642\u7cfb\u5217\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3011"},"content":{"rendered":"\n<p>Time Series K-means\u306e\u30c7\u30fc\u30bf\u52a0\u5de5\u3068\uff13\u7a2e\u985e\u306e\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u65b9\u6cd5\u3092\u5fb9\u5e95\u89e3\u8aac\u3010\u6642\u7cfb\u5217\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3011<\/p>\n\n\n<h2>\u306f\u3058\u3081\u306b<\/h2>\n<p>\u3053\u3061\u3089\u306e\u8a18\u4e8b\u306b\u6642\u7cfb\u5217\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3092Time Series K-means\u3067\u3084\u3063\u3066\u307f\u305f\u3068\u3044\u3046\u8a18\u4e8b\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u4eca\u56de\u306fTime Series K-means\u3067\u6642\u7cfb\u5217\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3092\u884c\u3046\u524d\u306b\u3069\u306e\u3088\u3046\u306b\u30c7\u30fc\u30bf\u52a0\u5de5\u3092\u3057\u305f\u3089\u3044\u3044\u306e\u304b\u3001\u307e\u305fTime Series K-means\u306b\u3042\u308b\uff13\u7a2e\u985e\u306e\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u65b9\u6cd5\u3092\u89e3\u8aac\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\r\n<p><a href=\"https:\/\/blog.since2020.jp\/data_analysis\/time_series_kmeans\/\">\u3010AI\u521d\u5b66\u8005\u5411\u3051\u3011Time Series K-means\u3067\u6642\u7cfb\u5217\u30c7\u30fc\u30bf\u3092\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3057\u3066\u307f\u305f | Data Driven Knowledgebase (since2020.jp)<\/a><\/p>\r\n<p>\u3067\u306f\u3055\u3063\u305d\u304f\u30c7\u30fc\u30bf\u52a0\u5de5\u306e\u65b9\u6cd5\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3059\uff01<\/p>\r\n<p><!-- notionvc: 67cf8b78-cee3-4b6b-8f9f-37fc82525034 --><\/p>\n\n<h2>Time Series K-means\u7528\u306e\u30c7\u30fc\u30bf\u52a0\u5de5\u3068\u524d\u51e6\u7406<\/h2>\n<p>Time Series K-means\u306b\u30c7\u30fc\u30bf\u3092\u6e21\u3059\u524d\u306b\u4ee5\u4e0b\u306e\u52a0\u5de5\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\r\n<p>\u79c1\u304c\u53c2\u8003\u306b\u3057\u305f\u306e\u306f\u516c\u5f0f\u306e\u30b3\u30fc\u30c9\u3067\u3059\u3002<\/p>\r\n<p><a href=\"https:\/\/tslearn.readthedocs.io\/en\/stable\/auto_examples\/clustering\/plot_kmeans.html#sphx-glr-auto-examples-clustering-plot-kmeans-py\">k-means \u2014 tslearn 0.6.3 documentation<\/a><\/p>\r\n<p><strong>\uff1cTime Series K-means\u306b\u30c7\u30fc\u30bf\u3092\u6e21\u3059\u524d\u306b\u5fc5\u8981\u306a\u52a0\u5de5\uff1e<\/strong><\/p>\r\n<p><strong>\u2460\u65e5\u4ed8\u3068\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3057\u305f\u3044\u5909\u6570\u306e\uff12\u5909\u6570\u306e\u307f\u6b8b\u3059<\/strong><\/p>\r\n<p><strong>\u2461\u6642\u7cfb\u5217\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u7528\u306b\u30b9\u30b1\u30fc\u30ea\u30f3\u30b0<\/strong><\/p>\r\n<p><strong>\u2462numpy\u914d\u5217\u306b\u5909\u63db<\/strong><\/p>\r\n<p><strong>\u2463\uff08\u8907\u6570\u6642\u7cfb\u5217\u306e\u6570,\u6642\u7cfb\u5217\u306e\u65e5\u4ed8\u306e\u6570,1)\u306e\uff13\u6b21\u5143\u30d9\u30af\u30c8\u30eb\u306b\u5909\u63db<\/strong><\/p>\r\n<p>\u2462\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u89e3\u8aac\u3059\u308b\u3068\u3001numpy\u914d\u5217\u306b\u5909\u63db\u3059\u308b\u3068\u304d\u306b\u3001\u884c\u304c\u8907\u6570\u6642\u7cfb\u5217\u306b\u306a\u308a\u3001\u30ab\u30e9\u30e0\u304c\u65e5\u4ed8\u306b\u306a\u308b\u30a4\u30e1\u30fc\u30b8\u3067\u3059\u3002<\/p>\r\n<p>\u4f8b\uff1a<\/p>\r\n<p>\u516c\u5f0f\u30b3\u30fc\u30c9<\/p>\r\n<p><!-- notionvc: 362c1553-7e58-480a-8be8-d9ee5995f655 --><\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>!pip install tslearn\r\nimport numpy\r\nimport pandas as pd\r\nimport matplotlib.pyplot as plt\r\n\r\nfrom tslearn.clustering import TimeSeriesKMeans\r\nfrom tslearn.datasets import CachedDatasets\r\nfrom tslearn.preprocessing import TimeSeriesScalerMeanVariance, \\\r\nTimeSeriesResampler\r\n\r\nseed = 0\r\nnumpy.random.seed(seed)\r\nX_train, y_train, X_test, y_test = CachedDatasets().load_dataset(\"Trace\")\r\nX_train = X_train[y_train &lt; 4] # Keep first 3 classes\r\nnumpy.random.shuffle(X_train)\r\n# Keep only 50 time series\r\nX_train = TimeSeriesScalerMeanVariance().fit_transform(X_train[:50])\r\n# Make time series shorter\r\nX_train = TimeSeriesResampler(sz=40).fit_transform(X_train)\r\nsz = X_train.shape[1]\r\n\r\n# Soft-DTW-k-means\r\nprint(\"DTW k-means\")\r\n# data_3d \u306e\u5f62\u72b6\u3092\u78ba\u8a8d\r\nprint(X_train.shape)\r\nsdtw_km = TimeSeriesKMeans(n_clusters=3,\r\nmetric=\"softdtw\",\r\nmetric_params={\"gamma\": .01},\r\nverbose=True,\r\nrandom_state=seed)<\/code><\/pre>\r\n<\/div>\r\n<p>\u3053\u3061\u3089\u306b\u516c\u5f0f\u306e\u30b3\u30fc\u30c9\u306eTime Series K-means\u3092\u884c\u3046\u524d\u306e\u30c7\u30fc\u30bf\u3092\u8f09\u305b\u307e\u3059\u3002<\/p>\r\n<p>\u3053\u306e\u30c7\u30fc\u30bf\u3060\u3068\u3001(50,40,1)\u3068\u3044\u304650\u7a2e\u985e\u306e\u6642\u7cfb\u5217\u30c7\u30fc\u30bf\u306840\u5206\u306e\u65e5\u4ed8\u30c7\u30fc\u30bf\u3092\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3057\u3066\u3044\u308b\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3059\u3002<\/p>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-96.png\" alt=\"\" width=\"945\" height=\"39\" class=\"alignnone size-full wp-image-4673\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-96.png 945w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-96-300x12.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-96-768x32.png 768w\" sizes=\"(max-width: 945px) 100vw, 945px\" \/><\/p>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-97.png\" alt=\"\" width=\"665\" height=\"253\" class=\"alignnone size-full wp-image-4674\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-97.png 665w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-97-300x114.png 300w\" sizes=\"(max-width: 665px) 100vw, 665px\" \/><\/p>\r\n<p>\u3061\u306a\u307f\u306b\u79c1\u306f\u3053\u306e\u3088\u3046\u306b\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3057\u305f\u3044\u30c7\u30fc\u30bf\u3092\u52a0\u5de5\u3057\u307e\u3057\u305f\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>def df_to_DSK(df):\r\n# \u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u3092\u30d4\u30dc\u30c3\u30c8\u3057\u3066 numpy \u914d\u5217\u306b\u5909\u63db\r\npivot_df = df.pivot(index='ID', columns='date', values='y')\r\nprint(pivot_df)\r\nnumpy_array = pivot_df.to_numpy()\r\n\r\n# 2\u6b21\u5143\u304b\u30893\u6b21\u5143\u306b\u5909\u63db\r\nnumpy_array = numpy_array [:, :, numpy.newaxis]\r\n\r\n# data_3d \u306e\u5f62\u72b6\u3092\u78ba\u8a8d\r\nprint(numpy_array .shape)\r\nreturn numpy_array<\/code><\/pre>\r\n<\/div>\r\n<p><!-- notionvc: 764c10ad-3683-4e59-84d6-e68f0037b5a5 --><\/p>\n\n<h2>Time Series K-means\u306b\u3042\u308b\uff13\u7a2e\u985e\u306e\u30d0\u30ea\u30a2\u30f3\u30c8<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-98.png\" alt=\"\" width=\"806\" height=\"568\" class=\"alignnone size-full wp-image-4675\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-98.png 806w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-98-300x211.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-98-768x541.png 768w\" sizes=\"(max-width: 806px) 100vw, 806px\" \/><\/p>\r\n<p>\u5f15\u7528\uff1a<a href=\"https:\/\/tslearn.readthedocs.io\/en\/stable\/auto_examples\/clustering\/plot_kmeans.html#sphx-glr-auto-examples-clustering-plot-kmeans-py\">k-means \u2014 tslearn 0.6.3 documentation<\/a><\/p>\r\n<p>Time Series K-means\u306b\u306f\uff13\u7a2e\u985e\u3042\u308a\u307e\u3059\u3002<\/p>\r\n<p><!-- notionvc: 4fc3bd2f-a32d-4f47-b635-43dce5e0709d --><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-100.png\" alt=\"\" width=\"972\" height=\"126\" class=\"alignnone size-full wp-image-4677\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-100.png 972w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-100-300x39.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-100-768x100.png 768w\" sizes=\"(max-width: 972px) 100vw, 972px\" \/><\/p>\r\n<p><strong>\uff1cTime Series K-means\u306e\uff13\u7a2e\u985e\uff1e<\/strong><\/p>\r\n<p><strong>\u2460Euclidean K-means<\/strong><\/p>\r\n<p><strong>\u2461DBA-K-means<\/strong><\/p>\r\n<p><strong>\u2462Soft-DTW-K-means<\/strong><\/p>\r\n<p>\u305d\u308c\u305e\u308c\u306e\u610f\u5473\u306f\u3053\u3061\u3089\u306b\u8a18\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\r\n<p>[1] F. Petitjean, A. Ketterlin &amp; P. Gancarski. A global averaging method for dynamic time warping, with applications to clustering. Pattern Recognition, Elsevier, 2011, Vol. 44, Num. 3, pp. 678-693 [2] M. Cuturi, M. Blondel \u201cSoft-DTW: a Differentiable Loss Function for Time-Series,\u201d ICML 2017.<\/p>\r\n<p>\u4e00\u822c\u7684\u306aDTW\u306b\u3088\u308b\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u306fDBA-K-means\u3068\u547c\u3070\u308c\u3066\u3044\u308b\u3082\u306e\u3067\u3059\u306d\u3002<\/p>\r\n<p><!-- notionvc: c38a6289-6240-4ac8-9431-1e6884d7994f --><\/p>\n\n<h2>\uff13\u7a2e\u985e\u306eTime Series K-means\u306e\u30b3\u30fc\u30c9<\/h2>\n<p>\u3055\u3066\u3001\u3053\u308c\u3089\uff13\u7a2e\u985e\u306e\u65b9\u6cd5\u3067\u6642\u7cfb\u5217\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3092\u884c\u3046\u306b\u306f\u3069\u3046\u3057\u305f\u3089\u3088\u3044\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\r\n<p>\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u306b\u8a18\u3057\u3066\u304a\u304d\u307e\u3057\u305f\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># Euclidean k-means\r\nprint(\"Euclidean k-means\")\r\nkm = TimeSeriesKMeans(n_clusters=3, verbose=True, random_state=seed)\r\ny_pred = km.fit_predict(X_train)\r\n\r\n# DBA-k-means\r\nprint(\"DBA k-means\")\r\ndba_km = TimeSeriesKMeans(n_clusters=3,\r\nn_init=2,\r\nmetric=\"dtw\",\r\nverbose=True,\r\nmax_iter_barycenter=10,\r\nrandom_state=seed)\r\ny_pred = dba_km.fit_predict(X_train)\r\n\r\n# Soft-DTW-k-means\r\nprint(\"Soft-DTW k-means\")\r\nsdtw_km = TimeSeriesKMeans(n_clusters=3,\r\nmetric=\"softdtw\",\r\nmetric_params={\"gamma\": .01},\r\nverbose=True,\r\nrandom_state=seed)\r\ny_pred = sdtw_km.fit_predict(X_train)<\/code><\/pre>\r\n<\/div>\r\n<p>&nbsp;<\/p>\n\n<h2>\u6642\u7cfb\u5217\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u306e\u7d50\u679c\u306e\u53ef\u8996\u5316\u65b9\u6cd5<\/h2>\n<p>\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u306e\u3088\u3046\u306b\u3057\u3066\u53ef\u8996\u5316\u3092\u884c\u3044\u307e\u3057\u305f\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>y_pred = sdtw_km.fit_predict(X_train)\r\n\r\nfor yi in range(3):\r\nplt.subplot(3, 3, 7 + yi)\r\nfor xx in X_train[y_pred == yi]:\r\nplt.plot(xx.ravel(), \"k-\", alpha=.2)\r\nplt.plot(sdtw_km.cluster_centers_[yi].ravel(), \"r-\")\r\nplt.xlim(0, sz)\r\nplt.ylim(-4, 4)\r\nplt.text(0.55, 0.85,'Cluster %d' % (yi + 1),\r\ntransform=plt.gca().transAxes)\r\nif yi == 1:\r\nplt.title(\"Soft-DTW $k$-means\")\r\n\r\nplt.tight_layout()\r\nplt.show()<\/code><\/pre>\r\n<\/div>\r\n<p>\u5b9f\u884c\u5f8c\u306f\u3053\u306e\u3088\u3046\u306b\u8868\u793a\u3055\u308c\u307e\u3059\u3002<\/p>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-99.png\" alt=\"\" width=\"1273\" height=\"362\" class=\"alignnone size-full wp-image-4676\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-99.png 1273w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-99-300x85.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-99-1024x291.png 1024w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/02\/Untitled-99-768x218.png 768w\" sizes=\"(max-width: 1273px) 100vw, 1273px\" \/><\/p>\n\n<h2>Time Series K-means\u306e\u5168\u30b3\u30fc\u30c9<\/h2>\n<p>\u516c\u5f0f\u306e\u30b3\u30fc\u30c9\u306f\u3053\u3061\u3089\u306b\u3042\u308a\u307e\u3059\u306e\u3067\u3001\u3054\u53c2\u7167\u4e0b\u3055\u3044\u3002\u3053\u3061\u3089\u306e\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30fc\u3059\u308b\u3060\u3051\u3067\u3082\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002<\/p>\r\n<p><a href=\"https:\/\/tslearn.readthedocs.io\/en\/stable\/auto_examples\/clustering\/plot_kmeans.html#sphx-glr-auto-examples-clustering-plot-kmeans-py\">k-means \u2014 tslearn 0.6.3 documentation<\/a><\/p>\r\n<p>\u79c1\u306e\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3057\u305f\u304b\u3063\u305f\u3082\u306e\u3092\u6642\u7cfb\u5217\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3057\u305f\u3044\u3068\u304d\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u884c\u3044\u307e\u3057\u305f\u3002<\/p>\r\n<p><!-- notionvc: c66fb7e5-9e81-4523-85e6-b60706230976 --><\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>#TimeSeriesKMeans\u3092\u5b9f\u884c\r\n#!pip install tslearn\r\nimport pandas as pd\r\nimport numpy\r\nimport time\r\nimport matplotlib.pyplot as plt\r\nimport seaborn as sns\r\nfrom tslearn.clustering import TimeSeriesKMeans\r\nfrom tslearn.datasets import CachedDatasets\r\nfrom tslearn.preprocessing import TimeSeriesScalerMeanVariance, \\\r\nTimeSeriesResampler\r\n\r\ndef df_to_DSK(df):\r\n# \u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u3092\u30d4\u30dc\u30c3\u30c8\u3057\u3066 numpy \u914d\u5217\u306b\u5909\u63db\r\npivot_df = df.pivot(index='ID', columns='date', values='y')\r\nprint(pivot_df)\r\nnumpy_array = pivot_df.to_numpy()\r\n\r\n# 2\u6b21\u5143\u304b\u30893\u6b21\u5143\u306b\u5909\u63db\r\nnumpy_array = numpy_array [:, :, numpy.newaxis]\r\n\r\n# data_3d \u306e\u5f62\u72b6\u3092\u78ba\u8a8d\r\nprint(numpy_array .shape)\r\nreturn numpy_array\r\n\r\n\r\ndef TimeSeriesKmeans(df,n):\r\ndf_train = df[df['date'] &lt; '2022-11-25']\r\nnumpy_array=df_to_DSK(df_train)\r\n\r\nX_train = numpy_array\r\n#\u30b9\u30b1\u30fc\u30ea\u30f3\u30b0\r\nX_train_scaled = TimeSeriesScalerMeanVariance().fit_transform(X_train)\r\n\r\n# Define a seed value\r\nseed = 42\r\n\r\n# DBA-k-means\r\nprint(\"DBA k-means\")\r\ndba_km = TimeSeriesKMeans(n_clusters=n,\r\nn_init=2,\r\nmetric=\"dtw\",\r\nverbose=True,\r\nmax_iter_barycenter=10,\r\nrandom_state=seed)\r\n\r\ny_pred = dba_km.fit_predict(X_train_scaled)\r\n\r\nreturn y_pred,X_train_scaled,n,dba_km\r\n\r\ndef show_result(X_train_scaled,n,dba_km):\r\nsz = X_train_scaled.shape[1]\r\n\r\nfor yi in range(n):\r\nplt.subplot(3, 3, 4 + yi)\r\nfor xx in X_train_scaled[y_pred == yi]:\r\nplt.plot(xx.ravel(), \"k-\", alpha=.2)\r\nplt.plot(dba_km.cluster_centers_[yi].ravel(), \"r-\")\r\nplt.xlim(0, sz)\r\n#plt.ylim(-4, 4)\r\nplt.text(0.55, 0.85,'Cluster %d' % (yi + 1),\r\ntransform=plt.gca().transAxes)\r\nif yi == 1:\r\nplt.title(\"DBA $k$-means\")\r\n\r\nplt.tight_layout()\r\nplt.show()\r\n\r\ndef show_Cluster(mergerd_df,X_train_scaled):\r\nX_train_reshaped = X_train_scaled.reshape(1059, 74, 1)\r\nX_train_flattened = X_train_reshaped.reshape((-1, 1))\r\ndf = pd.DataFrame(X_train_flattened, columns=['scaled_y'])\r\ndf_result = pd.concat([mergerd_df, df], axis=1)\r\n\r\nsubset_df = df_result\r\n\r\n# \u30b0\u30e9\u30d5\u63cf\u753b\r\nplt.figure(figsize=(10, 6))\r\nsns.lineplot(x='date', y='scaled_y', hue='cluster_label', style='ID', data=subset_df, markers=False, palette='viridis')\r\n\r\n# \u30d7\u30ed\u30c3\u30c8\u306e\u8a2d\u5b9a\r\nplt.xlabel('Date')\r\nplt.ylabel('Scaled y')\r\nplt.title('Line Plot with Cluster Labels')\r\n#plt.ylim(0,500)\r\nplt.show()\r\n\r\nreturn df_result\r\n\r\n#\u7d50\u679c\u3092pandas\u578b\u306b\u5909\u63db\r\ndef DSK_to_df(df,y_pred):\r\nresult = pd.DataFrame()\r\nresult['ID'] = df['ID'].drop_duplicates()\r\nresult['cluster_label'] = y_pred\r\n\r\nmergerd_df = pd.merge(df,result,on='ID',how='inner')\r\nprint(mergerd_df)\r\nreturn mergerd_df\r\n\r\ndef main():\r\ndf = pd.read_csv('\u4fdd\u5b58\u30d5\u30a1\u30a4\u30eb\u540d')\r\n\r\n# \u8a08\u6e2c\u958b\u59cb\r\nstart_time = time.time()\r\ny_pred,X_train_scaled,n,dba_km = TimeSeriesKmeans(df,3)\r\nmergerd_df=DSK_to_df(df,y_pred)\r\n# \u8a08\u6e2c\u7d42\u4e86\r\nend_time = time.time()\r\nelapsed_time = end_time - start_time\r\nprint(f\"\u7d4c\u904e\u6642\u9593: {elapsed_time}\u79d2\")\r\n#\u7d50\u679c\u3092\u63cf\u753b\r\nshow_result(X_train_scaled,n,dba_km)\r\nshow_Cluster(mergerd_df,X_train_scaled)\r\nreturn mergerd_df\r\n\r\nresult_df = main()<\/code><\/pre>\r\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Time Series K-means\u306e\u30c7\u30fc\u30bf\u52a0\u5de5\u3068\uff13\u7a2e\u985e\u306e\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u65b9\u6cd5\u3092\u5fb9\u5e95\u89e3\u8aac\u3010\u6642\u7cfb\u5217\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3011 \u306f\u3058\u3081\u306b \u3053\u3061\u3089\u306e\u8a18\u4e8b\u306b\u6642\u7cfb\u5217\u30af\u30e9\u30b9\u30bf\u30ea\u30f3\u30b0\u3092Time Series K-means\u3067\u3084\u3063\u3066\u307f\u305f\u3068\u3044\u3046\u8a18\u4e8b [&hellip;]<\/p>\n","protected":false},"author":83,"featured_media":3123,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","swell_btn_cv_data":"","footnotes":"","_wp_rev_ctl_limit":""},"categories":[1246],"tags":[568],"class_list":["post-4672","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-infrastructure","tag-568"],"_links":{"self":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/4672","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/users\/83"}],"replies":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/comments?post=4672"}],"version-history":[{"count":0,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/4672\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media\/3123"}],"wp:attachment":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media?parent=4672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/categories?post=4672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/tags?post=4672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}