Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nogawanogawa/83f8813699deb62130421dc107debf29 to your computer and use it in GitHub Desktop.
Save nogawanogawa/83f8813699deb62130421dc107debf29 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "anonymous-details",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: tslearn in /opt/conda/lib/python3.7/site-packages (0.5.0.5)\n",
"Requirement already satisfied: scipy in /opt/conda/lib/python3.7/site-packages (from tslearn) (1.5.4)\n",
"Requirement already satisfied: joblib in /opt/conda/lib/python3.7/site-packages (from tslearn) (1.0.1)\n",
"Requirement already satisfied: numpy in /opt/conda/lib/python3.7/site-packages (from tslearn) (1.19.5)\n",
"Requirement already satisfied: Cython in /opt/conda/lib/python3.7/site-packages (from tslearn) (0.29.23)\n",
"Requirement already satisfied: numba in /opt/conda/lib/python3.7/site-packages (from tslearn) (0.53.1)\n",
"Requirement already satisfied: scikit-learn in /opt/conda/lib/python3.7/site-packages (from tslearn) (0.24.1)\n",
"Requirement already satisfied: llvmlite<0.37,>=0.36.0rc1 in /opt/conda/lib/python3.7/site-packages (from numba->tslearn) (0.36.0)\n",
"Requirement already satisfied: setuptools in /opt/conda/lib/python3.7/site-packages (from numba->tslearn) (49.6.0.post20210108)\n",
"Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/conda/lib/python3.7/site-packages (from scikit-learn->tslearn) (2.1.0)\n"
]
}
],
"source": [
"!pip install tslearn"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "current-beatles",
"metadata": {},
"outputs": [],
"source": [
"from tslearn.utils import to_time_series_dataset\n",
"from tslearn.clustering import TimeSeriesKMeans\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "maritime-prerequisite",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv('data.csv', index_col=0)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "grateful-daisy",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>U1</th>\n",
" <th>U2</th>\n",
" <th>U3</th>\n",
" <th>U4</th>\n",
" <th>U5</th>\n",
" <th>U6</th>\n",
" <th>U7</th>\n",
" <th>U8</th>\n",
" <th>U9</th>\n",
" <th>U10</th>\n",
" <th>...</th>\n",
" <th>S2_11</th>\n",
" <th>S2_12</th>\n",
" <th>S2_13</th>\n",
" <th>S2_14</th>\n",
" <th>S2_15</th>\n",
" <th>S2_16</th>\n",
" <th>S2_17</th>\n",
" <th>S2_18</th>\n",
" <th>S2_19</th>\n",
" <th>S2_20</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>-1.369116</td>\n",
" <td>0.588713</td>\n",
" <td>-0.331683</td>\n",
" <td>0.956604</td>\n",
" <td>1.719448</td>\n",
" <td>-1.624502</td>\n",
" <td>-0.990678</td>\n",
" <td>0.886110</td>\n",
" <td>1.202466</td>\n",
" <td>0.640265</td>\n",
" <td>...</td>\n",
" <td>-0.759501</td>\n",
" <td>-0.647794</td>\n",
" <td>-0.790478</td>\n",
" <td>1.475009</td>\n",
" <td>0.298057</td>\n",
" <td>-1.098037</td>\n",
" <td>0.850984</td>\n",
" <td>-0.978142</td>\n",
" <td>-0.094059</td>\n",
" <td>-1.563210</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.413444</td>\n",
" <td>-0.881422</td>\n",
" <td>-0.057365</td>\n",
" <td>1.887388</td>\n",
" <td>0.122004</td>\n",
" <td>-3.372905</td>\n",
" <td>0.210756</td>\n",
" <td>-0.428336</td>\n",
" <td>4.454524</td>\n",
" <td>-0.951762</td>\n",
" <td>...</td>\n",
" <td>1.228968</td>\n",
" <td>3.557932</td>\n",
" <td>2.345388</td>\n",
" <td>2.750395</td>\n",
" <td>3.482565</td>\n",
" <td>2.375817</td>\n",
" <td>4.281437</td>\n",
" <td>0.354449</td>\n",
" <td>2.525218</td>\n",
" <td>2.201484</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1.501657</td>\n",
" <td>0.201008</td>\n",
" <td>-1.245297</td>\n",
" <td>2.270236</td>\n",
" <td>-0.361249</td>\n",
" <td>1.938491</td>\n",
" <td>3.835235</td>\n",
" <td>-0.025113</td>\n",
" <td>6.146861</td>\n",
" <td>0.741025</td>\n",
" <td>...</td>\n",
" <td>5.775468</td>\n",
" <td>4.053440</td>\n",
" <td>3.288227</td>\n",
" <td>3.011937</td>\n",
" <td>3.320740</td>\n",
" <td>4.354548</td>\n",
" <td>7.000637</td>\n",
" <td>2.008984</td>\n",
" <td>3.482153</td>\n",
" <td>5.735146</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1.614704</td>\n",
" <td>0.619525</td>\n",
" <td>-3.441087</td>\n",
" <td>3.652103</td>\n",
" <td>0.155117</td>\n",
" <td>2.989046</td>\n",
" <td>1.495931</td>\n",
" <td>3.153596</td>\n",
" <td>7.260651</td>\n",
" <td>0.599006</td>\n",
" <td>...</td>\n",
" <td>5.532211</td>\n",
" <td>7.297367</td>\n",
" <td>5.996035</td>\n",
" <td>2.035381</td>\n",
" <td>5.843981</td>\n",
" <td>5.949602</td>\n",
" <td>9.120375</td>\n",
" <td>3.242696</td>\n",
" <td>3.787943</td>\n",
" <td>6.989630</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>2.606210</td>\n",
" <td>1.529422</td>\n",
" <td>1.198023</td>\n",
" <td>4.469868</td>\n",
" <td>-1.792950</td>\n",
" <td>1.813075</td>\n",
" <td>1.673723</td>\n",
" <td>1.395687</td>\n",
" <td>5.256596</td>\n",
" <td>2.157961</td>\n",
" <td>...</td>\n",
" <td>2.983631</td>\n",
" <td>2.212022</td>\n",
" <td>5.908732</td>\n",
" <td>3.817045</td>\n",
" <td>7.009344</td>\n",
" <td>6.672379</td>\n",
" <td>9.935899</td>\n",
" <td>5.332582</td>\n",
" <td>4.893954</td>\n",
" <td>5.512125</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 100 columns</p>\n",
"</div>"
],
"text/plain": [
" U1 U2 U3 U4 U5 U6 U7 \\\n",
"1 -1.369116 0.588713 -0.331683 0.956604 1.719448 -1.624502 -0.990678 \n",
"2 0.413444 -0.881422 -0.057365 1.887388 0.122004 -3.372905 0.210756 \n",
"3 1.501657 0.201008 -1.245297 2.270236 -0.361249 1.938491 3.835235 \n",
"4 1.614704 0.619525 -3.441087 3.652103 0.155117 2.989046 1.495931 \n",
"5 2.606210 1.529422 1.198023 4.469868 -1.792950 1.813075 1.673723 \n",
"\n",
" U8 U9 U10 ... S2_11 S2_12 S2_13 S2_14 \\\n",
"1 0.886110 1.202466 0.640265 ... -0.759501 -0.647794 -0.790478 1.475009 \n",
"2 -0.428336 4.454524 -0.951762 ... 1.228968 3.557932 2.345388 2.750395 \n",
"3 -0.025113 6.146861 0.741025 ... 5.775468 4.053440 3.288227 3.011937 \n",
"4 3.153596 7.260651 0.599006 ... 5.532211 7.297367 5.996035 2.035381 \n",
"5 1.395687 5.256596 2.157961 ... 2.983631 2.212022 5.908732 3.817045 \n",
"\n",
" S2_15 S2_16 S2_17 S2_18 S2_19 S2_20 \n",
"1 0.298057 -1.098037 0.850984 -0.978142 -0.094059 -1.563210 \n",
"2 3.482565 2.375817 4.281437 0.354449 2.525218 2.201484 \n",
"3 3.320740 4.354548 7.000637 2.008984 3.482153 5.735146 \n",
"4 5.843981 5.949602 9.120375 3.242696 3.787943 6.989630 \n",
"5 7.009344 6.672379 9.935899 5.332582 4.893954 5.512125 \n",
"\n",
"[5 rows x 100 columns]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df .head()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "personalized-plain",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(100, 60, 1)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"ts = to_time_series_dataset(df.T)\n",
"\n",
"#形の確認\n",
"ts.shape"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "formed-humor",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ -1.36911576],\n",
" [ 0.4134441 ],\n",
" [ 1.5016571 ],\n",
" ...,\n",
" [ 23.20699066],\n",
" [ 26.19243319],\n",
" [ 22.47147175]],\n",
"\n",
" [[ 0.58871342],\n",
" [ -0.88142205],\n",
" [ 0.20100815],\n",
" ...,\n",
" [ 36.27279402],\n",
" [ 34.83189298],\n",
" [ 36.00628187]],\n",
"\n",
" [[ -0.33168339],\n",
" [ -0.05736545],\n",
" [ -1.2452973 ],\n",
" ...,\n",
" [ 18.19216913],\n",
" [ 20.82213286],\n",
" [ 18.14717848]],\n",
"\n",
" ...,\n",
"\n",
" [[ -0.9781419 ],\n",
" [ 0.35444924],\n",
" [ 2.00898362],\n",
" ...,\n",
" [ -0.87804185],\n",
" [ 1.90137692],\n",
" [ 1.84131418]],\n",
"\n",
" [[ -0.09405903],\n",
" [ 2.52521841],\n",
" [ 3.48215263],\n",
" ...,\n",
" [-10.83674329],\n",
" [ -8.87848103],\n",
" [ -9.65634187]],\n",
"\n",
" [[ -1.56321043],\n",
" [ 2.20148357],\n",
" [ 5.73514572],\n",
" ...,\n",
" [ 3.84033062],\n",
" [ 7.29490162],\n",
" [ 11.87765923]]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ts"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "reverse-checklist",
"metadata": {},
"outputs": [],
"source": [
"km = TimeSeriesKMeans(n_clusters=5, metric=\"dtw\")\n",
"labels = km.fit_predict(ts)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "other-council",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,\n",
" 3, 1, 1, 1, 3, 1, 1, 1, 0, 3, 3, 3, 0, 1, 3, 1, 3, 1, 2, 4, 4, 2,\n",
" 4, 2, 4, 4, 2, 2, 2, 2, 2, 4, 4, 4, 2, 4, 2, 4, 1, 0, 1, 1, 1, 0,\n",
" 1, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 1, 1, 3, 3, 1, 1, 3, 1, 1,\n",
" 3, 1, 3, 1, 1, 3, 3, 3, 3, 3, 1, 3])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"labels"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "infectious-region",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "amber-karaoke",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment