Reshaping arrays

NumPy makes it simple to change the shape of your arrays. Earlier in this chapter, we briefly saw the .reshape() method of the NumPy array and how it can be used to reshape a one-dimensional array into a matrix. It is also possible to convert from a matrix back to an array. The following example demonstrates this by creating a nine-element array, reshaping it into a 3 x 3 matrix, and then back to a 1 x 9 array:

In [47]:
   # create a 9 element array (1x9)
   a = np.arange(0, 9)
   # and reshape to a 3x3 2-d array
   m = a.reshape(3, 3)
  m

Out[47]:
   array([[0, 1, 2],
          [3, 4, 5],
          [6, 7, 8]])

In [48]:
   # and we can reshape downward in dimensions too
   reshaped = m.reshape(9)
   reshaped

Out[48]:
   array([0, 1, 2, 3, 4, 5, 6, 7, 8])

Note

Note that .reshape() returns a new array with a different shape. The original array's shape remains unchanged.

The .reshape() method is not the only means of reorganizing data. Another means is the .ravel() method that will flatten a matrix to one dimension as shown in the following example:

In [49]:
   # .ravel will generate array representing a flattened 2-d array
   raveled = m.ravel()
   raveled

Out[49]:
   array([0, 1, 2, 3, 4, 5, 6, 7, 8])

In [50]:
   # it does not alter the shape of the source
   m

Out[50]:
   array([[0, 1, 2],
          [3, 4, 5],
          [6, 7, 8]])

The preceding code has performed the same operation as using the previous .reshape() example, but without the need to pass the number of items in the matrix. Again, the shape of the original matrix is unchanged.

Even though .reshape() and .ravel() do not change the shape of the original array or matrix, they do actually return a one-dimensional view into the specified array or matrix. If you change an element in this view, the value in the original array or matrix is changed. The following example demonstrates this ability to change items of the original matrix through the view:

In [51]:
   # but it will be a view into the source
   # so items changed in the result of the ravel
   # are changed in the original object
   # reshape m to an array
   reshaped = m.reshape(np.size(m))
   # ravel into an array
   raveled = m.ravel()
   # change values in either
   reshaped[2] = 1000
   raveled[5] = 2000
   # and they show as changed in the original
   m

Out[51]:
   array([[   0,    1, 1000],
          [   3,    4, 2000],
          [   6,    7,    8]])

The .flatten() method functions similarly to .ravel() but instead returns a new array with copied data instead of a view. Changes to the result do not change the original matrix:

In [52]:
   # flattened is like ravel, but a copy of the data,
   # not a view into the source
   m2 = np.arange(0, 9).reshape(3,3)
   flattened = m2.flatten()
   # change in the flattened object
   flattened[0] = 1000
   flattened

Out[52]:
   array([1000,    1,    2,    3,    4,    5,    6,    7,    8])

In [53]:
   # but not in the original
   m2

Out[53]:
   array([[0, 1, 2],
          [3, 4, 5],
          [6, 7, 8]])

The .shape property returns a tuple representing the shape of the array:

In [54]:
   # we can reshape by assigning a tuple to the .shape property
   # we start with this, which has one dimension
   flattened.shape

Out[54]:
   (9,)

The property can also be assigned a tuple, which will force the array to reshape itself as specified:

In [55]:
   # and make it 3x3
   flattened.shape = (3, 3)
   # it is no longer flattened
   flattened

Out[55]:
   array([[1000,    1,    2],
          [   3,    4,    5],
          [   6,    7,    8]])

In linear algebra, it is common to transpose a matrix. This can be performed with the .transpose() method, as shown here:

In [56]:
   # transpose a matrix
   flattened.transpose()

Out[56]:
   array([[1000,    3,    6],
          [   1,    4,    7],
          [   2,    5,    8]])

Alternatively, this can also be performed with the .T property:

In [57]:
   # can also use .T property to transpose
   flattened.T

Out[57]:
   array([[1000,    3,    6],
          [   1,    4,    7],
          [   2,    5,    8]])

The .resize() method functions similarly to the .reshape() method, except that while reshaping returns a new array with data copied into it, .resize() performs an in-place reshaping of the array.:

In [58]:
   # we can also use .resize, which changes shape of
   # an object in-place
   m = np.arange(0, 9).reshape(3,3)
   m.resize(1, 9)
   m # my shape has changed

Out[58]:
   array([[0, 1, 2, 3, 4, 5, 6, 7, 8]])
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.61.170