0

I'm working on a problem in a machine learning and I see [-1] popping up somewhat frequently in difference places of the code but I can't seem to understand the significance of it.

In this particular example, the goal is to slightly shift all images in the training set.

Here is the code:

from scipy.ndimage.interpolation import shift

def shift_image(image, dx, dy):
    image = image.reshape((28, 28))
    shifted_image = shift(image, [dy, dx], cval=0, mode="constant")
    return shifted_image.reshape([-1])

What is the significance of the -1 in the last line?

Rob
  • 7
  • 3
  • Possible duplicate of [Getting the last element of a list in Python](https://stackoverflow.com/questions/930397/getting-the-last-element-of-a-list-in-python) – Cheche Jan 09 '19 at 20:11
  • 2
    It's [explicitly documented](https://www.numpy.org/devdocs/reference/generated/numpy.reshape.html#numpy.reshape).... – roganjosh Jan 09 '19 at 20:11
  • `[12]` is a list containing one element: `12`. Same for your minus one value (cannot type it, SO prevents it) – Jean-François Fabre Jan 09 '19 at 20:11
  • 1
    Both of those dupes are very wrong – roganjosh Jan 09 '19 at 20:11
  • 1
    @JETM that's wrong... – Óscar López Jan 09 '19 at 20:11
  • "The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions." – Jean-François Fabre Jan 09 '19 at 20:13
  • From the docs "One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions." In general `-1` can have different special meanings: the last item as an index, no value as seen above, negative result in `string.find()`... – Klaus D. Jan 09 '19 at 20:14
  • To be more specific: `shifted_image.reshape([-1])` flattens the 2-d image into a 1-d array. A more "numpythonic" way to do that is `shifted_image.ravel()`. – Warren Weckesser Jan 09 '19 at 20:43

2 Answers2

2

In numpy arrays, reshape Allows you to "infer" one of the dimensions when trying to reshape an array.

import numpy as np
a = np.arange(4).reshape(2,2)
#Output:
array([[0, 1],
       [2, 3]])
a.reshape([-1])
#Output:
array([0, 1, 2, 3])

If you notice, you can also rewrite the first reshape using inference as follows:

b =np.arange(4).reshape(2,-1)
#Output:
array([[0, 1],
       [2, 3]])
Paritosh Singh
  • 6,034
  • 2
  • 14
  • 33
  • 1
    My goodness, the number of incorrect dupes and answers thrown about on this question! I'm on my phone so couldn't answer, thankfully we got there :) – roganjosh Jan 09 '19 at 20:19
  • 1
    Haha, well, better late than never eh? I'm a slow typer i'm afraid. Oh well! – Paritosh Singh Jan 09 '19 at 20:21
  • 1
    Aha! Pretty new to programming so I'm still learning the ropes when it comes to reading through the documentation. Thanks for the help! – Rob Jan 09 '19 at 20:41
1

This line:

shifted_image.reshape([-1])

Is simply calling the reshape method with a list as parameter, and the list happens to contain a single element, the number -1. This has the effect of reshaping the numpy array. From the docs:

One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions

Óscar López
  • 232,561
  • 37
  • 312
  • 386