2

Consider a Numpy array

x = array(
  [[4, 0, 3, 3],
   [7, 9, 3, 5],
   [2, 4, 7, 6],
   [8, 8, 1, 6]])

Consider these 3 commands

x[: , 0]
x[: , 0:1]
x[: , [0]]

The first one yields array([4, 7, 2, 8]), while the second and third return

array(
  [[4],
   [7],
   [2],
   [8]])

What explains this difference in behaviour ?

  • @Divakar How is my question a duplicate of the one you mentioned ? – Gabriel Romon Oct 07 '17 at 08:34
  • Well basically the answer is with `x[: , 0]` dims are not preserved as there is dim reduction, while for other two cases, it is. It's somewhat discussed in [`this answer post`](https://stackoverflow.com/a/18183182/) to the linked question. – Divakar Oct 07 '17 at 08:36
  • @Divakar that question isn't of any help to me. You should read posts more carefully before you press the close button and not make abusive use of your gold badge... I don't have enough rep to warrant a reopen vote. – Gabriel Romon Oct 07 '17 at 08:41
  • Maybe its a good time to start reading the [`docs`](https://docs.scipy.org/doc/numpy-1.5.x/reference/arrays.indexing.html). – Divakar Oct 07 '17 at 08:45
  • @Divakar I've asked this question specifically because I don't have time to sift through the documentation. Is your close vote **upon duplicate reason** actually a rephrasing of RTFM ? – Gabriel Romon Oct 07 '17 at 08:47
  • Well here you go, linked all questions that should solve your question. Just trying to keep the site clean. If those linked questions don't answer your question, please do explain why they don't. Appreciate your cooperation! – Divakar Oct 07 '17 at 08:54
  • Indexing with `0` (scalar) selects a column returning a 1d. `0:1` (slice) returns a 2d `view`. `[0]` (list) returns a 2d copy. That's a 'what happens?' answer. A 'why?' could get into development history, or into compiled code? Are you looking for something simple, or a deep explanation? – hpaulj Oct 07 '17 at 16:42
  • @hpaulj a simple explanation without technicalities. The elements you have provided are enlightening, thank you. If you reopened the question and posted this as an answer, I would accept it. – Gabriel Romon Oct 07 '17 at 19:14

0 Answers0