Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 159 - 2615_KEC
P. 159

    Program.py

                      File  Edit  Format   Run    Options   Window    Help

                      import numpy as np
                      arr = np.array([
                          [[1, 2], [3, 4]],
                          [[5, 6], [7, 8]]
                                    ])
                      print(arr[0, 1, 1])

                         Output

                      4

                     SLICING ARRAYS

                     It allows to extract a range of elements using the format start:stop:step. This can be done for
                     both 1D and multidimensional arrays which allows us to select ranges of elements or submatrices
                     easily.

                     SLICING 1D ARRAYS

                     For a 1D array, slicing returns a subset of elements between the start and stop indices.

                         Program.py

                      File  Edit  Format   Run    Options   Window    Help

                      import numpy as np
                      arr = np.array([0, 1, 2, 3, 4, 5])
                      print(arr[1:4])

                         Output

                      [1 2 3]

                     SLICING MULTIDIMENSIONAL ARRAYS

                     It can be applied to each dimension separately which allows us to extract submatrices or smaller
                     blocks of data.

                                                                                   NumPy-Creation, Selection & Operation  157

| 154 155 156 157 158 159 160 161 162 163 164 |