# Learn NumPy for Data Science (Course II)

July 19, 2020 2020-07-27 6:00## Learn NumPy for Data Science (Course II)

### Operations on NumPy Arrays – Part 1

In the previous chapter, we learned about different ways for creating NumPy arrays. The next two chapters will focus on explaining various operations that can be performed on NumPy arrays.

In this chapter you will learn about slicing, indexing and arithmetic operations on NumPy arrays.

**Slicing and Indexing Operations on NumPy Arrays**

NumPy arrays can be sliced and indexed similar to that of Python Lists. The following examples showcase some of the slicing and indexing operations on NumPy Arrays:

import numpy as np # Creating a 1-D numpy array arr1 = np.arange(start=1, stop=10, step=1) # Print the array print("arr1: ", arr1) # Various Slicing Operations print("arr1[2:5]: ", arr1[2:5]) # Getting elements from index 2 to index 4 (upper limit is exclusive) print("arr1[2:]: ", arr1[2:]) # Getting all elements starting from index 2 print("arr1[:5]", arr1[:5]) # Getting all elements before index 5 # Various Indexing Operations print("arr1[5]: ", arr1[5]) # Getting element at index 5 print("arr1[-1]:", arr1[-1]) # Get the last element of the array

arr1: [1 2 3 4 5 6 7 8 9] arr1[2:5]: [3 4 5] arr1[2:]: [3 4 5 6 7 8 9] arr1[:5] [1 2 3 4 5] arr1[5]: 6 arr1[-1]: 9

However, for multi-dimensional NumPy arrays, the slicing and indexing operations are a bit different from what you know so far. In the following example, we will illustrate slicing and indexing operations on a 2-D NumPy array:

import numpy as np # Creating a 1-D numpy array arr1 = np.array([[1, 2, 3], [4, 5, 6]]) # Print the array print("arr1:\n", arr1) # Some Indexing operations print("arr1[0, 0]: ", arr1[0, 0]) # Get the first element of the array print("arr1[-1, -1]: ", arr1[-1, -1]) # Get the last element of the array # Some Slicing operations print("arr1[0, 1:3]: ", arr1[0, 1:3]) # Get only last two items of the first row print("arr1[1, :2]: ", arr1[1, :2]) # Get only first two items of the second row

arr1: [[1 2 3] [4 5 6]] arr1[0, 0]: 1 arr1[-1, -1]: 6 arr1[0, 1:3]: [2 3] arr1[1, :2]: [4 5]

#### Arithmetic Operations on NumPy Arrays

Various arithmetic operations such as addition, subtraction, multiplication, division, etc can be performed on NumPy arrays. Such operations can either be performed between NumPy arrays of similar shape or between a NumPy array and a number. Following are some of the examples of arithmetic operations on NumPy arrays:

import numpy as np # Creating two 1-D numpy arrays arr1 = np.arange(start=6, stop=10, step=1) arr2 = np.arange(start=1, stop=5, step=1) # Printing the arrays print("arr1: ", arr1) print("arr2: ", arr2) print("\n") # Arithmetic operations between an array and a number print("arr1 + 2: ", arr1+2) print("arr1 - 2: ", arr1-2) print("arr1 * 2: ", arr1*2) print("arr1 / 2: ", arr1/2) print("\n") # Arithmetic operations between NumPy arrays print("arr1 + arr2: ", arr1+arr2) print("arr1 - arr2: ", arr1-arr2) print("arr1 * arr2: ", arr1*arr2) print("arr1 / arr2: ", arr1/arr2)

arr1: [6 7 8 9] arr2: [1 2 3 4] arr1 + 2: [ 8 9 10 11] arr1 - 2: [4 5 6 7] arr1 * 2: [12 14 16 18] arr1 / 2: [3. 3.5 4. 4.5] arr1 + arr2: [ 7 9 11 13] arr1 - arr2: [5 5 5 5] arr1 * arr2: [ 6 14 24 36] arr1 / arr2: [6. 3.5 2.66666667 2.25 ]

This is it for slicing, indexing and arithmetic operations on NumPy arrays. Feel free to construct your own NumPy arrays (as learned from the previous chapter) and try out different operations.

The next chapter will dive deeper into more complicated operations that can be performed on NumPy arrays.