-
Notifications
You must be signed in to change notification settings - Fork 1
/
VAT22.txt
262 lines (215 loc) · 6.29 KB
/
VAT22.txt
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
x86 chapter in let us C
###****#####
import numpy
from numpy import *
from numpy import ones
import numpy as np
from numpy import ones as npones
import numpy as np
arr=np.array([[1,2,3],[4,2,5]])
print('Array is of type',type(arr))
print('No of dimensions:',arr.ndim)
print('Shape of array:',arr.shape)
print('Size of array',arr.size)
print('Array stores elements of type',arr.dtype)
o/p
Array is of type <class 'numpy.ndarray'>
No of dimensions: 2
Shape of array: (2, 3)
Size of array 6
Array stores elements of type int32
a=np.array([[1,2,4],[5,8,7]],dtype='float')
print('Array created using passed list:\n',a)
o/p
Array created using passed list:
[[1. 2. 4.]
[5. 8. 7.]]
b=np.array((1,3,2))
print('\nArray created using passed tuple:\n',b)
Array created using passed list:
[1 3 2]
c=np.zeros((3,4))
print('\nAn array initialised with all zeros:\n',c)
o/p
An array initialised with all zeros:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
d=np.full((3,3),6,dtype='complex')
print('\nAn array initialized with all 6s.''Array type is complex:\n',d)
o/p
An array initialized with all 6s.Array type is complex:
[[6.+0.j 6.+0.j 6.+0.j]
[6.+0.j 6.+0.j 6.+0.j]
[6.+0.j 6.+0.j 6.+0.j]]
e=np.random.random((2,2))
print('\nA random(garbage value) array:\n',e)
o/p
A random(garbage value) array:
[[0.66801979 0.28622739]
[0.27776717 0.80394206]]
f=np.arange(0,30,5)
print('\nA sequential array with steps of 5:\n',f)
o/p
A sequential array with steps of 5:
[ 0 5 10 15 20 25]
f=np.arange(10,100,3)
print('\nA sequential array with steps of 5:\n',f)
o/p
A sequential array with steps of 5:
[10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79
82 85 88 91 94 97]
g=np.linspace(0,5,10)
print('\nA sequential array with 10 values between''0 and 5:\n',g)
o/p
A sequential array with 10 values between0 and 5:
[0. 0.55555556 1.11111111 1.66666667 2.22222222 2.77777778
3.33333333 3.88888889 4.44444444 5. ]
arr=np.array([[1,2,3,4],[5,2,4,2],[1,2,0,4]])
print('\nOriginal array:\n',arr)
newarr=arr.reshape(2,2,3)
print('Reshaped array:/n',newarr)
o/p
Original array:
[[1 2 3 4]
[5 2 4 2]
[1 2 0 4]]
Reshaped array:/n [[[1 2 3]
[4 5 2]]
[[4 2 1]
[2 0 4]]]
arr=np.array([[1,2,3,4],[5,2,4,2],[1,2,0,4]])
print('\nOriginal array:\n',arr)
newarr=arr.reshape(2,2,3)
print('Reshaped array:/n',newarr)
o/p
Original array:
[[1 2 3 4]
[5 2 4 2]
[1 2 0 4]]
Reshaped array:/n [[[1 2 3]
[4 5 2]]
[[4 2 1]
[2 0 4]]]
arr=np.array([[1,2,3,4],[5,6,7,8]])
f1=arr.flatten()
print('Flattened array:\n',f1)
o/p
Flattened array:
[1 2 3 4 5 6 7 8]
arr=np.array([[1,2,3,4],[5,2,4,2],[1,2,0,4],[5,3,1,0]])
temp=arr[:2,::2]
print('Array with first two rows and alternate''column(0 and 2):\n',temp)
o/p
Array with first two rows and alternatecolumn(0 and 2):
[[1 4]
[5 2]
[1 4]]
arr=np.array([[1,2,3,4],[5,2,4,2],[1,2,0,4],[5,3,1,0]])
temp=arr[:3,::3]
print('Array with first three rows and alternate''column(0 and 3):\n',temp)
o/p
Array with first two rows and alternatecolumn(0 and 2):
[[1 4]
[5 2]
[1 4]]
temp=arr[[0,1,2,3],[3,2,1,0]]
print('\nElements at indices (0,3),(1,2),(2,1),''(3,0):\n',temp)
o/p
Elements at indices (0,3),(1,2),(2,1),(3,0):
[4 4 2 5]
cond=arr>0 #cond is boolean array
temp=arr[cond]
print('\nBoolean data\n',temp)
o/p
Boolean data
[1 2 3 4 5 2 4 2 1 2 4 5 3 1]
a=np.array([1,2,5,3])
print('Adding 1 to every element:',a+1)
print('subtracting 3 from each element:',a-3)
print('multiplying each element by 10:',a*10)
print('Squaring each element:',a**2)
a *=2
print('Doubled each element of original array:',a)
o/p
Adding 1 to every element: [2 3 6 4]
subtracting 3 from each element: [-2 -1 2 0]
multiplying each element by 10: [10 20 50 30]
Squaring each element: [ 1 4 25 9]
Doubled each element of original array: [ 2 4 10 6]
a=np.array([[1,2,3],[3,4,5],[9,6,0]])
print('original array:',a)
print('transpose of original array:',a.T)
o/p
original array: [[1 2 3]
[3 4 5]
[9 6 0]]
transpose of original array: [[1 3 9]
[2 4 6]
[3 5 0]]
#Array operation
arr=np.array([[1,2,3,4],[5,2,4,2],[1,2,6,4],[7,3,1,0]])
print('Largest element is:',arr.max())
print('Row-wise maximum element:',arr.max(axis=1))
print('Row-wise minimum element:',arr.min(axis=0))
print('Sum of all arrays elements: ',arr.sum())
o/p
Largest element is: 7
Row-wise maximum element: [4 5 6 7]
Row-wise minimum element: [1 2 1 0]
Sum of all arrays elements: 47
a=np.array([[1,2],[3,4]])
b=np.array([[5,6],[7,8]])
print('Array sum:\n',a+b)
print('Array multiplication:\n',a*b)
print('Matrix multiplication:\n',a.dot(b))
o/p
Array sum:
[[ 6 8]
[10 12]]
Array multiplication:
[[ 5 12]
[21 32]]
Matrix multiplication:
[[19 22]
[43 50]]
a=np.array([0, np.pi/2,np.pi])
print('Sine value of array elements:\n',np.sin(a))
o/p
Sine value of array elements:
[0.0000000e+00 1.0000000e+00 1.2246468e-16]
a=np.array([0,1,2,3])
print('Exponent of array elements:',np.exp(a))
print('Square root of every element',np.sqrt(a))
o/p
Exponent of array elements: [ 1. 2.71828183 7.3890561 20.08553692]
Square root of every element [0. 1. 1.41421356 1.73205081]
arr=np.array([[1,2,3,4],
[5,2,4,2],
[1,2,6,4]])
print('Array elements in sorted order:\n',np.sort(arr,axis=None))
print('Row wise sorted array:\n',np.sort(arr,axis=1))
print('Column wise sort by applying merge sort:\n',np.sort(arr,axis=0,kind='mergesort'))
o/p
Array elements in sorted order:
[1 1 2 2 2 2 3 4 4 4 5 6]
Row wise sorted array:
[[1 2 3 4]
[2 2 4 5]
[1 2 4 6]]
Column wise sort by applying merge sort:
[[1 2 3 2]
[1 2 4 4]
[5 2 6 4]]
dtypes=[('name','S10'),('grad_year', int),('cgpa',float)]
values=[('Hrithik',2009,8.5),('Ajay',2008,8.7),('Pankaj',2008,7.9),('Aakash',2009,9.0)]
arr=np.array(values, dtype=dtypes)
print('\nArray sorted by names:\n',np.sort(arr,order='name'))
print('sorted data',np.sort(arr,order=['grad_year','cgpa']))
o/p
Array sorted by names:
[(b'Aakash', 2009, 9. ) (b'Ajay', 2008, 8.7) (b'Hrithik', 2009, 8.5)
(b'Pankaj', 2008, 7.9)]
sorted data [(b'Pankaj', 2008, 7.9) (b'Ajay', 2008, 8.7) (b'Hrithik', 2009, 8.5)
(b'Aakash', 2009, 9. )]
?