NumPy TutorialPython NumPy Array SlicingIntroduction to Python NumPy Array SlicingPython NumPy Array SlicingArray slicing refers to extracting a portion of an array by specifying a range of indices or using a specific pattern. Slicing allows you to work with subsets of the original array without creating a copy, which can be useful for manipulating and analyzing data efficiently. Here's how you can perform array slicing in NumPy:One-Dimensional ArraysFor a one-dimensional array, slicing is similar to Python lists. You can use the colon : notation to specify the start, stop, and step size.import numpy as np# Create a 1-dimensional arrayarr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])# Slice a portion of the arraysliced_arr = arr[2:7] # Elements from index 2 to 6 (7 is exclusive)print(sliced_arr) # Output: [3 4 5 6 7]# Slice with a step sizestep_arr = arr[1:9:2] # Elements from index 1 to 8 with step size 2print(step_arr) # Output: [2 4 6 8]# Slice from the beginning to a specific indexstart_arr = arr[:5] # Elements from the beginning to index 4 (5 is exclusive)print(start_arr) # Output: [1 2 3 4 5]# Slice from a specific index to the endend_arr = arr[6:] # Elements from index 6 to the endprint(end_arr) # Output: [7 8 9 10]# Slice with negative indexingneg_arr = arr[-5:-2] # Elements from the fifth last element to the third last elementprint(neg_arr) # Output: [6 7 8]Multi-Dimensional ArraysFor multi-dimensional arrays, you can slice along each dimension separately using the comma , notation.import numpy as np# Create a 2-dimensional arrayarr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])# Slice rows and columnssliced_rows = arr[1:3, :] # Rows 1 and 2, all columnsprint(sliced_rows)# Output:# [[4 5 6]# [7 8 9]]sliced_cols = arr[:, 1:] # All rows, columns 1 and onwardsprint(sliced_cols)# Output:# [[2 3]# [5 6]# [8 9]]# Combine row and column slicingsliced_subset = arr[0:2, 1:] # Rows 0 and 1, columns 1 and onwardsprint(sliced_subset)# Output:# [[2 3]# [5 6]]Negative SlicingNegative slicing in NumPy allows you to extract elements from an array using negative indices. It provides a convenient way to access elements from the end of the array while specifying a range or pattern. Here's how you can perform negative slicing in NumPy:One-Dimensional ArraysFor a one-dimensional array, negative slicing works similar to positive slicing using the colon : notation.import numpy as np# Create a 1-dimensional arrayarr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])# Negative slicingnegative_slice = arr[-6:-2] # Elements from the sixth last element to the third last elementprint(negative_slice) # Output: [5 6 7 8]In the example above:The negative slice [-6:-2] corresponds to elements starting from the sixth last element (index -6) up to the third last element (index -2).Multi-Dimensional ArraysFor multi-dimensional arrays, negative slicing can be applied along each dimension using negative indices.import numpy as np# Create a 2-dimensional arrayarr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])# Negative slicingnegative_slice = arr[-2:, :-1] # Last two rows, all columns except the last oneprint(negative_slice)# Output:# [[4 5]# [7 8]]In the example above:The negative slice [-2:, :-1] corresponds to the last two rows (index -2 onwards) and all columns except the last one (index :-1).