3

I have an array looks like this (two dimensions):

[[1.]
 [1.]
 [1.]
 [1.]
 [1.]
 [1.]
 [1.]
 [0.]
 [0.]
 [1.]
 [1.]
 [1.]
 [1.]
 [1.]
 [1.]
 [1.]
 [1.]
 [1.]
 [0.]
 [1.]
 [1.]
 [1.]
 [1.]
 [0.]
 [1.]]

How can I change it to: [1 1 1 1 ............1 0 1] I've been looking for solution for a whole afternoon but still got no idea, can someone give me some hint, thanks.

wawawa
  • 2,835
  • 6
  • 44
  • 105
  • `arr.ravel()` or `arr.flatten()`? – Quang Hoang Jun 15 '20 at 15:08
  • 1
    Did you spend any of that afternoon reading the basic numpy documentation? Like the quick start tutorial? – hpaulj Jun 15 '20 at 15:18
  • 1
    @High-Octane, he does call it an array, and use a `[numpy]` tag. And the display(s) is normal for `numpy` arrays, not lists. In `numpy` `reshape` (and `ravel`) produces a `view` (usually), which is fast and memory efficient. Your flattener may be nice for lists, but will be slower when applied to a numpy array (if it works at all :) ). – hpaulj Jun 15 '20 at 16:47
  • I didn't notice the numpy tag and aren't numpy arrays output tags like this ? array([5, 0, 3, 3, 7, 9]). In any case I'm at mistake here and I'll remove my comments. – High-Octane Jun 15 '20 at 16:49

1 Answers1

5

On a numpy array a:

np.squeeze(a)

or

a.flatten()
sdcbr
  • 7,021
  • 3
  • 27
  • 44