質問編集履歴

2

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

2020/03/20 23:11

投稿

dendenmushi
dendenmushi

スコア98

test CHANGED
File without changes
test CHANGED
@@ -100,50 +100,106 @@
100
100
 
101
101
 
102
102
 
103
- ### 2020/03/21追記
103
+ ### 2020/03/21追記(8:11訂正)
104
+
105
+
106
+
107
+ ごめんなさい間違えました。1次元には変換はできてもリスト型になってしまって、その後の相関係数を求める際に、リストが入っているのでだめですよと言われてしまうエラーでした。
104
108
 
105
109
  ```python
106
110
 
107
111
  import numpy as np
108
112
 
113
+ d_float_y = np.array([
114
+
115
+ [0.],1.,0.
116
+
117
+ ])
118
+
119
+ e_float_y = np.array([
120
+
121
+ [1.],1.,0.
122
+
123
+ ])
124
+
125
+ print(d_float_y.flatten()) # 1次元に3つあるものを1次元K個にreshapeできる
126
+
127
+ print(e_float_y.flatten())
128
+
109
129
  arr6 = []
110
130
 
111
- arr4 = [[1.], [1.], [0.], [1.]]
131
+ import numpy as np
112
-
113
- arr3 = [[1.], [1.], [0.], [0.]]
132
+
114
-
115
- arr6.append(arr4)
133
+ arr6.append(d_float_y)
116
-
134
+
117
- arr6.append(arr3)
135
+ arr6.append(e_float_y)
118
136
 
119
137
  print(arr6)
120
138
 
121
139
  results = np.corrcoef(arr6)
122
140
 
141
+ print("相関係数を出力")
142
+
143
+ print(results)
144
+
123
- ```
145
+ ```
124
-
146
+
125
-
147
+ 結果
126
-
148
+
127
- ```python
149
+ ```python
150
+
151
+ [list([0.0]) 1.0 0.0]
152
+
153
+ [list([1.0]) 1.0 0.0]
154
+
155
+ [array([list([0.0]), 1.0, 0.0], dtype=object), array([list([1.0]), 1.0, 0.0], dtype=object)]
156
+
157
+ ---------------------------------------------------------------------------
158
+
159
+ TypeError Traceback (most recent call last)
160
+
161
+ <ipython-input-14-97f2f6639cde> in <module>()
162
+
163
+ 13 arr6.append(e_float_y)
164
+
165
+ 14 print(arr6)
166
+
167
+ ---> 15 results = np.corrcoef(arr6)
168
+
169
+ 16 print("相関係数を出力")
170
+
171
+ 17 print(results)
172
+
173
+
174
+
175
+ <__array_function__ internals> in corrcoef(*args, **kwargs)
176
+
177
+
178
+
179
+ 3 frames
128
180
 
129
181
  <__array_function__ internals> in cov(*args, **kwargs)
130
182
 
131
183
 
132
184
 
185
+ <__array_function__ internals> in average(*args, **kwargs)
186
+
187
+
188
+
133
- /usr/local/lib/python3.6/dist-packages/numpy/lib/function_base.py in cov(m, y, rowvar, bias, ddof, fweights, aweights)
189
+ /usr/local/lib/python3.6/dist-packages/numpy/core/_methods.py in _mean(a, axis, dtype, out, keepdims)
190
+
134
-
191
+ 149 is_float16_result = True
192
+
193
+ 150
194
+
195
+ --> 151 ret = umr_sum(arr, axis, dtype, out, keepdims)
196
+
197
+ 152 if isinstance(ret, mu.ndarray):
198
+
135
- 2369 m = np.asarray(m)
199
+ 153 ret = um.true_divide(
136
-
137
- 2370 if m.ndim > 2:
200
+
138
-
201
+
202
+
139
- -> 2371 raise ValueError("m has more than 2 dimensions")
203
+ TypeError: can only concatenate list (not "float") to list
140
-
141
- 2372
204
+
142
-
143
- 2373 if y is None:
144
-
145
-
146
-
147
- ValueError: m has more than 2 dimensions
148
-
149
- ```
205
+ ```

1

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

2020/03/20 23:11

投稿

dendenmushi
dendenmushi

スコア98

test CHANGED
File without changes
test CHANGED
@@ -97,3 +97,53 @@
97
97
  win10
98
98
 
99
99
  python3.7
100
+
101
+
102
+
103
+ ### 2020/03/21追記
104
+
105
+ ```python
106
+
107
+ import numpy as np
108
+
109
+ arr6 = []
110
+
111
+ arr4 = [[1.], [1.], [0.], [1.]]
112
+
113
+ arr3 = [[1.], [1.], [0.], [0.]]
114
+
115
+ arr6.append(arr4)
116
+
117
+ arr6.append(arr3)
118
+
119
+ print(arr6)
120
+
121
+ results = np.corrcoef(arr6)
122
+
123
+ ```
124
+
125
+
126
+
127
+ ```python
128
+
129
+ <__array_function__ internals> in cov(*args, **kwargs)
130
+
131
+
132
+
133
+ /usr/local/lib/python3.6/dist-packages/numpy/lib/function_base.py in cov(m, y, rowvar, bias, ddof, fweights, aweights)
134
+
135
+ 2369 m = np.asarray(m)
136
+
137
+ 2370 if m.ndim > 2:
138
+
139
+ -> 2371 raise ValueError("m has more than 2 dimensions")
140
+
141
+ 2372
142
+
143
+ 2373 if y is None:
144
+
145
+
146
+
147
+ ValueError: m has more than 2 dimensions
148
+
149
+ ```