The document explains NumPy array slicing, detailing how to extract and modify portions of 1D and 2D arrays using start, stop, and step parameters. It includes examples of slicing with both positive and negative indices, as well as reversing arrays. Additionally, it covers the syntax for slicing 2D arrays, emphasizing the need to specify slices for both rows and columns.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
35 views
NumPy Array Slicing Notes
The document explains NumPy array slicing, detailing how to extract and modify portions of 1D and 2D arrays using start, stop, and step parameters. It includes examples of slicing with both positive and negative indices, as well as reversing arrays. Additionally, it covers the syntax for slicing 2D arrays, emphasizing the need to specify slices for both rows and columns.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2
NumPy Array Slicing 1.
Using start Parameter
Array Slicing is the process of extracting a portion import numpy as np of an array. # create a numpy array With slicing, we can easily access elements in the numbers = np.array([2, 4, 6, 8, 10, 12]) array. It can be done on one or more dimensions of # modify elements from index 3 onwards a NumPy array. numbers[3:] = 20 print(numbers) Syntax of NumPy Array Slicing Here's the syntax of array slicing in NumPy: # Output: [ 2 4 6 20 20 20] array[start:stop:step] Here, numbers[3:] = 20 replaces all the elements Here, from index 3 onwards with new value 20. start - index of the first element to be 2. Using stop Parameter included in the slice import numpy as np stop - index of the last element (exclusive) step - step size between each element in # create a numpy array the slice numbers = np.array([2, 4, 6, 8, 10, 12]) Note: When we slice arrays, the start index is inclusive but the stop index is exclusive. # modify the first 3 elements If we omit start, slicing starts from the first numbers[:3] = 40 element print(numbers) If we omit stop, slicing continues up to the last element # Output: [40 40 40 8 10 12] If we omit step, default step size is 1 Here, numbers[:3] = 20 replaces the first 3 elements with the new value 40. 1D NumPy Array Slicing 3. Using start and stop parameter In NumPy, it's possible to access the portion of an import numpy as np array using the slicing operator :. For example, # create a numpy array import numpy as np numbers = np.array([2, 4, 6, 8, 10, 12]) # create a 1D array # modify elements from indices 2 to 5 array1 = np.array([1, 3, 5, 7, 8, 9, 2, 4, 6]) numbers[2:5] = 22 # slice array1 from index 2 to index 6 (exclusive) print(numbers) print(array1[2:6]) # [5 7 8 9] # slice array1 from index 0 to index 8 (exclusive) # Output: [2 4 22 22 22 12] with a step size of 2 Here, numbers[2:5] = 22 selects elements from print(array1[0:8:2]) # [1 5 8 2] index 2 to index 4 and replaces them with new # slice array1 from index 3 up to the last element value 22. print(array1[3:]) # [7 8 9 2 4 6] 4. Using start, stop, and step parameter # items from start to end import numpy as np print(array1[:]) # [1 3 5 7 8 9 2 4 6] # create a numpy array In the above example, we have created the array numbers = np.array([2, 4, 6, 8, 10, 12]) named array1 with 9 elements. # modify every second element from indices 1 to 5 Then, we used the slicing operator : to slice array numbers[1:5:2] = 16 elements. print(numbers) array1[2:6] - slices array1 from index 2 to # Output: [ 2 16 6 16 10 12] index 6, not including index 6 In the above example, array1[0:8:2] - slices array1 from numbers[1:5:2] = 16 index 0 to index 8, not including index 8 modifies every second element from index 1 to array1[3:] - slices array1 from index 3 up index 5 with a new value 16. to the last element array1[:] - returns all items from NumPy Array Negative Slicing beginning to end We can also use negative indices to perform negative slicing in NumPy arrays. During negative Modify Array Elements Using Slicing slicing, elements are accessed from the end of the With slicing, we can also modify array elements array. using: Let's see an example. start parameter import numpy as np stop parameter # create a numpy array start and stop parameter numbers = np.array([2, 4, 6, 8, 10, 12]) start, stop, and step parameter # slice the last 3 elements of the array # using the start parameter print(array1[:2, :2]) print(numbers[-3:]) # [8 10 12] # slice elements from 2nd-to-last to 4th-to-last # Output element [[ 1 3] # using the start and stop parameters [ 9 11]] print(numbers[-5:-2]) # [4 6 8] Here, the , in [:2, :2] separates the rows of the # slice every other element of the array from the array. end The first :2 returns first 2 rows i.e., entire array1. # using the start, stop, and step parameters This results in print(numbers[-1::-2]) # [12 8 4] [1 3] Output The second :2 returns first 2 columns from the 2 Using numbers[-3:]- [ 8 10 12] rows. This results in Using numbers[-5:-2]- [4 6 8] [9 11] Using numbers[-1::-2]- [12 8 4] Here, Example: 2D NumPy Array Slicing numbers[-3:] - slices last 3 elements import numpy as np of numbers # create a 2D array numbers[-5:-2] - slices numbers elements array1 = np.array([[1, 3, 5, 7], from 5th last to 2nd last(excluded) [9, 11, 13, 15], numbers[-1::-2] - slices every [2, 4, 6, 8]]) other numbers elements from the end with step size 2 # slice the array to get the first two rows and Reverse NumPy Array Using Negative Slicing columns In NumPy, we can also reverse array elements subarray1 = array1[:2, :2] using the negative slicing. For example, # slice the array to get the last two rows and import numpy as np columns # create a numpy array subarray2 = array1[1:3, 2:4] numbers = np.array([2, 4, 6, 8, 10, 12]) # print the subarrays # generate reversed array print("First Two Rows and Columns: reversed_numbers = numbers[::-1] \n",subarray1) print(reversed_numbers) print("Last two Rows and Columns: \n",subarray2) Output # Output: [12 10 8 6 4 2] First Two Rows and Columns: Here, the slice numbers[::-1] selects all the [[ 1 3] elements of the array with a step size of -1, which [ 9 11]] reverses the order of the elements. Last two Rows and Columns: [[13 15] 2D NumPy Array Slicing [ 6 8]] A 2D NumPy array can be thought of as a matrix, Here, where each element has two indices, row index and array1[:2, :2] - slices array1 that starts at column index. the first row and first column (default To slice a 2D NumPy array, we can use the same values), and ends at the second row and syntax as for slicing a 1D NumPy array. The only second column (exclusive) difference is that we need to specify a slice for each array1[1:3, 2:4] - slices array1 that starts dimension of the array. at the second row and third column Syntax of 2D NumPy Array Slicing (index 1 and 2), and ends at the third row array[row_start:row_stop:row_step, and fourth column (index 2 and 3) col_start:col_stop:col_step] Here, row_start,row_stop,row_step - specifies starting index, stopping index, and step size for the rows respectively col_start,col_stop,col_step - specifies starting index, stopping index, and step size for the columns respectively Let's understand this with an example. # create a 2D array array1 = np.array([[1, 3, 5, 7], [9, 11, 13, 15]])
(Chapman & Hall_CRC Series in Operations Research) Vincent Knight, Geraint Palmer - Applied Mathematics With Open-Source Software_ Operational Research Problems With Python and R-Chapman and Hall_CRC