Uploaded by Nitin Khetade

Shraddha Kale(ISCT-4)

advertisement
Shraddha Kale
112005022
Div. 7
Part A
Que1. Use NumPy to generate an array of 25 random numbers sampled from a
standard normal distribution.
Solution:
Inputimport numpy as np
a = np.random.normal(0,1,25)
print("Array of 25 random No.-: ")
print(a)
OutputArray of 25 random No.-:
[-1.02418449 -0.58975288 0.73706266 1.32010955 -0.8365482 1.7
5662171
-3.60192736 -1.67521822 -0.34010706 0.67558589 0.3170328 0.5
6493777
0.54984462 0.2527343 -0.65417579 -0.46221818 -0.54857458 -0.1
3920322
0.00937948 0.69474047 -0.17474956 -0.75560188 -1.75618595 -0.
87186494
1.13200534]
Que2. Create an array of 20 linearly spaced points between 0 and 1.
Solution:
Inputimport numpy as np
a = np.linspace(0, 1, 20)
print("Array of 20 linearly points.-: ")
print(a)
OutputArray of 20 linearly points.-:
[0.
0.05263158 0.10526316 0.15789474 0.21052632 0.26315789
0.31578947 0.36842105 0.42105263 0.47368421 0.52631579 0.5789
4737
0.63157895 0.68421053 0.73684211 0.78947368 0.84210526 0.8947
3684
0.94736842 1.
]
Que3. Write a code to generate matrix given below
Then do the following:
1. Get the standard deviation of the values in mat
2. Get the sum of all the columns in mat
Solution:
Inputimport numpy as np
a = np.arange(1,26).reshape(5,5)
print("Martix of array: ")
print(a)
print("Part A:")
b = np.std(a)
print("Standerd deviation of matrix: ", b)
print("Part B:")
c = np.sum(a)
print("Sum of matrix: ", c)
OutputMartix of array:
[[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]
[16 17 18 19 20]
[21 22 23 24 25]]
Part A:
Standerd deviation of matrix: 7.211102550927978
Part B:
Sum of matrix: 325
Que4. Let X = np.array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]). Get the diagonal of
X, that is, [0, 5, 10].
Solution:
Inputimport numpy as np
a = np.array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
print("The matrix is- ",a)
b = np.diag(a)
print("The digonal matrix: ",b)
OutputThe matrix is- [[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
The digonal matrix: [ 0 5 10]
Que5. Create a 2-D array whose diagonal equals [1, 2, 3, 4] and 0's elsewhere.
Solution:
Inputimport numpy as np
x = np.diagflat([1, 2, 3, 4])
print("The digonal matrix: ",x)
OutputThe digonal matrix: [[1 0 0 0]
[0 2 0 0]
[0 0 3 0]
[0 0 0 4]]
Que6. Create an array which looks like below. array([[ 1, 2, 3], [ 4, 5, 6], [ 0, 8, 9], [
0, 0, 12]]).
Solution:
Inputimport numpy as np
x = np.array(a)
a = ([[ 1, 2, 3], [ 4, 5, 6], [ 0, 8, 9], [ 0, 0, 12]])
print("The matrix is: ",x)
OutputThe matrix is: [[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Que7. Create a 5 X 2 integer array from a range between 100 to 200 such that the
difference between each element is 10.
Solution:
Inputimport numpy as np
a = np.arange(100, 200, 10).reshape(5,2)
print("The array will be: ",a)
OutputThe array will be: [[100 110]
[120 130]
[140 150]
[160 170]
[180 190]]
Que8. Write a program to check whether specified values are present in NumPy a
rray?
Solution:
Inputimport numpy as np
a= np.array([[1, 3, 5, 7],[9, 11, 15, 17]])
print("Given array:",a)
print(2 in a)
print(1 in a)
print(6 in a)
print(5 in a)
print(14 in a)
print(17 in a)
OutputGiven array: [[ 1 3 5 7]
[ 9 11 15 17]]
False
True
False
True
False
True
Que9. Write a program to calculate the sum of the diagonal elements of a NumPy
array?
Solution:
Inputimport numpy as np
a=np.arange(12).reshape(4,3)
print("The matrix: \n",a)
b=np.trace(a)
print("The sum of diagonal elements of matrix :", b)
OutputThe matrix:
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
The sum of diagonal elements of matrix : 12
Que10. Write a program to Calculate the determinant of a matrix using NumPy?
Solution:
Inputimport numpy as np
a = np.array([[6, 2], [3, 4]])
print("Numpy Matrix is:",a)
det = np.linalg.det(a)
print("Determinant of given 2X2 matrix:")
print(int(det))
OutputNumpy Matrix is: [[6 2]
[3 4]]
Determinant of given 2X2 matrix:
17
Que11. Write a NumPy program to create an element-wise comparison (greater,
greater than or equal, less, and less than or equal) of two given arrays.
Solution:
Inputimport numpy as np
a = np.array([1, 3])
b = np.array([4, 2])
print("The given matrises are: ", a, b)
print("1.greater")
print(np.greater(a, b))
print("2.greater than orequal")
print(np.greater_equal(a, b))
print("3.less")
print(np.less(a, b))
print("4.less than or equal")
print(np.less_equal(a, b))
OutputThe given matrises are: [1 3] [4 2]
1.greater
[False True]
2.greater than orequal
[False True]
3.less
[ True False]
4.less than or equal
[ True False]
Que12. Write a NumPy program to create a 10x10 matrix, in which the elements
on the borders will be equal to 1, and inside 0.
Solution:
Inputimport numpy as np
a = np.ones((10, 10))
a[1:-1, 1:-1] = 0
print("The matrix will be: ", a)
OutputThe matrix will be: [[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
Part B
Que1. Numpy Array construction:
Create 5 by 10 array with all elements random between 50 and 100.
Solution:
Inputimport numpy as np
a = np.random.uniform(50, 100, 50).reshape(5,10)
print("The array: ", a)
OutputThe array: [[67.73047821 98.97794505 70.93869071 61.05655996 93.70802342 84.
08390008
51.26927424 66.43299111 54.56059563 87.74983369]
[69.42606347 63.57198462 52.25570299 82.75943691 81.91170793 61.88779821
81.03575319 72.91173145 96.14635297 66.08331858]
[62.99287805 60.72144687 87.18012147 69.79705763 62.40935986 72.25703561
94.73357914 60.11295609 55.21648884 96.59159395]
[57.92218273 70.82609821 91.67749037 57.5455357 88.03530794 95.76815749
69.26824313 70.70168015 86.37918265 81.87403132]
[94.85498654 61.83253609 90.89260694 54.74341448 86.53079673 87.14732804
80.14337201 90.17663066 92.03303769 59.45585051]]
Que2. Numpy Array construction:
Create 5 by 10 array with all elements random between 50 and 100 separated by
0.01.
Solution:
Inputimport numpy as np
a = np.random.uniform(50,100)
b = np.arange(a,a+0.5,0.01).reshape(5,10)
print("The array will be ", b)
OutputThe array will be [[52.12066925 52.13066925 52.14066925 52.15066925 52.1606
6925 52.17066925
52.18066925 52.19066925 52.20066925 52.21066925]
[52.22066925 52.23066925 52.24066925 52.25066925 52.26066925 52.2706692
5
52.28066925 52.29066925 52.30066925 52.31066925]
[52.32066925 52.33066925 52.34066925 52.35066925 52.36066925 52.3706692
5
52.38066925 52.39066925 52.40066925 52.41066925]
[52.42066925 52.43066925 52.44066925 52.45066925 52.46066925 52.4706692
5
52.48066925 52.49066925 52.50066925 52.51066925]
[52.52066925 52.53066925 52.54066925 52.55066925 52.56066925 52.5706692
5
52.58066925 52.59066925 52.60066925 52.61066925]]
Que3.
Create a numpy array that contain integers i such that 0 < i < 100 and 2i has the
last digit 6.
Solution:
Inputimport numpy as np
a = np.array([2**i for i in range(0,100) if (2**i)%10==6])
print("The No. will be: ", a)
OutputThe No. will be: [16 256 4096 65536 1048576 16777216 268435456 4294967296
68719476736
1099511627776 17592186044416 281474976710656 4503599627370496
72057594037927936 1152921504606846976 18446744073709551616
295147905179352825856 4722366482869645213696 755578637259143234191
36
1208925819614629174706176 19342813113834066795298816
309485009821345068724781056 4951760157141521099596496896
79228162514264337593543950336]
Que4.
Create an array of first 10 powers of 2.
Solution:
Inputn_powers = 10
list = []
for i in range(n_powers):
list.append(2 ** i)
print(list)
Output[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
Que5.
Use all the slicing methods for each dimension of the given mutidimensional array
First of all, create a mutidimensional array as shown in the figure below and then
write your code to answer from 1) to 5)
1) retrieve 3,4 as shown in red box
2) retrieve all elements of column indexed with 2 (output
should be 2,12,22,32, 42, 52
3) retrieve row indexed with 2 and a row indexed with 4 at
a time
4) retrieve element 33
5) retrieve the elements as shown in the green box
Solution:
Inputimport numpy as np
a = np.array([[0, 1, 2, 3, 4, 5], [10, 11, 12, 13, 14, 15], [20, 21, 22, 23,
24, 25], [30, 31, 32, 33, 34, 35], [40, 41, 42, 43, 44, 45], [50, 51, 52,
53, 54, 55]])
print("The given array: ",a)
print("1. ", a[0, 3:5])
print("2. ", a[:, 2])
print("3. ", a[2::2, ::2])
print("4. ", a[3, 3:4])
print("5. ", a[4:, 4:])
OutputThe given array: [[ 0 1 2 3 4 5]
[10 11 12 13 14 15]
[20 21 22 23 24 25]
[30 31 32 33 34 35]
[40 41 42 43 44 45]
[50 51 52 53 54 55]]
1. [3 4]
2. [ 2 12 22 32 42 52]
3. [[20 22 24]
[40 42 44]]
4. [33]
5. [[44 45]
[54 55]]
Download