-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn3.py
422 lines (320 loc) · 10.7 KB
/
n3.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
# ndarrays: a and b
import numpy as np
a = np.array([[10,20],[30,40]])
b = np.array([[1,2],[3,4]])
a
b
a*b
# If we want matrix multiplication then we should go for dot() function
np.dot(a,b)
# A.B ===>a.dot(b)
# -------------------------------------------------------------- Importance of matrix class in numpy library --------------------------------------------
# 1-D array is caled ---> Vector
# 2-D array is called --> Matrix
# Matrix class is specially designed class to create 2-D arrays.
# How to creare 2-D array:
# 1.By using ndarray
# 2.By using matrix class
# >>>help(np.matrix):
# class matrix(ndarray)
# matrix(data, dtype=None, copy=True)
# Creating matrix object from string:
a = np.matrix('col1 col2 col3;col1 col2 col3')
a = np.matrix('col1,col2,col3;col1,col2,col3')
# Ex:
a = np.matrix('10,20;30,40')
type(a) #<class 'numpy.matrix'>
a
b = np.matrix('10 20;30 40')
type(b) #<class 'numpy.matrix'>
b
# Creating matrix object from nested list
a = np.matrix([[10,20],[30,40]])
a
# Create a matrix from ndarray
a = np.arange(6).reshape(3,2)
type(a) #<class 'numpy.ndarray'>
b = np.matrix(a)
type(b) #<class 'numpy.matrix'>
# + operator in ndarray and matrix:
# The behaviour is the same for both.
# Ex:
a = np.array([[1,2],[3,4]])
m = np.matrix([[1,2,],[3,4]])
a+a
m+m
# * operator in ndarray and matrix:
# -->In case of ndarray * operator performs element level multiplication
# -->In case of matrix * operator performs matrix multiplication
a = np.array([[1,2],[3,4]])
m = np.matrix([[1,2,],[3,4]])
a*a
m*m
# ** operator in ndarray and matrix:
# -->In case of ndaaray ** operator performs power operation at element level.
# -->In case of matrix ** operator performs power operation at matrix level
# m**2 ==> m * m
# Ex:
a
a**2
m
m**2
# T in ndarray and matrix:
# T behaves same way in both.
a
a.T
m
m.T
# Note:It is no longer recommended to use this class, even for linear algebra.
# Instead use regular arrays. The class may be removed in the future.
# Differences between ndarray and matrix?
# -------------------------------------------------------------
# ndarray matrix
# ----------- -----------
# 1.It can represent any n-dimension array. 1.It can represent only 2-D array.
# 2.We can create from any array_like object 2.We can create from either array_like
# but not from the string. object or from string.
# 3.* operator meant for element multiplication3.* operator for dot product not for
# but not for dot product element multiplication.
# 4.** meant for element level power operation. 4.** meant for matrix power operation.
# 5.It is the parent class. 5.It is the child class.
# 6.It is the recommended to use 6.It is not recommended to use and it
# is depricated.
# Chapter-18:Linear Algebra functions from linalg module
# ------------------------------------------------------------------------------------
# numpy.linlag ---> To perform linear algebra operations.
# 1.inv() ---> To inverse() of a matrix.
# 2.matrix_power() ---> To find power of matrix line A power(n).
# 3.det() -->To find determinant of matrix.
# 4.solve() ---> To solve linear algebra equations.
# 1.inv():To find inverse of a matrix
# --------------------------------------------------
help(np.linalg.inv)
# inv(a)
# Compute the (multiplicative) inverse of a matrix.
# Given a square matrix 'a', return the matrix 'ainv' satisfying
# dot(a, ainv) = dot(ainv, a) = eye(a.shape[0]).
a = np.array([[1,2],[3,4]])
a
ainv = np.linalg.inv(a)
ainv
# How to check:
# ---------------------
i = np.eye(2)
i
np.dot(a,ainv)
np.allclose(i,np.dot(a,ainv))
True
# allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)
# Returns True if two arrays are element-wise equal within a tolerance.
# Note:
# We can find inverse only for square matrices, otherwise we will get an error.
# Ex:
a = np.array([[10,20,30],[40,50,60]])
a
np.linalg.inv(a) #Last 2 dimensions of the array must be square
# How to find inverse of 3-D array:
# -------------------------------------------------
# 3-D arrays contains collection of 2-D arrays.
# Finding inverse of 3-D array means, finding inverse for every 2-D array.
a = np.arange(8).reshape(2,2,2)
a
np.linalg.inv(a)
# 2.matrix_power():To find power of a matrix line a power(n)
# ---------------------------------------------------------------------------------------
# >>> help(np.linalg.matrix_power)
# matrix_power(a, n)
# Raise a square matrix to the (integer) power 'n'.
# if n == 0 ==>Identity Matrix
# if n > 0 ==>normal power operation
# if n < 0 ==>First inverse and then power operation for absolute value
# Ex:
a = np.array([[1,2],[3,4]])
a
np.linalg.matrix_power(a,0)
np.linalg.matrix_power(a,2)
np.linalg.matrix_power(a,-2)
np.dot(np.linalg.inv(a),np.linalg.inv(a))
np.linalg.matrix_power(np.linalg.inv(a),2)
# Note:We can find matix_power only for a square matrix, otherwise we will get error.
# 3.det():To find determinant of a matrix
# ---------------------------------------------
# help(np.linalg.det)
# print(help(np.linalg.det))
a = np.array([[1,2],[3,4]])
a
# For 3x3 matrix:
# ------------------
# a = np.array([[1,2,3],[4,5,6],[7,8,9]])
a = np.arange(9).reshape(3,3)
a
np.linalg.det(a)
# Note: We can find determinant only for square matrix.
# 4.solve():To solve linear algebra equations
# -----------------------------------------------
# help(np.linalg.solve)
# print(help(np.linalg.solve))
'''
case study:
-------------
Boys + Girls = 2200
Boys = 3$ , Girls = 8$
Total Fees = 10100$
x + y = 2200 ------> x = 2200 - y
3x + 8y = 10100
3(2200-y) + 8y = 10100
6600 - 3y + 8y = 10100
5y = 10100 - 6600
5y = 3500
y = 3500/5
y = 700
x = 2200 - 700
x = 1500
'''
a = np.array([[1,1],[3,8]])
a
b = np.array([2200,10100])
b
np.linalg.solve(a,b)
# Note:We can solve linear algebra equations only for square matrix, otherwise we will get an error.
# Ex:
a = np.array([-4,7,-2],[1,-2,1],[2,-3,1])
a
b = np.array([2,3,-4])
b
np.linalg.solve(a,b)
# ----------------------------------------- I/O Operations with numpy ----------------------------------------------------------------
# We can save/write ndarray objects to a binary file for further purpose.Later point of time,whenever these objects are required, we can read from that binary file.
# save() ----> To save/write ndarray object to a file.
# load() ----> To read ndarray object from a file.
print(help(np.save()))
print(help(np.load()))
# EX - 1: Saving ndarray object to a file and read back
# ------------------------------------------------------------
import numpy as np
a = np.array([10,20,30],[40,50,60])
# save/serialize ndarray object to a file
np.save('out.npy',a)
# load ndarray objectt from a file
out_array = np.load('out.npy')
print(out_array)
# Note :
# 1. The data will be stored in binary format.
# 2. File extension should be .npy, otherwise save() function itself will add that file extension.
# 3. By using save() function we can write only one object to the file. If we want to write multiple objects to a file then we should go for save() function.
# Saving multiple ndarray objects to the binary file:
# --------------------------------------------------------------
import numpy as np
a = np.array([10,20,30],[40,50,60])
b = np.array([70,80],[90,100])
np.savez('out.npz',a,b)
npzfileobj = np.load('out.npz') # returns NpzFile object
print(type(npzfileobj))
print(npzfileobj.files)
print(npzfileobj['arr_0'])
print(npzfileobj['arr_1'])
# Note:
# 1. np.save() ---> save an array to a binary file in .npy format.
# 2. np.savez() ---> save several arrays into a single file in .npz format but in uncompressed form.
# 3. np.savez.compressed() ---> save several arrays into a single file in .npz format but in compressed form.
# 4. np.load() ---> To load/read ndarray from .npy or .npz file.
# Compressed form:
# --------------------
np.savez_compressed('out_compressed.npz',a,b)
# Analysis:
# ---------
# path of the out_compressed.npz file
# Q : we can save object in compressed form,then what is the need of uncompressed form?
# compressed form ----> Memory will be saved , but performance will be degraded.
# uncompressed form ----> Memory will be wasted, but performance will be increased.
# Save ndarray objects to the file in normal text format:
# --------------------------------------------------------------
# savetxt() and loadtxt()
print(np.savetxt())
print(np.loadtxt())
# Ex:
import numpy as np
a = np.array([10,20,30],[40,50,60])
np.savetxt('out.txt',a)
out_array = np.loadtxt('out.txt')
print(out_array)
print('output array in the format')
out_array2 = np.loadtxt('out.txt',delimiter=',')
print(out_array2)
# Ex:
import numpy as np
a = np.array([['sunny',1000],['rainy',2000],['cloudy',3000]])
np.savetxt('out.txt',a,fmt='%s %d',delimiter=',')
a2 = np.loadtxt('out.txt',delimiter=',')
print(a2)
# Writing ndarray objects to the csv file:
# --------------------------------------------
# csv - comma separated values
import numpy as np
a1 = np.array([10,20,30],[40,50,60])
np.savetxt('out.csv',a1,fmt='%d',delimiter=',')
a2 = np.loadtxt('out.csv',delimiter=',')
print(a2)
# Creation of array by using diag() function:
# ------------------------------------------------
print(help(np.diag))
# Parameters
# ----------
# v : array_like
# If `v` is a 2-D array, return a copy of its `k`-th diagonal.
# If `v` is a 1-D array, return a 2-D array with `v` on the `k`-th
# diagonal.
# k : int, optional
# Diagonal in question. The default is 0. Use `k>0` for diagonals
# above the main diagonal, and `k<0` for diagonals below the main
# diagonal.
# Ex:
a = np.arange(1,10).reshape(3,3)
a
np.diag(a,k=0)
np.diag(a,k=1)
np.diag(a,k=-1)
np.diag(a,k=2)
np.diag(a,k=-2)
np.diag(a,k=-3)
# Ex:
a = np.diag([10,20,30,40])
a
np.diag(a,k=0)
np.diag(a,k=1)
np.diag(a,k=-1)
np.diag(a,k=2)
'''
View vs Copy:
--------------------
View:
--------
-->View is not a separate object and just it is logical representation of existing array.
If we perofrm any changes to the original array, those changes will be reflected to the view. Viceversa also.
-->We can create view explicitly by using view() method of ndarray class.
Ex:
a = np.array([10,20,30,40])
a #array([10, 20, 30, 40])
b = a.view()
a[0] = 333
a #array([333, 20, 30, 40])
b #array([333, 20, 30, 40])
b[-1] = 999
a #array([333, 20, 30, 999])
b #array([333, 20, 30, 999])
Copy:
--------
-->Copy means separate object.
-->If we perform any changes to the original array, those changes wont be reflected to the copy. Viceversa also.
-->By using copy() method of ndarray class, we can create copy of existing ndarray.
Ex:
a = np.array([10,20,30,40])
b = a.copy()
b
a[0] = 333
a #array([333, 20, 30, 40])
b #array([10, 20, 30, 40])
b[-1] = 999
b #array([ 10, 20, 30, 999])
a #array([333, 20, 30, 40])
'''