Numpy is Numerical Python, an open source data science library of python which is used mainly for the arrays, matrices calculation. This library provides lot of supporting functions which make the complex mathematical calculation of arrays very easy. Numpy are often used with other python libraries like pandas, scipy and matplotlib to solve complex problem in short span of time and show the result accurately.
Below are the set of multiple questions which will help to give the basic knowledge of all type of programming of Numpy Data science library in Python. You can visit website https://www.w3resource.com/python-exercises/numpy/ orhttps://www.geeksforgeeks.org/python-numpy-practice-exercises-questions-and-solutions/
Steps to run below programs:
- Install Anaconda in your PC/ Laptop.
- Open Spyder.
- Copy and paste below programs.
- Press Run button. You can see the result in Console section at right side.
#1. Declaration of Numpy , create arrays of zeros, ones, fifties
import numpy as np
arr = np.zeros(5)
print("Zeros array in numpy are:", arr)
arr_z= np.zeros(shape=(2,3), dtype='int')
print("Zeros array with diff shape in numpy are:",arr_z)
arr1 = np.ones(5)
print("Ones array in numpy are:",arr1)
arr2 = np.ones(5)*50
print("50th value array in numpy are:", arr2)
#2. creation of random numbers b/w 0 and 1
rand= np.random.normal(0,1,1)
print("Random values from 0 to 1 are: ", rand)
rand1 = np.random.normal(0,1,25)
print("25 Random values from 0 to 1 are: ",rand1)
#3. create a vector with values ranging from 10 to 19, with reshaping
vec= np.arange(10,19)
print("Vector with values from 10 to 19 are: ", vec)
vec_rev= vec[::-1]
print("Reversing of vector: ", vec_rev)
vec1 = vec.reshape(3,3)
print("Reshaping of Vector \n: ", vec1)
print ('\n')
#4. creation of identity matrix
Iden= np.eye(3)
print("Identity Matrix is \n:", Iden)
print ('\n')
#5. create 3*3 matrix with values ranging from 2 to 10
mat = np.arange(2, 11).reshape(3,3)
print("3*3 matrix with values ranging from 2 to 10 \n:", (mat))
print ('\n')
#6. Write a NumPy program to convert a list and tuple into arrays.
my_list = [1, 2, 3, 4, 5, 6, 7, 8]
print("List to array: ")
print(np.asarray(my_list))
my_tuple = ([8, 4, 6], [1, 2, 3])
print("Tuple to array: ")
print(np.asarray(my_tuple))
#7. Write a NumPy program to append values to the end of an array.
import numpy as np
x = [10, 20, 30]
print("Original array:")
print(x)
x = np.append(x, [[40, 50, 60], [70, 80, 90]])
print("After append values to the end of the array:")
print(x)
#8. Write a NumPy program to construct an array by repeating.
#Sample array: [1, 2, 3, 4]
a = [1, 2, 3, 4]
print("Original array:")
print(a)
print("2 times repeating the same array:")
x = np.tile(a, 2)
print(x)
print("3 times repeating the same array:")
x = np.tile(a, 3)
print(x)
#9. Write a NumPy program to change the dimension of an array.
x = np.array([1, 2, 3, 4, 5, 6])
print("Shape of array is: ", x.shape)
print("Size of array is: ", x.size)
y = np.array([[1, 5, 2],[6, 7, 1],[3,8,4]])
print("Make array of 3 rows and 3 colums \n :",y)
print ('\n')
x = np.array([5,3,11,4,5,9,7,25,19])
x.shape = (3, 3)
print("Change shape of given array into 3 rows and 3 columns \n: ", x)
print ('\n')
#10. Mathematical practice for Numpy
print("Addition: ", np.add(55, 30))
print("Subtraction: ", np.subtract(55, 30))
print("Multiplication :", np.multiply(55, 30))
print("Division :", np.divide(55,30))
expo = np.arange(5)
print("Originial term are: ", expo)
expo_result = np.power(expo,3)
print("Originial term are: ", expo_result)
abs =np.array([-2.3, 6.8, -9.1])
abs_result = np.absolute(abs)
print("Absolute Value are: ", abs_result)
#11. Round array elements of given decimals to nearest value
round_element = np.round([1.6, 2.4, 0.34, 7.8, 8.1])
print("Round Values of given array are: ",round_element)
#12. find max and min value of array
arr_max_min = np.arange(6).reshape((2,3))
print(arr_max_min)
print("Max value of array \n: ",(np.amax(arr_max_min)))
print ('\n')
print("Min value of array \n: ", (np.amin(arr_max_min)))
print ('\n')
print("Sum of all the elements \n: ", (arr_max_min.sum()))
print ('\n')
#13. Matrix addition and multiplication of two arrays
mat1 = [[4, 5], [2,5], [6,2]]
mat2 = [[5,7,1], [6,9,3]]
print("Matrix multiplication \n: ", (np.matmul(mat1,mat2)))
print ('\n')
#14. Another method of addition, multiplication, Division, Concatenation (Vertical stacking and horizontal stacking)
mat1_np = np.array([[4, 5], [2,5]])
mat2_np = np.array([[5,7], [6,9]])
print("Another method of Matrix addition \n: ", mat1_np + mat2_np)
print ('\n')
print("Another method of Matrix multiplication \n: ", mat1_np * mat2_np)
print ('\n')
print("Another method of Matrix Division \n: ", mat1_np / mat2_np)
print ('\n')
print("Concatenation, Vertical stacking: \n :", np.vstack((mat1_np,mat2_np)));
print ('\n')
print("Concatenation, horizontally stacking \n :", np.hstack((mat1_np,mat2_np)));
print ('\n')
#15. calculate inverse sine value (trignometric function)
sine_value = np.array([-1., 0, 1.])
print("Inverse Sine Value: ", np.arcsin(sine_value))
print ('\n')
#16. Array Iteration
mat_iter = np.array([[5,7], [6,9]])
print("Array before Iterating: /n", mat_iter)
print("Array after Iterating: /n")
for x in np.nditer(mat_iter):
print(x, end=' ')
print ('\n')
#17. Transpose of Array
mat_Trans = np.array([[5,7], [6,9]])
print("Transpose of array: \n", mat_Trans.T)
print ('\n')
#18. Find mean, median, average of array
m = np.array([[4,7,5],[6,2,8],[3,1,7]])
print("Original array: \n", m)
print('\n')
print("Mean of array along axis 0 :", np.mean(m,0))
print("Median of array along axis 1 :", np.median(m,0))
print("Average of array along axis 1 :", np.average(m,1))
print("\n")
Result of above Exercise
Zeros array in numpy are: [0. 0. 0. 0. 0.]
Zeros array with diff shape in numpy are: [[0 0 0]
[0 0 0]]
Ones array in numpy are: [1. 1. 1. 1. 1.]
50th value array in numpy are: [50. 50. 50. 50. 50.]
Random values from 0 to 1 are: [0.1241432]
25 Random values from 0 to 1 are: [ 0.62204881 -0.82931417 0.23603654 -1.29256341 -1.68773158 0.09786349
-0.63253742 0.64259035 1.60333346 0.27708779 -0.36226831 0.094398
-1.31297426 0.29043894 0.37204664 0.06414133 -0.96859801 -0.59008381
0.19558017 -0.13458364 0.03206455 -0.89067194 -0.56143579 0.19745269
0.04995617]
Vector with values from 10 to 19 are: [10 11 12 13 14 15 16 17 18]
Reversing of vector: [18 17 16 15 14 13 12 11 10]
Reshaping of Vector
: [[10 11 12]
[13 14 15]
[16 17 18]]
Identity Matrix is
: [[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
3*3 matrix with values ranging from 2 to 10
: [[ 2 3 4]
[ 5 6 7]
[ 8 9 10]]
List to array:
[1 2 3 4 5 6 7 8]
Tuple to array:
[[8 4 6]
[1 2 3]]
Original array:
[10, 20, 30]
After append values to the end of the array:
[10 20 30 40 50 60 70 80 90]
Original array:
[1, 2, 3, 4]
2 times repeating the same array:
[1 2 3 4 1 2 3 4]
3 times repeating the same array:
[1 2 3 4 1 2 3 4 1 2 3 4]
Shape of array is: (6,)
Size of array is: 6
Make array of 3 rows and 3 colums
: [[1 5 2]
[6 7 1]
[3 8 4]]
Change shape of given array into 3 rows and 3 columns
: [[ 5 3 11]
[ 4 5 9]
[ 7 25 19]]
Addition: 85
Subtraction: 25
Multiplication : 1650
Division : 1.8333333333333333
Originial term are: [0 1 2 3 4]
Originial term are: [ 0 1 8 27 64]
Absolute Value are: [2.3 6.8 9.1]
Round Values of given array are: [2. 2. 0. 8. 8.]
[[0 1 2]
[3 4 5]]
Max value of array
: 5
Min value of array
: 0
Sum of all the elements
: 15
Matrix multiplication
: [[50 73 19]
[40 59 17]
[42 60 12]]
Another method of Matrix addition
: [[ 9 12]
[ 8 14]]
Another method of Matrix multiplication
: [[20 35]
[12 45]]
Another method of Matrix Division
: [[0.8 0.71428571]
[0.33333333 0.55555556]]
Concatenation, Vertical stacking:
: [[4 5]
[2 5]
[5 7]
[6 9]]
Concatenation, horizontally stacking
: [[4 5 5 7]
[2 5 6 9]]
Inverse Sine Value: [-1.57079633 0. 1.57079633]
Array before Iterating: /n [[5 7]
[6 9]]
Array after Iterating: /n
5 7 6 9
Transpose of array:
[[5 6]
[7 9]]
Original array:
[[4 7 5]
[6 2 8]
[3 1 7]]
Mean of array along axis 0 : [4.33333333 3.33333333 6.66666667]
Median of array along axis 1 : [4. 2. 7.]
Average of array along axis 1 : [5.33333333 5.33333333 3.66666667]
No comments:
Post a Comment