PrabhakarVenkat commited on
Commit
b553092
·
verified ·
1 Parent(s): f0e7979

Upload 3 files

Browse files
Files changed (3) hide show
  1. NumPy_Library_Basics.ipynb +1116 -0
  2. README.md +29 -0
  3. numpy.png +0 -0
NumPy_Library_Basics.ipynb ADDED
@@ -0,0 +1,1116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "### Load in NumPy (remember to pip install numpy first)"
8
+ ]
9
+ },
10
+ {
11
+ "cell_type": "code",
12
+ "execution_count": 1,
13
+ "metadata": {},
14
+ "outputs": [],
15
+ "source": [
16
+ "import numpy as np"
17
+ ]
18
+ },
19
+ {
20
+ "cell_type": "markdown",
21
+ "metadata": {},
22
+ "source": [
23
+ "### The Basics"
24
+ ]
25
+ },
26
+ {
27
+ "cell_type": "code",
28
+ "execution_count": 2,
29
+ "metadata": {},
30
+ "outputs": [
31
+ {
32
+ "name": "stdout",
33
+ "output_type": "stream",
34
+ "text": [
35
+ "[1 2 3]\n"
36
+ ]
37
+ }
38
+ ],
39
+ "source": [
40
+ "a = np.array([1,2,3], dtype='int32')\n",
41
+ "print(a)"
42
+ ]
43
+ },
44
+ {
45
+ "cell_type": "code",
46
+ "execution_count": 3,
47
+ "metadata": {},
48
+ "outputs": [
49
+ {
50
+ "name": "stdout",
51
+ "output_type": "stream",
52
+ "text": [
53
+ "[[9. 8. 7.]\n",
54
+ " [6. 5. 4.]]\n"
55
+ ]
56
+ }
57
+ ],
58
+ "source": [
59
+ "b = np.array([[9.0,8.0,7.0],[6.0,5.0,4.0]])\n",
60
+ "print(b)"
61
+ ]
62
+ },
63
+ {
64
+ "cell_type": "code",
65
+ "execution_count": 4,
66
+ "metadata": {},
67
+ "outputs": [
68
+ {
69
+ "data": {
70
+ "text/plain": [
71
+ "1"
72
+ ]
73
+ },
74
+ "execution_count": 4,
75
+ "metadata": {},
76
+ "output_type": "execute_result"
77
+ }
78
+ ],
79
+ "source": [
80
+ "# Get Dimension\n",
81
+ "a.ndim"
82
+ ]
83
+ },
84
+ {
85
+ "cell_type": "code",
86
+ "execution_count": 5,
87
+ "metadata": {},
88
+ "outputs": [
89
+ {
90
+ "data": {
91
+ "text/plain": [
92
+ "(2, 3)"
93
+ ]
94
+ },
95
+ "execution_count": 5,
96
+ "metadata": {},
97
+ "output_type": "execute_result"
98
+ }
99
+ ],
100
+ "source": [
101
+ "# Get Shape\n",
102
+ "b.shape"
103
+ ]
104
+ },
105
+ {
106
+ "cell_type": "code",
107
+ "execution_count": 6,
108
+ "metadata": {},
109
+ "outputs": [
110
+ {
111
+ "data": {
112
+ "text/plain": [
113
+ "dtype('int32')"
114
+ ]
115
+ },
116
+ "execution_count": 6,
117
+ "metadata": {},
118
+ "output_type": "execute_result"
119
+ }
120
+ ],
121
+ "source": [
122
+ "# Get Type\n",
123
+ "a.dtype"
124
+ ]
125
+ },
126
+ {
127
+ "cell_type": "code",
128
+ "execution_count": 7,
129
+ "metadata": {},
130
+ "outputs": [
131
+ {
132
+ "data": {
133
+ "text/plain": [
134
+ "4"
135
+ ]
136
+ },
137
+ "execution_count": 7,
138
+ "metadata": {},
139
+ "output_type": "execute_result"
140
+ }
141
+ ],
142
+ "source": [
143
+ "# Get Size\n",
144
+ "a.itemsize"
145
+ ]
146
+ },
147
+ {
148
+ "cell_type": "code",
149
+ "execution_count": 8,
150
+ "metadata": {},
151
+ "outputs": [
152
+ {
153
+ "data": {
154
+ "text/plain": [
155
+ "12"
156
+ ]
157
+ },
158
+ "execution_count": 8,
159
+ "metadata": {},
160
+ "output_type": "execute_result"
161
+ }
162
+ ],
163
+ "source": [
164
+ "# Get total size\n",
165
+ "a.nbytes"
166
+ ]
167
+ },
168
+ {
169
+ "cell_type": "code",
170
+ "execution_count": 9,
171
+ "metadata": {},
172
+ "outputs": [
173
+ {
174
+ "data": {
175
+ "text/plain": [
176
+ "3"
177
+ ]
178
+ },
179
+ "execution_count": 9,
180
+ "metadata": {},
181
+ "output_type": "execute_result"
182
+ }
183
+ ],
184
+ "source": [
185
+ "# Get number of elements\n",
186
+ "a.size"
187
+ ]
188
+ },
189
+ {
190
+ "cell_type": "markdown",
191
+ "metadata": {},
192
+ "source": [
193
+ "### Accessing/Changing specific elements, rows, columns, etc"
194
+ ]
195
+ },
196
+ {
197
+ "cell_type": "code",
198
+ "execution_count": 10,
199
+ "metadata": {},
200
+ "outputs": [
201
+ {
202
+ "name": "stdout",
203
+ "output_type": "stream",
204
+ "text": [
205
+ "[[ 1 2 3 4 5 6 7]\n",
206
+ " [ 8 9 10 11 12 13 14]]\n"
207
+ ]
208
+ }
209
+ ],
210
+ "source": [
211
+ "a = np.array([[1,2,3,4,5,6,7],[8,9,10,11,12,13,14]])\n",
212
+ "print(a)"
213
+ ]
214
+ },
215
+ {
216
+ "cell_type": "code",
217
+ "execution_count": 11,
218
+ "metadata": {},
219
+ "outputs": [
220
+ {
221
+ "data": {
222
+ "text/plain": [
223
+ "13"
224
+ ]
225
+ },
226
+ "execution_count": 11,
227
+ "metadata": {},
228
+ "output_type": "execute_result"
229
+ }
230
+ ],
231
+ "source": [
232
+ "# Get a specific element [r, c]\n",
233
+ "a[1, 5]"
234
+ ]
235
+ },
236
+ {
237
+ "cell_type": "code",
238
+ "execution_count": 12,
239
+ "metadata": {},
240
+ "outputs": [
241
+ {
242
+ "data": {
243
+ "text/plain": [
244
+ "array([1, 2, 3, 4, 5, 6, 7])"
245
+ ]
246
+ },
247
+ "execution_count": 12,
248
+ "metadata": {},
249
+ "output_type": "execute_result"
250
+ }
251
+ ],
252
+ "source": [
253
+ "# Get a specific row \n",
254
+ "a[0, :]"
255
+ ]
256
+ },
257
+ {
258
+ "cell_type": "code",
259
+ "execution_count": 13,
260
+ "metadata": {},
261
+ "outputs": [
262
+ {
263
+ "data": {
264
+ "text/plain": [
265
+ "array([ 3, 10])"
266
+ ]
267
+ },
268
+ "execution_count": 13,
269
+ "metadata": {},
270
+ "output_type": "execute_result"
271
+ }
272
+ ],
273
+ "source": [
274
+ "# Get a specific column\n",
275
+ "a[:, 2]"
276
+ ]
277
+ },
278
+ {
279
+ "cell_type": "code",
280
+ "execution_count": 14,
281
+ "metadata": {},
282
+ "outputs": [
283
+ {
284
+ "data": {
285
+ "text/plain": [
286
+ "array([2, 4, 6])"
287
+ ]
288
+ },
289
+ "execution_count": 14,
290
+ "metadata": {},
291
+ "output_type": "execute_result"
292
+ }
293
+ ],
294
+ "source": [
295
+ "# Getting a little more fancy [startindex:endindex:stepsize]\n",
296
+ "a[0, 1:-1:2]"
297
+ ]
298
+ },
299
+ {
300
+ "cell_type": "code",
301
+ "execution_count": 15,
302
+ "metadata": {},
303
+ "outputs": [
304
+ {
305
+ "name": "stdout",
306
+ "output_type": "stream",
307
+ "text": [
308
+ "[[ 1 2 1 4 5 6 7]\n",
309
+ " [ 8 9 2 11 12 20 14]]\n"
310
+ ]
311
+ }
312
+ ],
313
+ "source": [
314
+ "a[1,5] = 20\n",
315
+ "\n",
316
+ "a[:,2] = [1,2]\n",
317
+ "print(a)"
318
+ ]
319
+ },
320
+ {
321
+ "cell_type": "markdown",
322
+ "metadata": {},
323
+ "source": [
324
+ "*3-d example"
325
+ ]
326
+ },
327
+ {
328
+ "cell_type": "code",
329
+ "execution_count": 16,
330
+ "metadata": {},
331
+ "outputs": [
332
+ {
333
+ "name": "stdout",
334
+ "output_type": "stream",
335
+ "text": [
336
+ "[[[1 2]\n",
337
+ " [3 4]]\n",
338
+ "\n",
339
+ " [[5 6]\n",
340
+ " [7 8]]]\n"
341
+ ]
342
+ }
343
+ ],
344
+ "source": [
345
+ "b = np.array([[[1,2],[3,4]],[[5,6],[7,8]]])\n",
346
+ "print(b)"
347
+ ]
348
+ },
349
+ {
350
+ "cell_type": "code",
351
+ "execution_count": 17,
352
+ "metadata": {},
353
+ "outputs": [
354
+ {
355
+ "data": {
356
+ "text/plain": [
357
+ "4"
358
+ ]
359
+ },
360
+ "execution_count": 17,
361
+ "metadata": {},
362
+ "output_type": "execute_result"
363
+ }
364
+ ],
365
+ "source": [
366
+ "# Get specific element (work outside in)\n",
367
+ "b[0,1,1]"
368
+ ]
369
+ },
370
+ {
371
+ "cell_type": "code",
372
+ "execution_count": 19,
373
+ "metadata": {},
374
+ "outputs": [],
375
+ "source": [
376
+ "# replace \n",
377
+ "b[:,1,:] = [[9,9],[8,8]]"
378
+ ]
379
+ },
380
+ {
381
+ "cell_type": "code",
382
+ "execution_count": 20,
383
+ "metadata": {},
384
+ "outputs": [
385
+ {
386
+ "data": {
387
+ "text/plain": [
388
+ "array([[[1, 2],\n",
389
+ " [9, 9]],\n",
390
+ "\n",
391
+ " [[5, 6],\n",
392
+ " [8, 8]]])"
393
+ ]
394
+ },
395
+ "execution_count": 20,
396
+ "metadata": {},
397
+ "output_type": "execute_result"
398
+ }
399
+ ],
400
+ "source": [
401
+ "b"
402
+ ]
403
+ },
404
+ {
405
+ "cell_type": "markdown",
406
+ "metadata": {},
407
+ "source": [
408
+ "### Initializing Different Types of Arrays"
409
+ ]
410
+ },
411
+ {
412
+ "cell_type": "code",
413
+ "execution_count": 21,
414
+ "metadata": {},
415
+ "outputs": [
416
+ {
417
+ "data": {
418
+ "text/plain": [
419
+ "array([[0., 0., 0.],\n",
420
+ " [0., 0., 0.]])"
421
+ ]
422
+ },
423
+ "execution_count": 21,
424
+ "metadata": {},
425
+ "output_type": "execute_result"
426
+ }
427
+ ],
428
+ "source": [
429
+ "# All 0s matrix\n",
430
+ "np.zeros((2,3))"
431
+ ]
432
+ },
433
+ {
434
+ "cell_type": "code",
435
+ "execution_count": 22,
436
+ "metadata": {},
437
+ "outputs": [
438
+ {
439
+ "data": {
440
+ "text/plain": [
441
+ "array([[[1, 1],\n",
442
+ " [1, 1]],\n",
443
+ "\n",
444
+ " [[1, 1],\n",
445
+ " [1, 1]],\n",
446
+ "\n",
447
+ " [[1, 1],\n",
448
+ " [1, 1]],\n",
449
+ "\n",
450
+ " [[1, 1],\n",
451
+ " [1, 1]]])"
452
+ ]
453
+ },
454
+ "execution_count": 22,
455
+ "metadata": {},
456
+ "output_type": "execute_result"
457
+ }
458
+ ],
459
+ "source": [
460
+ "# All 1s matrix\n",
461
+ "np.ones((4,2,2), dtype='int32')"
462
+ ]
463
+ },
464
+ {
465
+ "cell_type": "code",
466
+ "execution_count": 23,
467
+ "metadata": {},
468
+ "outputs": [
469
+ {
470
+ "data": {
471
+ "text/plain": [
472
+ "array([[99, 99],\n",
473
+ " [99, 99]])"
474
+ ]
475
+ },
476
+ "execution_count": 23,
477
+ "metadata": {},
478
+ "output_type": "execute_result"
479
+ }
480
+ ],
481
+ "source": [
482
+ "# Any other number\n",
483
+ "np.full((2,2), 99)"
484
+ ]
485
+ },
486
+ {
487
+ "cell_type": "code",
488
+ "execution_count": 24,
489
+ "metadata": {},
490
+ "outputs": [
491
+ {
492
+ "data": {
493
+ "text/plain": [
494
+ "array([[4, 4, 4, 4, 4, 4, 4],\n",
495
+ " [4, 4, 4, 4, 4, 4, 4]])"
496
+ ]
497
+ },
498
+ "execution_count": 24,
499
+ "metadata": {},
500
+ "output_type": "execute_result"
501
+ }
502
+ ],
503
+ "source": [
504
+ "# Any other number (full_like)\n",
505
+ "np.full_like(a, 4)"
506
+ ]
507
+ },
508
+ {
509
+ "cell_type": "code",
510
+ "execution_count": 25,
511
+ "metadata": {},
512
+ "outputs": [
513
+ {
514
+ "data": {
515
+ "text/plain": [
516
+ "array([[0.24480678, 0.71347348],\n",
517
+ " [0.56163517, 0.80732991],\n",
518
+ " [0.72750015, 0.65200353],\n",
519
+ " [0.13660036, 0.92045687]])"
520
+ ]
521
+ },
522
+ "execution_count": 25,
523
+ "metadata": {},
524
+ "output_type": "execute_result"
525
+ }
526
+ ],
527
+ "source": [
528
+ "# Random decimal numbers\n",
529
+ "np.random.rand(4,2)"
530
+ ]
531
+ },
532
+ {
533
+ "cell_type": "code",
534
+ "execution_count": 26,
535
+ "metadata": {},
536
+ "outputs": [
537
+ {
538
+ "data": {
539
+ "text/plain": [
540
+ "array([[-4, 2, 6],\n",
541
+ " [ 6, -4, -3],\n",
542
+ " [ 3, 2, 4]])"
543
+ ]
544
+ },
545
+ "execution_count": 26,
546
+ "metadata": {},
547
+ "output_type": "execute_result"
548
+ }
549
+ ],
550
+ "source": [
551
+ "# Random Integer values\n",
552
+ "np.random.randint(-4,8, size=(3,3))"
553
+ ]
554
+ },
555
+ {
556
+ "cell_type": "code",
557
+ "execution_count": 27,
558
+ "metadata": {},
559
+ "outputs": [
560
+ {
561
+ "data": {
562
+ "text/plain": [
563
+ "array([[1., 0., 0., 0., 0.],\n",
564
+ " [0., 1., 0., 0., 0.],\n",
565
+ " [0., 0., 1., 0., 0.],\n",
566
+ " [0., 0., 0., 1., 0.],\n",
567
+ " [0., 0., 0., 0., 1.]])"
568
+ ]
569
+ },
570
+ "execution_count": 27,
571
+ "metadata": {},
572
+ "output_type": "execute_result"
573
+ }
574
+ ],
575
+ "source": [
576
+ "# The identity matrix\n",
577
+ "np.identity(5)"
578
+ ]
579
+ },
580
+ {
581
+ "cell_type": "code",
582
+ "execution_count": 28,
583
+ "metadata": {},
584
+ "outputs": [
585
+ {
586
+ "name": "stdout",
587
+ "output_type": "stream",
588
+ "text": [
589
+ "[[1 2 3]\n",
590
+ " [1 2 3]\n",
591
+ " [1 2 3]]\n"
592
+ ]
593
+ }
594
+ ],
595
+ "source": [
596
+ "# Repeat an array\n",
597
+ "arr = np.array([[1,2,3]])\n",
598
+ "r1 = np.repeat(arr,3, axis=0)\n",
599
+ "print(r1)"
600
+ ]
601
+ },
602
+ {
603
+ "cell_type": "code",
604
+ "execution_count": 29,
605
+ "metadata": {},
606
+ "outputs": [
607
+ {
608
+ "name": "stdout",
609
+ "output_type": "stream",
610
+ "text": [
611
+ "[[1. 1. 1. 1. 1.]\n",
612
+ " [1. 1. 1. 1. 1.]\n",
613
+ " [1. 1. 1. 1. 1.]\n",
614
+ " [1. 1. 1. 1. 1.]\n",
615
+ " [1. 1. 1. 1. 1.]]\n",
616
+ "[[0. 0. 0.]\n",
617
+ " [0. 9. 0.]\n",
618
+ " [0. 0. 0.]]\n",
619
+ "[[1. 1. 1. 1. 1.]\n",
620
+ " [1. 0. 0. 0. 1.]\n",
621
+ " [1. 0. 9. 0. 1.]\n",
622
+ " [1. 0. 0. 0. 1.]\n",
623
+ " [1. 1. 1. 1. 1.]]\n"
624
+ ]
625
+ }
626
+ ],
627
+ "source": [
628
+ "output = np.ones((5,5))\n",
629
+ "print(output)\n",
630
+ "\n",
631
+ "z = np.zeros((3,3))\n",
632
+ "z[1,1] = 9\n",
633
+ "print(z)\n",
634
+ "\n",
635
+ "output[1:-1,1:-1] = z\n",
636
+ "print(output)"
637
+ ]
638
+ },
639
+ {
640
+ "cell_type": "markdown",
641
+ "metadata": {},
642
+ "source": [
643
+ "##### Be careful when copying arrays!!!"
644
+ ]
645
+ },
646
+ {
647
+ "cell_type": "code",
648
+ "execution_count": 30,
649
+ "metadata": {},
650
+ "outputs": [
651
+ {
652
+ "name": "stdout",
653
+ "output_type": "stream",
654
+ "text": [
655
+ "[1 2 3]\n"
656
+ ]
657
+ }
658
+ ],
659
+ "source": [
660
+ "a = np.array([1,2,3])\n",
661
+ "b = a.copy()\n",
662
+ "b[0] = 100\n",
663
+ "\n",
664
+ "print(a)"
665
+ ]
666
+ },
667
+ {
668
+ "cell_type": "markdown",
669
+ "metadata": {},
670
+ "source": [
671
+ "### Mathematics"
672
+ ]
673
+ },
674
+ {
675
+ "cell_type": "code",
676
+ "execution_count": 31,
677
+ "metadata": {},
678
+ "outputs": [
679
+ {
680
+ "name": "stdout",
681
+ "output_type": "stream",
682
+ "text": [
683
+ "[1 2 3 4]\n"
684
+ ]
685
+ }
686
+ ],
687
+ "source": [
688
+ "a = np.array([1,2,3,4])\n",
689
+ "print(a)"
690
+ ]
691
+ },
692
+ {
693
+ "cell_type": "code",
694
+ "execution_count": 32,
695
+ "metadata": {},
696
+ "outputs": [
697
+ {
698
+ "data": {
699
+ "text/plain": [
700
+ "array([3, 4, 5, 6])"
701
+ ]
702
+ },
703
+ "execution_count": 32,
704
+ "metadata": {},
705
+ "output_type": "execute_result"
706
+ }
707
+ ],
708
+ "source": [
709
+ "a + 2"
710
+ ]
711
+ },
712
+ {
713
+ "cell_type": "code",
714
+ "execution_count": 33,
715
+ "metadata": {},
716
+ "outputs": [
717
+ {
718
+ "data": {
719
+ "text/plain": [
720
+ "array([-1, 0, 1, 2])"
721
+ ]
722
+ },
723
+ "execution_count": 33,
724
+ "metadata": {},
725
+ "output_type": "execute_result"
726
+ }
727
+ ],
728
+ "source": [
729
+ "a - 2"
730
+ ]
731
+ },
732
+ {
733
+ "cell_type": "code",
734
+ "execution_count": 34,
735
+ "metadata": {},
736
+ "outputs": [
737
+ {
738
+ "data": {
739
+ "text/plain": [
740
+ "array([2, 4, 6, 8])"
741
+ ]
742
+ },
743
+ "execution_count": 34,
744
+ "metadata": {},
745
+ "output_type": "execute_result"
746
+ }
747
+ ],
748
+ "source": [
749
+ "a * 2"
750
+ ]
751
+ },
752
+ {
753
+ "cell_type": "code",
754
+ "execution_count": 35,
755
+ "metadata": {},
756
+ "outputs": [
757
+ {
758
+ "data": {
759
+ "text/plain": [
760
+ "array([0.5, 1. , 1.5, 2. ])"
761
+ ]
762
+ },
763
+ "execution_count": 35,
764
+ "metadata": {},
765
+ "output_type": "execute_result"
766
+ }
767
+ ],
768
+ "source": [
769
+ "a / 2"
770
+ ]
771
+ },
772
+ {
773
+ "cell_type": "code",
774
+ "execution_count": 36,
775
+ "metadata": {},
776
+ "outputs": [
777
+ {
778
+ "data": {
779
+ "text/plain": [
780
+ "array([2, 2, 4, 4])"
781
+ ]
782
+ },
783
+ "execution_count": 36,
784
+ "metadata": {},
785
+ "output_type": "execute_result"
786
+ }
787
+ ],
788
+ "source": [
789
+ "b = np.array([1,0,1,0])\n",
790
+ "a + b"
791
+ ]
792
+ },
793
+ {
794
+ "cell_type": "code",
795
+ "execution_count": 37,
796
+ "metadata": {},
797
+ "outputs": [
798
+ {
799
+ "data": {
800
+ "text/plain": [
801
+ "array([ 1, 4, 9, 16])"
802
+ ]
803
+ },
804
+ "execution_count": 37,
805
+ "metadata": {},
806
+ "output_type": "execute_result"
807
+ }
808
+ ],
809
+ "source": [
810
+ "a ** 2"
811
+ ]
812
+ },
813
+ {
814
+ "cell_type": "code",
815
+ "execution_count": 38,
816
+ "metadata": {},
817
+ "outputs": [
818
+ {
819
+ "data": {
820
+ "text/plain": [
821
+ "array([ 0.54030231, -0.41614684, -0.9899925 , -0.65364362])"
822
+ ]
823
+ },
824
+ "execution_count": 38,
825
+ "metadata": {},
826
+ "output_type": "execute_result"
827
+ }
828
+ ],
829
+ "source": [
830
+ "# Take the sin\n",
831
+ "np.cos(a)\n",
832
+ "\n"
833
+ ]
834
+ },
835
+ {
836
+ "cell_type": "markdown",
837
+ "metadata": {},
838
+ "source": [
839
+ "##### Linear Algebra"
840
+ ]
841
+ },
842
+ {
843
+ "cell_type": "code",
844
+ "execution_count": 40,
845
+ "metadata": {},
846
+ "outputs": [
847
+ {
848
+ "name": "stdout",
849
+ "output_type": "stream",
850
+ "text": [
851
+ "[[1. 1. 1.]\n",
852
+ " [1. 1. 1.]]\n",
853
+ "[[2 2]\n",
854
+ " [2 2]\n",
855
+ " [2 2]]\n"
856
+ ]
857
+ },
858
+ {
859
+ "data": {
860
+ "text/plain": [
861
+ "array([[6., 6.],\n",
862
+ " [6., 6.]])"
863
+ ]
864
+ },
865
+ "execution_count": 40,
866
+ "metadata": {},
867
+ "output_type": "execute_result"
868
+ }
869
+ ],
870
+ "source": [
871
+ "a = np.ones((2,3))\n",
872
+ "print(a)\n",
873
+ "\n",
874
+ "b = np.full((3,2), 2)\n",
875
+ "print(b)\n",
876
+ "\n",
877
+ "np.matmul(a,b)"
878
+ ]
879
+ },
880
+ {
881
+ "cell_type": "code",
882
+ "execution_count": 41,
883
+ "metadata": {},
884
+ "outputs": [
885
+ {
886
+ "data": {
887
+ "text/plain": [
888
+ "1.0"
889
+ ]
890
+ },
891
+ "execution_count": 41,
892
+ "metadata": {},
893
+ "output_type": "execute_result"
894
+ }
895
+ ],
896
+ "source": [
897
+ "# Find the determinant\n",
898
+ "c = np.identity(3)\n",
899
+ "np.linalg.det(c)"
900
+ ]
901
+ },
902
+ {
903
+ "cell_type": "markdown",
904
+ "metadata": {},
905
+ "source": [
906
+ "##### Statistics"
907
+ ]
908
+ },
909
+ {
910
+ "cell_type": "code",
911
+ "execution_count": 43,
912
+ "metadata": {},
913
+ "outputs": [
914
+ {
915
+ "data": {
916
+ "text/plain": [
917
+ "array([[1, 2, 3],\n",
918
+ " [4, 5, 6]])"
919
+ ]
920
+ },
921
+ "execution_count": 43,
922
+ "metadata": {},
923
+ "output_type": "execute_result"
924
+ }
925
+ ],
926
+ "source": [
927
+ "stats = np.array([[1,2,3],[4,5,6]])\n",
928
+ "stats"
929
+ ]
930
+ },
931
+ {
932
+ "cell_type": "code",
933
+ "execution_count": 44,
934
+ "metadata": {},
935
+ "outputs": [
936
+ {
937
+ "data": {
938
+ "text/plain": [
939
+ "1"
940
+ ]
941
+ },
942
+ "execution_count": 44,
943
+ "metadata": {},
944
+ "output_type": "execute_result"
945
+ }
946
+ ],
947
+ "source": [
948
+ "np.min(stats)"
949
+ ]
950
+ },
951
+ {
952
+ "cell_type": "code",
953
+ "execution_count": 45,
954
+ "metadata": {},
955
+ "outputs": [
956
+ {
957
+ "data": {
958
+ "text/plain": [
959
+ "array([3, 6])"
960
+ ]
961
+ },
962
+ "execution_count": 45,
963
+ "metadata": {},
964
+ "output_type": "execute_result"
965
+ }
966
+ ],
967
+ "source": [
968
+ "np.max(stats, axis=1)"
969
+ ]
970
+ },
971
+ {
972
+ "cell_type": "code",
973
+ "execution_count": 46,
974
+ "metadata": {},
975
+ "outputs": [
976
+ {
977
+ "data": {
978
+ "text/plain": [
979
+ "array([5, 7, 9])"
980
+ ]
981
+ },
982
+ "execution_count": 46,
983
+ "metadata": {},
984
+ "output_type": "execute_result"
985
+ }
986
+ ],
987
+ "source": [
988
+ "np.sum(stats, axis=0)"
989
+ ]
990
+ },
991
+ {
992
+ "cell_type": "markdown",
993
+ "metadata": {},
994
+ "source": [
995
+ "### Reorganizing Arrays"
996
+ ]
997
+ },
998
+ {
999
+ "cell_type": "code",
1000
+ "execution_count": 49,
1001
+ "metadata": {},
1002
+ "outputs": [
1003
+ {
1004
+ "name": "stdout",
1005
+ "output_type": "stream",
1006
+ "text": [
1007
+ "[[1 2 3 4]\n",
1008
+ " [5 6 7 8]]\n",
1009
+ "[[1]\n",
1010
+ " [2]\n",
1011
+ " [3]\n",
1012
+ " [4]\n",
1013
+ " [5]\n",
1014
+ " [6]\n",
1015
+ " [7]\n",
1016
+ " [8]]\n"
1017
+ ]
1018
+ }
1019
+ ],
1020
+ "source": [
1021
+ "before = np.array([[1,2,3,4],[5,6,7,8]])\n",
1022
+ "print(before)\n",
1023
+ "\n",
1024
+ "after = before.reshape((8,1))\n",
1025
+ "print(after)"
1026
+ ]
1027
+ },
1028
+ {
1029
+ "cell_type": "code",
1030
+ "execution_count": 50,
1031
+ "metadata": {},
1032
+ "outputs": [
1033
+ {
1034
+ "data": {
1035
+ "text/plain": [
1036
+ "array([[1, 2, 3, 4],\n",
1037
+ " [5, 6, 7, 8],\n",
1038
+ " [1, 2, 3, 4],\n",
1039
+ " [5, 6, 7, 8]])"
1040
+ ]
1041
+ },
1042
+ "execution_count": 50,
1043
+ "metadata": {},
1044
+ "output_type": "execute_result"
1045
+ }
1046
+ ],
1047
+ "source": [
1048
+ "# Vertically stacking vectors\n",
1049
+ "v1 = np.array([1,2,3,4])\n",
1050
+ "v2 = np.array([5,6,7,8])\n",
1051
+ "\n",
1052
+ "np.vstack([v1,v2,v1,v2])"
1053
+ ]
1054
+ },
1055
+ {
1056
+ "cell_type": "code",
1057
+ "execution_count": 51,
1058
+ "metadata": {},
1059
+ "outputs": [
1060
+ {
1061
+ "data": {
1062
+ "text/plain": [
1063
+ "array([[1., 1., 1., 1., 0., 0.],\n",
1064
+ " [1., 1., 1., 1., 0., 0.]])"
1065
+ ]
1066
+ },
1067
+ "execution_count": 51,
1068
+ "metadata": {},
1069
+ "output_type": "execute_result"
1070
+ }
1071
+ ],
1072
+ "source": [
1073
+ "# Horizontal stack\n",
1074
+ "h1 = np.ones((2,4))\n",
1075
+ "h2 = np.zeros((2,2))\n",
1076
+ "\n",
1077
+ "np.hstack((h1,h2))"
1078
+ ]
1079
+ },
1080
+ {
1081
+ "cell_type": "code",
1082
+ "execution_count": null,
1083
+ "metadata": {},
1084
+ "outputs": [],
1085
+ "source": []
1086
+ },
1087
+ {
1088
+ "cell_type": "code",
1089
+ "execution_count": null,
1090
+ "metadata": {},
1091
+ "outputs": [],
1092
+ "source": []
1093
+ }
1094
+ ],
1095
+ "metadata": {
1096
+ "kernelspec": {
1097
+ "display_name": "Python 3 (ipykernel)",
1098
+ "language": "python",
1099
+ "name": "python3"
1100
+ },
1101
+ "language_info": {
1102
+ "codemirror_mode": {
1103
+ "name": "ipython",
1104
+ "version": 3
1105
+ },
1106
+ "file_extension": ".py",
1107
+ "mimetype": "text/x-python",
1108
+ "name": "python",
1109
+ "nbconvert_exporter": "python",
1110
+ "pygments_lexer": "ipython3",
1111
+ "version": "3.10.9"
1112
+ }
1113
+ },
1114
+ "nbformat": 4,
1115
+ "nbformat_minor": 2
1116
+ }
README.md ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Numpy_Basics
2
+ ![logo](https://github.com/prabhakarvenkat/Numpy_Basics/blob/4784ae0b600d35a1fe12375a41b840ce37bb7643/numpy.png)
3
+ <h2>What is NumPy?</h2>
4
+ <p>NumPy is a Python library used for working with arrays.
5
+
6
+ It also has functions for working in domain of linear algebra, fourier transform, and matrices.
7
+
8
+ NumPy was created in 2005 by Travis Oliphant. It is an open source project and you can use it freely.
9
+
10
+ NumPy stands for Numerical Python.</p>
11
+ --------------------------------------------------------------------------------------------------------------------------------------------------
12
+ <h2>Why Use NumPy?</h2>
13
+ <p>In Python we have lists that serve the purpose of arrays, but they are slow to process.
14
+
15
+ NumPy aims to provide an array object that is up to 50x faster than traditional Python lists.
16
+
17
+ The array object in NumPy is called ndarray, it provides a lot of supporting functions that make working with ndarray very easy.
18
+
19
+ Arrays are very frequently used in data science, where speed and resources are very important.</p>
20
+ --------------------------------------------------------------------------------------------------------------------------------------------------
21
+ <h2>Why is NumPy Faster Than Lists?</h2>
22
+ <p>NumPy arrays are stored at one continuous place in memory unlike lists, so processes can access and manipulate them very efficiently.
23
+
24
+ This behavior is called locality of reference in computer science.
25
+
26
+ This is the main reason why NumPy is faster than lists. Also it is optimized to work with latest CPU architectures.</p>
27
+ --------------------------------------------------------------------------------------------------------------------------------------------------
28
+ <h2>Which Language is NumPy written in?</h2>
29
+ <p>NumPy is a Python library and is written partially in Python, but most of the parts that require fast computation are written in C or C++.</p>
numpy.png ADDED