# To install this library in Jupyter notebook
#import sys
#!{sys.executable} -m pip install numpy
import numpy as np
np.__version__ , np.__path__
('1.19.5', ['/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy'])
arr = np.random.randint(low = 1, high = 100, size = 10)
print("Original Array \n", arr)
arr[2] = 333
arr[-1] = 777
print("Updated Array \n", arr)
Original Array [21 64 42 91 22 79 40 84 91 92] Updated Array [ 21 64 333 91 22 79 40 84 91 777]
# Creating 2-D array of size 4x4 of int type b/w interval 1 to 9
arr = np.random.randint(low = 1, high = 10, size = (4, 4))
print("Original Array \n", arr)
arr[0][1] = 77
arr[1][2] = 66
arr[2][-1] = 22
print("Updated Array \n", arr)
Original Array [[7 4 7 1] [3 9 2 3] [7 4 8 5] [4 5 5 9]] Updated Array [[ 7 77 7 1] [ 3 9 66 3] [ 7 4 8 22] [ 4 5 5 9]]
np.append()
method allows us to insert new values at the end of a NumPy array.np.append(arr, values, axis=None)
arr
is the array in which we want to appendvalues
must be of the correct shape (the same shape as arr
excluding axis
)axis
is not specified, both arr
and values
are flattened before use.axis
is specified, then values
must be of the correct shape (the same shape as arr
excluding axis
)arr1 = np.random.randint(low = 1, high = 100, size = 10)
print("arr1 = ", arr1)
arr1 = [27 43 66 95 39 38 42 7 25 37]
# You can add a scalar value or a list of values at the end of a 1-D array
arr2 = np.append(arr1, [101, 202,303])
print("After append:")
print("arr1 = ", arr1)
print("arr2 = ", arr2)
After append: arr1 = [27 43 66 95 39 38 42 7 25 37] arr2 = [ 27 43 66 95 39 38 42 7 25 37 101 202 303]
print(id(arr1))
print(id(arr2))
Example: In case of 2-D Arrays if axis
is not mentioned both arr
and values
are flattened before use
arr1 = np.random.randint(low = 1, high = 10, size = (3,3))
print("arr1 = \n", arr1)
arr1 = [[3 3 1] [7 7 3] [6 8 7]]
# If the axis is not mentioned, values can be of any shape and both `arr` and `values` are flattened before use.
arr2 = np.append(arr1, [101, 202,303, 404, 505])
print("After append:")
print("arr1 = \n", arr1)
print("arr2 = ", arr2)
After append: arr1 = [[3 3 1] [7 7 3] [6 8 7]] arr2 = [ 3 3 1 7 7 3 6 8 7 101 202 303 404 505]
Example: Appending a Row to a 2-D array (axis=0
)
arr1 = np.random.randint(low = 1, high = 10, size = (4,3))
print("arr1 = \n", arr1)
print("shape: ", arr1.shape)
arr1 = [[2 7 2] [8 7 3] [4 1 9] [9 8 1]] shape: (4, 3)
# For appending at axis 0, the values argument must the same shape as `arr` excluding `axis`
# so the values should be a row vector, and in this case of shape (1,3), having 1 row and 3 columns
arr2 = np.append(arr1, [[101, 202,303]], axis=0)
print("After append:")
print("arr1 = \n", arr1)
print("arr2 = \n", arr2)
After append: arr1 = [[2 7 2] [8 7 3] [4 1 9] [9 8 1]] arr2 = [[ 2 7 2] [ 8 7 3] [ 4 1 9] [ 9 8 1] [101 202 303]]
Example: Appending a Column to a 2-D array (axis=1
)
arr1 = np.random.randint(low = 1, high = 10, size = (4,3))
print("arr1 = \n", arr1)
print("shape: ", arr1.shape)
arr1 = [[4 5 6] [8 3 5] [4 6 9] [4 2 7]] shape: (4, 3)
# For appending at axis 1, the values argument must the same shape as `arr` excluding `axis`
# so the values should be a column vector of shape (4,1), having 4 rows and 1 column
arr2 = np.append(arr1, [[101], [202], [303], [404]], axis=1)
print("After append:")
print("arr1 = \n", arr1)
print("arr2 = \n", arr2)
After append: arr1 = [[4 5 6] [8 3 5] [4 6 9] [4 2 7]] arr2 = [[ 4 5 6 101] [ 8 3 5 202] [ 4 6 9 303] [ 4 2 7 404]]
np.insert()
method allows us to insert new values along the given axis before the given index.np.insert(arr, index, values, axis=None)
arr
is the array in which we want to insertindex
is the index before which we want to insertvalues
[array_like] values to be added in the arr
axis
is not specified, both arr
and values
are flattened before use.axis
is zero, a row is inserted (For 2-D arrays)axis
is one, a column is inserted (For 2-D arrays)arr1 = np.random.randint(low = 1, high = 100, size = 5)
print("arr1 = ", arr1)
arr1 = [15 58 34 72 77]
# You can insert a scalar value or a list of values in between array elements before the mentioned index
arr2 = np.insert(arr1, 3, [55, 66,77])
print("After insert:")
print("arr1 = ", arr1)
print("arr2 = ", arr2)
After insert: arr1 = [15 58 34 72 77] arr2 = [15 58 34 55 66 77 72 77]
Example: In case of 2-D array, if axis
is not mentioned the array is flattened first
arr1 = np.random.randint(low = 1, high = 10, size = (3,4))
print("arr1 = \n", arr1)
arr1 = [[5 5 1 9] [8 3 1 1] [2 7 9 2]]
# Inserting a single value
arr2 = np.insert(arr1, 4, 55)
print("After insert:")
print("arr1 = \n", arr1)
print("arr2 = ", arr2)
After insert: arr1 = [[5 5 1 9] [8 3 1 1] [2 7 9 2]] arr2 = [ 5 5 1 9 55 8 3 1 1 2 7 9 2]
# Inserting a multiple values
arr2 = np.insert(arr1, 4, [55, 66])
print("After insert:")
print("arr1 = \n", arr1)
print("arr2 = ", arr2)
Example: If axis=0, value(s) are added as a row before mentioned index
arr1 = np.random.randint(low = 1, high = 10, size = (3,4))
print("arr1 = \n", arr1)
arr1 = [[8 9 3 7] [9 8 7 7] [8 6 1 9]]
# For axis = 0, note how the scalar value is replicated before insertion
arr2 = np.insert(arr1, 2, 55, axis=0)
print("After insert:")
print("arr1 = \n", arr1)
print("arr2 = \n", arr2)
After insert: arr1 = [[8 9 3 7] [9 8 7 7] [8 6 1 9]] arr2 = [[ 8 9 3 7] [ 9 8 7 7] [55 55 55 55] [ 8 6 1 9]]
# For axis=0, note the size of values has to be 4 in this case (equal to number of columns)
arr2 = np.insert(arr1, 2, [55, 66, 77, 88], axis=0)
print("After insert:")
print("arr1 = \n", arr1)
print("arr2 = \n", arr2)
Example: If axis=1, value(s) are added as a column at mentioned index
arr1 = np.random.randint(low = 1, high = 10, size = (3,4))
print("arr1 = \n", arr1)
# For axis = 1, note how the scalar value is replicated before insertion
arr2 = np.insert(arr1, 2, 55, axis=1)
print("After insert:")
print("arr1 = \n", arr1)
print("arr2 = \n", arr2)
# For axis=1, note the size of values has to be 3 in this case (equal to number of rows)
arr2 = np.insert(arr1, 2, [55, 66, 77], axis=1)
print("After insert:")
print("arr1 = \n", arr1)
print("arr2 = \n", arr2)
np.delete()
method allows us to delete value(s) from an array at the given indexnp.delete(arr, index, axis=None)
Example:
arr1 = np.random.randint(low = 1, high = 10, size = 5)
print("arr1 = ", arr1)
# You can delete a scalar value from a specific index
arr2 = np.delete(arr1, 3)
print("After delete:")
print("arr1 = ", arr1)
print("arr2 = ", arr2)
Example:
arr1 = np.random.randint(low = 1, high = 100, size = 10)
print("arr1 = ", arr1)
# You can delete a list of values in between array elements from specific indices
arr2 = np.delete(arr1, [2,5])
print("After delete:")
print("arr1 = ", arr1)
print("arr2 = ", arr2)
Example: Delete a specific element from a 2-D array, don't mention the axis. The resulting array is flattened before use
arr1 = np.random.randint(low = 1, high = 10, size = (3,3))
print("arr1 = \n", arr1)
arr2 = np.delete(arr1, 5)
print("After delete:")
print("arr1 = \n", arr1)
print("arr2 = ", arr2)
Example: Delete a specific row from an existing 2-D array
arr1 = np.random.randint(low = 1, high = 10, size = (4,4))
print("arr1 = \n", arr1)
arr2 = np.delete(arr1, 2, axis=0)
print("After delete:")
print("arr1 = \n", arr1)
print("arr2 = \n", arr2)
Example: Delete a specific column from an existing 2-D array
arr1 = np.random.randint(low = 1, high = 10, size = (4,4))
print("arr1 = \n", arr1)
arr2 = np.delete(arr1, 2, axis=1)
print("After delete:")
print("arr1 = \n", arr1)
print("arr2 = \n", arr2)
arr1 = np.random.randint(low = 1, high = 10, size = 10)
# Creating a copy using assignment operator, both variables point at the same array
arr2 = arr1
print("arr1 = ", arr1)
print("arr2 = ", arr2)
print(id(arr1))
print(id(arr2))
arr1 = [8 6 3 4 9 3 4 3 2 9] arr2 = [8 6 3 4 9 3 4 3 2 9] 140657482813808 140657482813808
# Change value in arr1 will also occur in arr2
arr2[2] = 55
print("arr1 = ", arr1)
print("arr2 = ", arr2)
Arrays that share some data. The view method creates an object looking at the same data. Slicing an array returns a view of that array.
import numpy as np
arr1 = np.random.randint(low = 1, high = 10, size = 10)
# Creating a shallow copy (view) using slice operator
arr2 = arr1[:]
print("arr1 = ", arr1)
print("arr2 = ", arr2)
print(id(arr1))
print(id(arr2))
arr1 = [6 9 5 9 6 5 4 8 3 3] arr2 = [6 9 5 9 6 5 4 8 3 3] 140657482814368 140657482813312
# Change value in arr1 will occur in arr2
arr2[2] = 55
print("arr1 = ", arr1)
print("arr2 = ", arr2)
arr1 = [ 6 9 55 9 6 5 4 8 3 3] arr2 = [ 6 9 55 9 6 5 4 8 3 3]
arr1 = np.random.randint(low = 1, high = 10, size = 10)
# Create a Deep copy using copy() method, which will create a new copy of the array
arr2 = arr1.copy()
print("arr1 = ", arr1)
print("arr2 = ", arr2)
print(id(arr1))
print(id(arr2))
arr1 = [1 5 9 4 1 5 9 8 6 6] arr2 = [1 5 9 4 1 5 9 8 6 6] 140657482815488 140657482769472
# Change value in array 1 will NOT occur in array 2
arr2[2] = 55
print("arr1 = ", arr1)
print("arr2 = ", arr2)
arr1 = [1 5 9 4 1 5 9 8 6 6] arr2 = [ 1 5 55 4 1 5 9 8 6 6]