How To Change Numpy Array Dimension Examples

There are several methods to reshape a Numpy array to a different dimensional array. This article will tell you how to use them with examples.

1. Use Numpy Array’s flat Attribute.

  1. The numpy.ndarray.flat attribute returns an array iterator。
  2. Then we can use the for loop to traverse each element in the array through the iterator and then create a one-dimensional array.
    import numpy as np
    
    def numpy_ndarray_flat_example():
        # the original array is a one dimension array.
        array = np.arange(15)
        print('array: ', array)
        
        # reshape it to a 3 rows & 5 columns array.
        array_reshape = array.reshape(3, 5)
        print('array_reshape: ', array_reshape)
        
        # loop in the reshaped array.
        for ele in array_reshape:
            
            print ('element in array_reshape: ', ele)
            
            print('====================')
    
        # create a new one dimension numpy array.
        array1 = np.array([])
        # loop in the reshaped 2 dimensional array's flat attribute.
        for ele in array_reshape.flat:
            # append each element to the one dimension array.
            array1 = np.append(array1, ele)
            
        print('array1: ', array1)
        
        print('array1.ndim: ', array1.ndim)
                                                   
    
    if __name__ == '__main__':
        
        numpy_ndarray_flat_example()
  3. When you run the above example, you will get the below result.
    array:  [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
    array_reshape:  [[ 0  1  2  3  4]
     [ 5  6  7  8  9]
     [10 11 12 13 14]]
    element in array_reshape:  [0 1 2 3 4]
    ====================
    element in array_reshape:  [5 6 7 8 9]
    ====================
    element in array_reshape:  [10 11 12 13 14]
    ====================
    array1:  [ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14.]
    array1.ndim:  1

2. Use numpy.ndarray.flatten() Method.

  1. The numpy.ndarray.flatten() method returns a copy of an array in the form of a one-dimensional array.
  2. You can make operations on the copy and it will not affect the original array. Below is the flatten() method syntax.
    ndarray.flatten(order='C')
  3. Below is the example source code.
    import numpy as np
    
    def numpy_ndarray_flatten_example():
        # the original array is a one dimension array.
        array = np.arange(15)
        print('array: ', array)
        
        # reshape it to a 3 rows & 5 columns array.
        array_reshape = array.reshape(3, 5)
        print('array_reshape: ', array_reshape)
        
        # The default is to sort the expanded array in row order.
        array_flatten_c_order = array_reshape.flatten()
        print('array_reshape.flatten(): ', array_flatten_c_order)
        
        # Array expanded in column order.
        array_flatten_f_order = array_reshape.flatten(order = 'F')
        print ('array_reshape.flatten(order = \'F\')', array_flatten_f_order)
        
                                                   
    
    if __name__ == '__main__':
        
        numpy_ndarray_flatten_example()
  4. Below is the above example execution output.
    array:  [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
    array_reshape:  [[ 0  1  2  3  4]
     [ 5  6  7  8  9]
     [10 11 12 13 14]]
    array_reshape.flatten():  [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
    array_reshape.flatten(order = 'F') [ 0  5 10  1  6 11  2  7 12  3  8 13  4  9 14]

3. Use numpy.ravel() Method.

  1. The numpy.ravel() method expands the elements of a multidimensional array as a one-dimensional array.
  2. This method returns a view of the source array, if modified, it will affect the original array.
  3. It has the below syntax.
    numpy.ravel(array, order='C')
  4. Below is the example source code.
    import numpy as np
    
    def numpy_ndarray_ravel_example():
        # the original array is a one dimension array.
        array = np.arange(15)
        print('array: ', array)
        
        # reshape it to a 3 rows & 5 columns array.
        array_reshape = array.reshape(3, 5)
        print('array_reshape: ', array_reshape)
        
        # The default is to sort the expanded array in row order.
        array_ravel_c_order = array_reshape.ravel()
        print('array_reshape.ravel(): ', array_ravel_c_order)
        
        # Array expanded in column order.
        array_ravel_f_order = array_reshape.ravel(order = 'F')
        print ('array_reshape.ravel(order = \'F\')', array_ravel_f_order)                                               
    
    if __name__ == '__main__':
        
        numpy_ndarray_ravel_example()
  5. Below is the above source code execution result.
    array:  [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
    array_reshape:  [[ 0  1  2  3  4]
     [ 5  6  7  8  9]
     [10 11 12 13 14]]
    array_reshape.ravel():  [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
    array_reshape.ravel(order = 'F') [ 0  5 10  1  6 11  2  7 12  3  8 13  4  9 14]

4. Use Numpy Array’s reshape() Function.

  1. The Numpy reshape() function can reshape a NumPy array to another dimension array, below is the example.
  2. Open a terminal and input the command python.
  3. Then run the below python source code in it, you can see the 1-dimensional array has been reshaped to a 2-dimensional array with 3  rows and 5 columns.
    C:\Users\zhaosong>python
    >>>
    >>> import numpy as np
    >>>
    >>> array = np.arange(15)
    >>>
    >>> array
    array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
    >>>
    >>> array_reshape = array.reshape(3, 5)
    >>>
    >>> array_reshape
    array([[ 0,  1,  2,  3,  4],
           [ 5,  6,  7,  8,  9],
           [10, 11, 12, 13, 14]])

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Clicky