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

質問編集履歴

2

flattenなど1次元変換の際のエラー訂正

2020/03/20 23:11

投稿

dendenmushi
dendenmushi

スコア98

title CHANGED
File without changes
body CHANGED
@@ -49,27 +49,55 @@
49
49
  win10
50
50
  python3.7
51
51
 
52
- ### 2020/03/21追記
52
+ ### 2020/03/21追記(8:11訂正)
53
+
54
+ ごめんなさい間違えました。1次元には変換はできてもリスト型になってしまって、その後の相関係数を求める際に、リストが入っているのでだめですよと言われてしまうエラーでした。
53
55
  ```python
54
56
  import numpy as np
57
+ d_float_y = np.array([
58
+ [0.],1.,0.
59
+ ])
60
+ e_float_y = np.array([
61
+ [1.],1.,0.
62
+ ])
63
+ print(d_float_y.flatten()) # 1次元に3つあるものを1次元K個にreshapeできる
64
+ print(e_float_y.flatten())
55
65
  arr6 = []
56
- arr4 = [[1.], [1.], [0.], [1.]]
66
+ import numpy as np
57
- arr3 = [[1.], [1.], [0.], [0.]]
58
- arr6.append(arr4)
67
+ arr6.append(d_float_y)
59
- arr6.append(arr3)
68
+ arr6.append(e_float_y)
60
69
  print(arr6)
61
70
  results = np.corrcoef(arr6)
71
+ print("相関係数を出力")
72
+ print(results)
62
73
  ```
63
-
74
+ 結果
64
75
  ```python
76
+ [list([0.0]) 1.0 0.0]
77
+ [list([1.0]) 1.0 0.0]
78
+ [array([list([0.0]), 1.0, 0.0], dtype=object), array([list([1.0]), 1.0, 0.0], dtype=object)]
79
+ ---------------------------------------------------------------------------
80
+ TypeError Traceback (most recent call last)
81
+ <ipython-input-14-97f2f6639cde> in <module>()
82
+ 13 arr6.append(e_float_y)
83
+ 14 print(arr6)
84
+ ---> 15 results = np.corrcoef(arr6)
85
+ 16 print("相関係数を出力")
86
+ 17 print(results)
87
+
88
+ <__array_function__ internals> in corrcoef(*args, **kwargs)
89
+
90
+ 3 frames
65
91
  <__array_function__ internals> in cov(*args, **kwargs)
66
92
 
67
- /usr/local/lib/python3.6/dist-packages/numpy/lib/function_base.py in cov(m, y, rowvar, bias, ddof, fweights, aweights)
68
- 2369 m = np.asarray(m)
69
- 2370 if m.ndim > 2:
70
- -> 2371 raise ValueError("m has more than 2 dimensions")
93
+ <__array_function__ internals> in average(*args, **kwargs)
71
- 2372
72
- 2373 if y is None:
73
94
 
95
+ /usr/local/lib/python3.6/dist-packages/numpy/core/_methods.py in _mean(a, axis, dtype, out, keepdims)
96
+ 149 is_float16_result = True
97
+ 150
98
+ --> 151 ret = umr_sum(arr, axis, dtype, out, keepdims)
99
+ 152 if isinstance(ret, mu.ndarray):
74
- ValueError: m has more than 2 dimensions
100
+ 153 ret = um.true_divide(
101
+
102
+ TypeError: can only concatenate list (not "float") to list
75
103
  ```

1

2次元に対するflattenやreshape(-1,)はエラーになる旨追記

2020/03/20 23:11

投稿

dendenmushi
dendenmushi

スコア98

title CHANGED
File without changes
body CHANGED
@@ -47,4 +47,29 @@
47
47
 
48
48
  ### 補足
49
49
  win10
50
- python3.7
50
+ python3.7
51
+
52
+ ### 2020/03/21追記
53
+ ```python
54
+ import numpy as np
55
+ arr6 = []
56
+ arr4 = [[1.], [1.], [0.], [1.]]
57
+ arr3 = [[1.], [1.], [0.], [0.]]
58
+ arr6.append(arr4)
59
+ arr6.append(arr3)
60
+ print(arr6)
61
+ results = np.corrcoef(arr6)
62
+ ```
63
+
64
+ ```python
65
+ <__array_function__ internals> in cov(*args, **kwargs)
66
+
67
+ /usr/local/lib/python3.6/dist-packages/numpy/lib/function_base.py in cov(m, y, rowvar, bias, ddof, fweights, aweights)
68
+ 2369 m = np.asarray(m)
69
+ 2370 if m.ndim > 2:
70
+ -> 2371 raise ValueError("m has more than 2 dimensions")
71
+ 2372
72
+ 2373 if y is None:
73
+
74
+ ValueError: m has more than 2 dimensions
75
+ ```