-
Notifications
You must be signed in to change notification settings - Fork 0
/
ml_numpy.py
442 lines (339 loc) · 11.6 KB
/
ml_numpy.py
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# dimesion of an array is known as rank.
import numpy as np
# # arr1 with rank one
# arr1=np.array([1,2,3,4])
# print(arr1)
# # arr1 with rank two using list
# arr2=np.array([[1,2,3,4],
# [1,2,3,4]])
# print(arr2)
# # arr1 with rank one using tuples
# arr3=np.array((1,2,3,4))
# print(arr3)
# # Initial Array
# arr = np.array([[-1, 2, 0, 4],
# [4, -0.5, 6, 0],
# [2.6, 0, 7, 8],
# [3, -7, 4, 2.0]])
# print("Initial Array: ")
# print(arr)
# # Printing a range of Array
# # with the use of slicing method
# sliced_arr = arr[:2,::2]
# print ("Array with first 2 rows and alternate columns(0 and 2):\n", sliced_arr)
# Printing elements at
# specific Indices
# Index_arr = arr[[1, 1, 0, 3],
# [3, 2, 1, 0]]
# print ("\nElements at indices (1, 3), (1, 2), (0, 1), (3, 0):\n", Index_arr)
# # Defining Array 1
# a = np.array([[1, 2],
# [3, 4]])
# # Defining Array 2
# b = np.array([[4, 3],
# [2, 1]])
# # Adding 1 to every element
# print ("Adding 1 to every element:\n", a + 1)
# # Subtracting 2 from each element
# print ("\nSubtracting 2 from each element:\n", b - 2)
# # sum of array elements
# # Performing Unary operations
# print ("\nSum of all array elements:\n", a.sum())
# # Adding two arrays
# # Performing Binary operations
# print ("\nArray sum:\n", a + b)
# # Integer datatype
# # guessed by Numpy
# x = np.array([1, 2])
# print("Integer Datatype: ")
# print(x.dtype)
# # Float datatype
# # guessed by Numpy
# x = np.array([1.0, 2.0])
# print("\nFloat Datatype: ")
# print(x.dtype)
# # Forced Datatype
# x = np.array([1, 2], dtype = np.int64)
# print("\nForcing a Datatype: ")
# print(x.dtype)
# # First Array
# arr1 = np.array([[4, 7], [2, 6]],
# dtype = np.float64)
# # Second Array
# arr2 = np.array([[3, 6], [2, 8]],
# dtype = np.float64)
# # Addition of two Arrays
# Sum = np.add(arr1, arr2)
# Sum = arr1+arr2
# print("Addition of Two Arrays: ")
# print(Sum)
# # Addition of all Array elements
# # using predefined sum method
# Sum1 = np.sum(arr1)
# # print(arr1.sum())
# print("\nAddition of Array elements: ")
# print(Sum1)
# # Square root of Array
# Sqrt = np.sqrt(arr1)
# print("\nSquare root of Array1 elements: ")
# print(Sqrt)
# # Transpose of Array
# # using In-built function 'T'
# Trans_arr = arr1.T
# print("\nTranspose of Array: ")
# print(Trans_arr)
# # Creating array object
# arr = np.array( [[ 1, 2, 3],
# [ 4, 2, 5]] )
# # Printing type of arr object
# print("Array is of type: ", type(arr))
# # Printing array dimensions (axes)
# print("No. of dimensions: ", arr.ndim)
# # Printing shape of array
# print("Shape of array: ", arr.shape)
# # Printing size (total number of elements) of array
# print("Size of array: ", arr.size)
# # Printing type of elements in array
# print("Array stores elements of type: ", arr.dtype)
# Creating array from list with type float
# a = np.array([[1, 2, 4], [5, 8, 7]], dtype = 'float')
# print ("Array created using passed list:\n", a)
# # Creating array from tuple
# b = np.array((1 , 3, 2))
# print ("\nArray created using passed tuple:\n", b)
# # Creating a 3X4 array with all zeros
# c = np.zeros((3, 4),dtype = 'int')
# print ("\nAn array initialized with all zeros:\n", c)
# # Create a constant value array of complex type
# d = np.full((3, 3), 6, dtype = 'complex')
# print ("\nAn array initialized with all 6s.Array type is complex:\n", d)
# # An exemplar array
# arr = np.array([[-1, 2, 0, 4],
# [4, -0.5, 6, 0],
# [2.6, 0, 7, 8],
# [3, -7, 4, 2.0]])
# # boolean array indexing example
# cond = arr > 0 # cond is a boolean array
# temp = arr[cond]
# print ("\nElements greater than 0:\n", temp)
# print(cond)
# arr = np.array([[1, 5, 6],
# [4, 7, 2],
# [3, 1, 9]])
# # maximum element of array
# print ("Largest element is:", arr.max())
# print ("Row-wise maximum elements:",arr.max(axis = 1))
# # minimum element of array
# print ("Column-wise minimum elements:",arr.min(axis = 0))
# # sum of array elements
# print ("Sum of all array elements:",arr.sum())
# # cumulative sum along each row
# print ("Cumulative sum along each row:\n",arr.cumsum(axis = 1))
# a = np.array([[1, 2],
# [3, 4]])
# b = np.array([[4, 3],
# [2, 1]])
# # add arrays
# print ("Array sum:\n", a + b)
# # multiply arrays (elementwise multiplication)
# print ("Array multiplication:\n", a*b)
# # matrix multiplication
# print ("Matrix multiplication:\n", a.dot(b))
# create an array of sine values
# a = np.array([0, np.pi/2, np.pi])
# print ("Sine values of array elements:\n", np.sin(a))
# # exponential values
# a = np.array([0, 1, 2, 3])
# print ("Exponent of array elements:\n", np.exp(a))
# # square root of array values
# print ("Square root of array elements:\n", np.sqrt(a))
# print(np.pi)
# np.int16 is converted into a data type object.
# print(np.dtype(np.int16))
# i4 represents integer of size 4 byte
# > represents big-endian byte ordering and
# < represents little-endian encoding.
# dt is a dtype object
# dt = np.dtype('>i4')
# print(dt)
# print(type(dt))
# print(dt.dtype)
# print("Byte order is:",dt.byteorder)
# print("Size is:",dt.itemsize)
# print("Data type is:",dt.name)
# Python Program illustrating
# numpy.all() method
# Parameter : keepdims
# # setting keepdims = True
# print("\nBool Value : ", np.all([[1, 0],[0, 4]], True))
# # setting keepdims = True
# print("\nBool Value : ", np.all([[0, 0],[0, 0]], False))
# Python Program illustrating
# numpy.all() method
# Axis = NULL
# True False
# True True
# True : False = False
# print("Bool Value with axis = NONE : ",np.all([[True,False],[True,True]]))
# Axis = 0
# True False
# True True
# True : False
# print("\nBool Value with axis = 0 : ",np.all([[True,False],[True,True]], axis = 0))
# print("\nBool : ", np.all([3,1,2]))
# # Not a Number (NaN), positive infinity and negative infinity
# # evaluate to True because these are not equal to zero.
# # print("\nBool : ", np.all([1.0, np.nan]))
# print("\nBool Value : ", np.all([[0, 0],[0, 0]]))
# print("\nBool Value : ", np.all([[1, 0],[0, 4]]))
# # setting keepdims = True
# print("\nBool Value : ", np.all([[0, 0],[0, 0]]))
# Python Programming illustrating
# numpy.arange method
# print("A\n", np.arange(4).reshape(2, 2), "\n")
# print("A\n", np.arange(4, 10), "\n")
# print("A\n", np.arange(4, 20, 3), "\n")
# print(np.arange(1, 2, 0.1))
# Python Program illustrating
# numpy.dot() method
# Scalars
# product = np.dot(5, 4)
# print("Dot Product of scalar values : ", product)
# # 1D array
# vector_a = 2 + 3j
# vector_b = 4 + 5j
# product = np.dot(vector_a, vector_b)
# print("Dot Product : ", product)
'''
How Code1 works ?
vector_a = 2 + 3j
vector_b = 4 + 5j
now dot product = 2(4 + 5j) + 3j(4 +5j) = 8 + 10j + 12j - 15 = -7 + 22j
here vector_a and vector_b is complex numbers
'''
# a=np.random.rand(3,3)
# a=np.linspace(2,9,50)
# a=np.random.randn(3,3)
# a=np.random.randint(1,100,15).reshape(3,5)
# a=np.random.random_sample((1,5))
# print(a)
# A structured data type containing a
# 16-character string (in field ‘name’)
# and a sub-array of two 64-bit floating
# -point number (in field ‘grades’)
# dt = np.dtype([('name', np.unicode_, 16),
# ('grades', np.float64, (2,))])
# # Data type of object with field grades
# print(dt)
# print(dt['grades'])
# # Data type of object with field name
# print(dt['name'])
# # x is a structured array with names
# # and marks of students.
# # Data type of name of the student is
# # np.unicode_ and data type of marks is
# # np.float(64)
# x = np.array([('Sarah', (8.0, 7.0)),
# ('John', (6.0, 7.0))], dtype=dt)
# print(x)
# print(x[1])
# print("Grades of John are: ", x[1]['grades'])
# print("Names are: ", x['name'])
# # sort along the first axis
# a = np.array([[12, 15],
# [10, 1]])
# arr1 = np.sort(a, axis = 0)
# print ("Along first axis : \n", arr1)
# # sort along the last axis
# a = np.array([[10, 15],
# [12, 1]])
# arr2 = np.sort(a, axis = 1)
# print ("\nAlong first axis : \n", arr2)
# a = np.array([[12, 15],
# [10, 1]])
# arr1 = np.sort(a, axis = None)
# print ("\nAlong none axis : \n", arr1.reshape(2,2))
# # Numpy array created
# a = np.array([9, 3, 1, 7, 4, 3, 6])
# # unsorted array print
# print('Original array:\n', a)
# # Sort array indices
# b = np.argsort(a)
# print('Sorted indices of original array->', b)
# # To get sorted array using sorted indices
# # c is temp array created of same len as of b
# c = np.zeros(len(b), dtype = int)
# print('Sorted array->', c)
# for i in range(0, len(b)):
# c[i]= a[b[i]]
# print('Sorted array->', c)
# # Numpy array created
# # First column
# a = np.array([9, 3, 1, 3, 4, 3, 6])
# # Second column
# b = np.array([4, 6, 9, 2, 1, 8, 7])
# print('column a, column b')
# for (i, j) in zip(a, b):
# print(i, ' ', j)
# print("sorted index of a: ",np.argsort(a))
# print("sorted index of b: ",np.argsort(b))
# # Sort by a then by b
# ind = np.lexsort((b, a))
# print('Sorted indices->', ind)
'''
numpy.ndarray.sort() Sort an array, in-place.
numpy.msort() Return a copy of an array sorted along the first axis.
numpy.sort_complex() Sort a complex array using the real part first, then the imaginary part.
numpy.partition() Return a partitioned copy of an array.
numpy.argpartition() Perform an indirect partition along the given axis using the algorithm specified by the kind keyword.
'''
# # Working on 2D array
# array = np.arange(12).reshape(3, 4)
# print("INPUT ARRAY : \n", array)
# # No axis mentioned, so works on entire array
# print("\nMax element : ", np.argmax(array))
# # returning Indices of the max element
# # as per the indices
# print(("\nIndices of Max element : ", np.argmax(array, axis=0)))
# print(("\nIndices of Max element : ", np.argmax(array, axis=1)))
# # Working on 1D array
# array = [np.nan, 1, 2, 3, 8]
# print("INPUT ARRAY 1 : \n", array)
# array2 = np.array([[np.nan, 1], [8, 3]])
# # returning Indices of the max element
# # as per the indices ingnoring NaN
# print("\nIndices of max in array1 : ", np.nanargmax(array))
# # Working on 2D array
# print("\nINPUT ARRAY 2 : \n", array2)
# print("\nIndices of max in array2 : ", np.nanargmax(array2))
# print("\nIndices at axis 1 of array2 : ", np.nanargmax(array2, axis = 1))
# # Working on 1D array
# array = np.arange(8)
# print("INPUT ARRAY : \n",array)
# # returning Indices of the min element
# # as per the indices
# print("\nIndices of min element : ", np.argmin(array, axis=0))
'''
numpy.nanargmin() Return the indices of the minimum values in the specified axis ignoring NaNs.
numpy.argwhere() Find the indices of array elements that are non-zero, grouped by element.
numpy.nonzero() Return the indices of the elements that are non-zero.
numpy.flatnonzero() Return indices that are non-zero in the flattened version of a.
numpy.where() Return elements chosen from x or y depending on condition.
numpy.searchsorted() Find indices where elements should be inserted to maintain order.
numpy.extract() Return the elements of an array that satisfy some condition
'''
# Counting a number of
# non-zero values
# a = np.count_nonzero([[0,1,7,0,20],
# [3,0,0,2,19]])
# b = np.count_nonzero([[0,1,7,0,30],
# [3,0,0,2,19]], axis=0)
# print("Number of nonzero values is :",a)
# print("Number of nonzero values is :",b)
# array = np.array([[1, 2], [3, 4]])
# # using flatten method
# array.flatten()
# print(array)
# #using fatten method
# array.flatten('F')
# print(array)