array slicing in numpy

  • Last Update :
  • Techknowledgy :

Slicing in python means taking elements from one given index to another given index.,We pass slice instead of index like this: [start:end].,Slice elements from index 4 to the end of the array:,From both elements, slice index 1 to index 4 (not included), this will return a 2-D array:

arr = np.array([10, 15, 20, 25, 30, 35, 40])

print(arr)

Suggestion : 2

Basic slicing is an extension of Python's basic concept of slicing to n dimensions. A Python slice object is constructed by giving start, stop, and step parameters to the built-in slice function. This slice object is passed to the array to extract a part of array.,The same result can also be obtained by giving the slicing parameters separated by a colon : (start:stop:step) directly to the ndarray object.,Contents of ndarray object can be accessed and modified by indexing or slicing, just like Python's in-built container objects.,In the above example, an ndarray object is prepared by arange() function. Then a slice object is defined with start, stop, and step values 2, 7, and 2 respectively. When this slice object is passed to the ndarray, a part of it starting with index 2 up to 7 with a step of 2 is sliced.

1._
import numpy as np
a = np.arange(10)
s = slice(2, 7, 2)
print a[s]

Its output is as follows −

[2 4 6]
3._
import numpy as np
a = np.arange(10)
b = a[2: 7: 2]
print b
5._
# slice single item
import numpy as np

a = np.arange(10)
b = a[5]
print b
7._
# slice items starting from index
import numpy as np
a = np.arange(10)
print a[2: ]

Suggestion : 3

Last Updated : 05 Aug, 2021,GATE CS 2021 Syllabus

Output : 

TypeError: can 't multiply sequence by non-int of type '
list '

Output :

Array is: [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]

a[-8: 17: 1] = [12 13 14 15 16]

a[10: ] = [10 11 12 13 14 15 16 17 18 19]

Suggestion : 4

As a part of extracting elements of NumPy array from ending, we have to use negative slicing, which is extracting elements of an array from ending.,In this article, I will explain Python NumPy array slicing and how to extract some parts of data from 1-dimensional arrays, 2-dimensional arrays, and 3-dimensional arrays.,Python NumPy array slicing is used to extract some portion of data from the actual array. Slicing in python means extracting data from one given index to another given index, however, NumPy slicing is slightly different. Slicing can be done with the help of (:). A NumPy array slicing object is constructed by giving start, stop, and step parameters to the built-in slicing function. This slicing object is passed to the array to extract some portion of the array.,In this article, I have explained Python NumPy array slicing techniques for extracting elements from 1-D arrays, 2-D arrays, and 3-D arrays with examples.

1._
# Below are a quick examples

# Example 1: Create numpy arrays
arr = np.array([3, 5, 7, 9, 11, 15, 18, 22])

# Example 2: Use slicing a 1 D arrays
arr2 = arr[1: 6]

# Example 3: Slice Starting from 3 rd value to end
arr2 = arr[3: ]

# Example 4: Slice 0 to 4 index
arr2 = arr[: 5]

# Example 5: Use negative slicing
arr2 = arr[-4: -2]

# Example 6: Use step value
arr2 = arr[::3]

# Example 7: Use slicing with interval
arr2 = arr[3::4]

# Example 8: Use slicing a 2 D arrays
arr = np.array([
   [3, 5, 7, 9, 11],
   [2, 4, 6, 8, 10]
])
arr2 = arr[1: , 1: 3]

# Example 9: Slicing 3 - D arrays
arr = np.array([
   [
      [3, 5, 7, 9, 11],
      [2, 4, 6, 8, 10]
   ],
   [
      [5, 7, 8, 9, 2],
      [7, 2, 3, 6, 7]
   ]
])
arr2 = arr[0, 1, 0: 2]

arr[1:6] syntax to slice elements from index 1 to index 6 from the following 1-D array.

#
import numpy module
import numpy as np

# Create NumPy arrays
arr = np.array([3, 5, 7, 9, 11, 15, 18, 22])

# Use slicing to get 1 - D arrays elements
arr2 = arr[1: 6]
print(arr2)

# OutPut
#[5 7 9 11 15]

When we pass the elements from index 3 to the end as a parameter of the slicing operation, we will get elements of an array from index 3 to the last index.

# Starting position from 3 to the end
arr2 = arr[3: ]
print(arr2)

# Output
#[9 11 15 18 22]

Use the step value to determine the step of the slicing. It returns every other element from the entire array. Follow the syntax of slicing [start: stop: step] here, parameters are separated by a colon (:).

# Use step value to get elements
arr2 = arr[::3]
print(arr2)

# Output
#[3 9 18]

arr[-4:-2] to slice from index -4 to index -2 from the end. Minus operator to refer to an index from the end.

# Use negative slicing to get elements
arr2 = arr[-4: -2]
print(arr2)

# Output
#[11 15]

Suggestion : 5

The most common way to slice a NumPy array is by using the : operator with the following syntax:

array[start: end]
array[start: end: step]

Slicing 1D (one dimensional) arrays in NumPy can be done with the same notation as slicing regular lists in Python:

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

Output:

[2]
[1 2 3]
[1 3]

Let's print this matrix out:

[
   [1 2 3 4]
   [5 6 7 8]
   [9 10 11 12]
]

Slicing a 2D array can eighter result in an array or a matrix. The syntax that results in a matrix would be:

arr[startx: endx: stepx, starty: endy: stepy]

Suggestion : 6

Updated: January 28, 2021

avg_monthly_precip = numpy.array([0.70, 0.75, 1.85])
precip_2002_2013 = numpy.array([
   [1.07, 0.44, 1.5],
   [0.27, 1.13, 1.72]
])
# Import necessary packages
import os

import numpy as np
import earthpy as et
# Download.txt with avg monthly precip(inches)
monthly_precip_url = 'https://ndownloader.figshare.com/files/12565616'
et.data.get_data(url = monthly_precip_url)

# Download.csv of precip data
for 2002 and 2013(inches)
precip_2002_2013_url = 'https://ndownloader.figshare.com/files/12707792'
et.data.get_data(url = precip_2002_2013_url)
'/root/earth-analytics/data/earthpy-downloads/monthly-precip-2002-2013.csv'
# Set working directory to earth - analytics
os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))