1. Import the NumPy package under the name np and Print the NumPy version and the configuration
import numpy as np
# NumPy version
np.__version__
# NumPy configuration
np.show_config()
2. Create a null vector of size 10
Answer: np.zeros(10)
3. Create Simple 1-D array and check type and check data types in array
Answer:
a = np.arange(0, 10)
# type
type(a)
# data type
a.dtype
4. How to find number of dimensions, bytes per element and bytes of memory used?
Answer:
a = np.arange(0, 10)
# No of dimensions
a.ndim
# No of bytes per element
a.itemsize
# No of bytes
a.nbytes
5. Create a null vector of size 10 but the fifth value which is 1
Answer:
null = np.zeros(10)
null[4] = 1
6. Create a vector with values ranging from 10 to 49
Answer: np.arange(10,50)
7. Reverse a vector (first element becomes last)
Answer:
b = np.arange(10,50)
b[::-1]
8. Create a 3x3 matrix with values ranging from 0 to 8
Answer:
c = np.arange(0,9)
c.reshape(3,3)
9. Find indices of non-zero elements from [1,2,0,0,4,0]
Answer:
d = np.array([1, 2, 0, 0, 4, 0])
np.nonzero(d)
10. Create a 3x3 identity matrix
Answer:
# identity() function
np.identity(3)
# eye() function
np.eye(3, 3)
11. Create a 3x3x3 array with random values
Answer: np.random.random((3, 3, 3))
12. Create a 10x10 array with random values and find the minimum and maximum values
Answer:
e = np.random.random((10,10))
# minimum value
np.min(e)
# maximum values
np.max(e)
13. Create a random vector of size 30 and find the mean value
Answer:
f = np.random.random((5,6))
# mean values of matrix
np.mean(f)
14. Create a 2d array with 1 on the border and 0 inside
Answer:
g = np.ones((5,5))
g[1:4, 1:4] = 0
15. How to add a border (filled with 0's) around an existing array?
Answer:
h = np.ones((5,5))
h[:1, :] = 0
h[:, :1] = 0
h[4:, :] = 0
h[:, 4:] = 0
16. How to Accessing/Changing specific elements, rows, columns, etc in Numpy array?
Example - [[ 1 2 3 4 5 6 7] [ 8 9 10 11 12 13 14]]
Get 13, get first row only, get 3rd column only, get [2, 4, 6], replace 13 by 20
Answer:
i = np.arange(1,15).reshape(2,7)
# To get 13
i[1,5]
# To get first row only
i[0,:]
# to get third column only
i[:,2]
# To get [2, 4, 6]
i[0,1::2]
# replace 13 by 20
i[1,5] = 20
17. How to Convert a 1D array to a 2D array with 2 rows
Answer:
j = np.arange(1,17)
# Dimension of j
j.ndim
# convert 1D to 2D
j.reshape((2,8))
18. Create the following pattern without hardcoding. Use only numpy and the below input array a.
Input:
a = np.array([1,2,3])` Desired Output:
#> array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])
Answer:
a = np.array([1, 2, 3])
np.append(np.repeat(a,3), np.tile(a,3))
19. Write a program to show how Numpy taking less memory compared to Python List?
Answer:
l = range(100)
a = 2
import sys
# Size of Python list
sys.getsizeof(a) * len(l)
# Size of numpy array
al = np.arange(100)
al.size * al.itemsize)
20. Write a program to show how Numpy taking less time compared to Python List?
Answer:
import time
import sys
# Python List
size = 10000000
l1 = range(size)
l2 = range(size)
start = time.time()
result = [(x * y) for x,y in zip(l1, l2)]
print("Time taken by List: ", (time.time() - start), "seconds")
# Numpy Array
nl1 = np.arange(size)
nl2 = np.arange(size)
start1 = time.time()
res = nl1 * nl2
print("Time taken by numpy array: ", (time.time() - start1), "seconds")