teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

ソースの追記を致しました。

2020/06/05 02:12

投稿

K-Shape
K-Shape

スコア1

title CHANGED
File without changes
body CHANGED
@@ -13,9 +13,42 @@
13
13
  2 18 2.2
14
14
  ・・・・・
15
15
 
16
+
16
17
  ### 該当のソース
17
18
  ```
18
19
  ```
20
+ def read_file_to_dataframe(filenames):
21
+ dfs = []
22
+ for filename in filenames:
23
+ original_df = pd.read_csv(filename, index_col=None, header=0)
24
+ dfs.append(original_df)
25
+ return dfs
26
+
27
+ def time_series_data_to_array(dataframes, target_col=''):
28
+ tsdata = []
29
+ for i, df in enumerate(dataframes):
30
+ tsdata.append(df[target_col].values.tolist()[:])
31
+ len_max = 0
32
+ for ts in tsdata:
33
+ if len(ts) > len_max:
34
+ len_max = len(ts)
35
+ for i, ts in enumerate(tsdata):
36
+ len_add = len_max - len(ts)
37
+ tsdata[i] = ts + [ts[-1]] * len_add
38
+
39
+ tsdata = np.array(tsdata)
40
+ return tsdata
41
+
42
+ def transform_vector(time_series_array):
43
+ stack_list = []
44
+ for j in range(len(time_series_array)):
45
+ data = np.array(time_series_array[j])
46
+ data = data.reshape((1, len(data))).T
47
+ stack_list.append(data)
48
+ stack_data = np.stack(stack_list, axis=0)
49
+ return stack_data
50
+ ```
51
+ ```
19
52
  df2=read_1file_to_dataframe('sample_data.csv')
20
53
  ```
21
54
  ```

2

ソース部分を```で囲みました。

2020/06/05 02:12

投稿

K-Shape
K-Shape

スコア1

title CHANGED
File without changes
body CHANGED
@@ -15,12 +15,14 @@
15
15
 
16
16
  ### 該当のソース
17
17
  ```
18
+ ```
18
19
  df2=read_1file_to_dataframe('sample_data.csv')
19
20
  ```
20
21
  ```
21
22
  tsdata = time_series_data_to_array(dataframes=df2, target_col='data')
22
23
  stack_data = transform_vector(time_series_array=tsdata)
23
24
  ```
25
+
24
26
  ```
25
27
  plt.figure(figsize=(16,9))
26
28
  for yi in range(2):

1

ソース部分を```で囲みました。

2020/06/05 01:32

投稿

K-Shape
K-Shape

スコア1

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
- ### 前提・実現したいこと
1
+ ```### 前提・実現したいこと
2
2
 
3
3
  Python 3.6.4およびtslearnを使用して、経時データのK-Shapeによるクラスタリングを行っています。
4
4
  一つのグラフ中の複数のサンプルの凡例を示したいです。
@@ -13,15 +13,15 @@
13
13
  2 18 2.2
14
14
  ・・・・・
15
15
 
16
-
17
16
  ### 該当のソース
18
-
17
+ ```
19
18
  df2=read_1file_to_dataframe('sample_data.csv')
20
-
19
+ ```
20
+ ```
21
21
  tsdata = time_series_data_to_array(dataframes=df2, target_col='data')
22
22
  stack_data = transform_vector(time_series_array=tsdata)
23
-
24
- -------------------------------
23
+ ```
24
+ ```
25
25
  plt.figure(figsize=(16,9))
26
26
  for yi in range(2):
27
27
  plt.subplot(2, 1, 1 + yi)
@@ -32,5 +32,6 @@
32
32
  plt.legend(loc="upper right")
33
33
  plt.tight_layout()
34
34
  plt.show()
35
+ ```
35
36
 
36
37
  以上、宜しくお願い致します。