Import the NumPy library under the alias np
.
import numpy as np
Add 2
to every element of the following NumPy array.
arr = np.array([2,3,4,9,7,5,8,2])
arr+2
array([ 4, 5, 6, 11, 9, 7, 10, 4])
Double the value of every element in arr
by adding it to itself.
arr + arr
array([ 4, 6, 8, 18, 14, 10, 16, 4])
Create an array of zeros by subtracting arr
from itself.
arr - arr
array([0, 0, 0, 0, 0, 0, 0, 0])
Multiply every element in arr
by 6.
arr * 6
array([12, 18, 24, 54, 42, 30, 48, 12])
Divide every element in arr
by 4.
arr / 4
array([0.5 , 0.75, 1. , 2.25, 1.75, 1.25, 2. , 0.5 ])
Create an array of ones by dividing every element in arr
by itself.
arr / arr
array([1., 1., 1., 1., 1., 1., 1., 1.])
Calculate the square root of every element in arr
. Do not use the **
operator.
np.sqrt(arr)
array([1.41421356, 1.73205081, 2. , 3. , 2.64575131, 2.23606798, 2.82842712, 1.41421356])