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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "5d2d2387",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import numpy as np"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": 8,
16
+ "id": "0c5af0b5",
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "arr1=np.array((1,2,4,7,8,9,4,5,6,7,9))"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": 9,
26
+ "id": "2e0d0565",
27
+ "metadata": {},
28
+ "outputs": [
29
+ {
30
+ "name": "stdout",
31
+ "output_type": "stream",
32
+ "text": [
33
+ "single dimentional: [1 2 4 7 8 9 4 5 6 7 9]\n"
34
+ ]
35
+ }
36
+ ],
37
+ "source": [
38
+ "print(\"single dimentional:\",arr1)"
39
+ ]
40
+ },
41
+ {
42
+ "cell_type": "code",
43
+ "execution_count": 6,
44
+ "id": "51a43691",
45
+ "metadata": {},
46
+ "outputs": [
47
+ {
48
+ "data": {
49
+ "text/plain": [
50
+ "dtype('int64')"
51
+ ]
52
+ },
53
+ "execution_count": 6,
54
+ "metadata": {},
55
+ "output_type": "execute_result"
56
+ }
57
+ ],
58
+ "source": [
59
+ "arr1.dtype"
60
+ ]
61
+ },
62
+ {
63
+ "cell_type": "code",
64
+ "execution_count": 11,
65
+ "id": "f6520b45",
66
+ "metadata": {},
67
+ "outputs": [
68
+ {
69
+ "name": "stdout",
70
+ "output_type": "stream",
71
+ "text": [
72
+ "2-d arrays:\n",
73
+ " [[ 10 20 30 40]\n",
74
+ " [100 200 300 400]]\n"
75
+ ]
76
+ }
77
+ ],
78
+ "source": [
79
+ "arr2=np.array([[10,20,30,40],[100,200,300,400]])\n",
80
+ "print(\"2-d arrays:\\n\",arr2)"
81
+ ]
82
+ },
83
+ {
84
+ "cell_type": "code",
85
+ "execution_count": 12,
86
+ "id": "319161ca",
87
+ "metadata": {},
88
+ "outputs": [
89
+ {
90
+ "name": "stdout",
91
+ "output_type": "stream",
92
+ "text": [
93
+ "arr1: 1\n",
94
+ "arr2: 2\n"
95
+ ]
96
+ }
97
+ ],
98
+ "source": [
99
+ "#checking thair dimensions:\n",
100
+ "print(\"arr1:\",arr1.ndim)\n",
101
+ "print(\"arr2:\",arr2.ndim)"
102
+ ]
103
+ },
104
+ {
105
+ "cell_type": "code",
106
+ "execution_count": 15,
107
+ "id": "c71c8ff9",
108
+ "metadata": {},
109
+ "outputs": [
110
+ {
111
+ "name": "stdout",
112
+ "output_type": "stream",
113
+ "text": [
114
+ "3-d array:\n",
115
+ " [[[ 1 2 3 4]\n",
116
+ " [ 10 20 30 40]\n",
117
+ " [100 200 300 400]]]\n",
118
+ "arr3: 3\n"
119
+ ]
120
+ }
121
+ ],
122
+ "source": [
123
+ "arr3=np.array([[[1,2,3,4],[10,20,30,40],[100,200,300,400]]])\n",
124
+ "print(\"3-d array:\\n\",arr3)\n",
125
+ "print(\"arr3:\",arr3.ndim)"
126
+ ]
127
+ },
128
+ {
129
+ "cell_type": "code",
130
+ "execution_count": 16,
131
+ "id": "a426eb80",
132
+ "metadata": {},
133
+ "outputs": [],
134
+ "source": [
135
+ "#for casual cheking with data frames :\n",
136
+ "import pandas as pd"
137
+ ]
138
+ },
139
+ {
140
+ "cell_type": "code",
141
+ "execution_count": 25,
142
+ "id": "3c61781d",
143
+ "metadata": {},
144
+ "outputs": [
145
+ {
146
+ "name": "stdout",
147
+ "output_type": "stream",
148
+ "text": [
149
+ "0 1\n",
150
+ "1 2\n",
151
+ "2 4\n",
152
+ "3 7\n",
153
+ "4 8\n",
154
+ "5 9\n",
155
+ "6 4\n",
156
+ "7 5\n",
157
+ "8 6\n",
158
+ "9 7\n",
159
+ "10 9\n",
160
+ "dtype: int64\n"
161
+ ]
162
+ }
163
+ ],
164
+ "source": [
165
+ "d_frame=pd.Series(arr1)\n",
166
+ "print(d_frame)"
167
+ ]
168
+ },
169
+ {
170
+ "cell_type": "code",
171
+ "execution_count": 26,
172
+ "id": "aeed557b",
173
+ "metadata": {},
174
+ "outputs": [
175
+ {
176
+ "name": "stdout",
177
+ "output_type": "stream",
178
+ "text": [
179
+ " 0 1 2 3\n",
180
+ "0 10 20 30 40\n",
181
+ "1 100 200 300 400\n"
182
+ ]
183
+ }
184
+ ],
185
+ "source": [
186
+ "#array to data frame\n",
187
+ "#here the data frame support only the 2-d arry for printing other wise the print function will show error message:\n",
188
+ "d_frame=pd.DataFrame(arr2)\n",
189
+ "print(d_frame)"
190
+ ]
191
+ },
192
+ {
193
+ "cell_type": "code",
194
+ "execution_count": 27,
195
+ "id": "88ae926a",
196
+ "metadata": {},
197
+ "outputs": [
198
+ {
199
+ "data": {
200
+ "text/plain": [
201
+ "(2, 4)"
202
+ ]
203
+ },
204
+ "execution_count": 27,
205
+ "metadata": {},
206
+ "output_type": "execute_result"
207
+ }
208
+ ],
209
+ "source": [
210
+ "d_frame.shape"
211
+ ]
212
+ },
213
+ {
214
+ "cell_type": "code",
215
+ "execution_count": 28,
216
+ "id": "5d15a9ed",
217
+ "metadata": {},
218
+ "outputs": [],
219
+ "source": [
220
+ "#data frame to array\n",
221
+ "check1=np.array(d_frame)"
222
+ ]
223
+ },
224
+ {
225
+ "cell_type": "code",
226
+ "execution_count": 29,
227
+ "id": "c4c87770",
228
+ "metadata": {},
229
+ "outputs": [
230
+ {
231
+ "name": "stdout",
232
+ "output_type": "stream",
233
+ "text": [
234
+ "[[ 10 20 30 40]\n",
235
+ " [100 200 300 400]]\n"
236
+ ]
237
+ }
238
+ ],
239
+ "source": [
240
+ "print(check1)"
241
+ ]
242
+ },
243
+ {
244
+ "cell_type": "code",
245
+ "execution_count": 34,
246
+ "id": "e84c9006",
247
+ "metadata": {},
248
+ "outputs": [
249
+ {
250
+ "name": "stdout",
251
+ "output_type": "stream",
252
+ "text": [
253
+ "1-D:\t [1 2 4 7 8 9 4 5 6 7 9] \n",
254
+ "\n",
255
+ "2-D:\t [[ 10 20 30 40]\n",
256
+ " [100 200 300 400]] \n",
257
+ "\n",
258
+ "3-D:\t [[[ 1 2 3 4]\n",
259
+ " [ 10 20 30 40]\n",
260
+ " [100 200 300 400]]]\n"
261
+ ]
262
+ }
263
+ ],
264
+ "source": [
265
+ "print(\"1-D:\\t\",arr1,\"\\n\")\n",
266
+ "print(\"2-D:\\t\",arr2,\"\\n\")\n",
267
+ "print(\"3-D:\\t\",arr3)"
268
+ ]
269
+ },
270
+ {
271
+ "cell_type": "code",
272
+ "execution_count": 41,
273
+ "id": "df3e081a",
274
+ "metadata": {},
275
+ "outputs": [
276
+ {
277
+ "name": "stdout",
278
+ "output_type": "stream",
279
+ "text": [
280
+ "[1 2 4 7 8 9 4 5 6 7 9]\n",
281
+ "[1 2 4 7 8 9 4]\n"
282
+ ]
283
+ }
284
+ ],
285
+ "source": [
286
+ "#nupy array indexing with step value:\n",
287
+ "print(arr1)\n",
288
+ "print(arr1[0:7])"
289
+ ]
290
+ },
291
+ {
292
+ "cell_type": "code",
293
+ "execution_count": 44,
294
+ "id": "ab12aa05",
295
+ "metadata": {},
296
+ "outputs": [
297
+ {
298
+ "name": "stdout",
299
+ "output_type": "stream",
300
+ "text": [
301
+ "first half of the elements: [1 2 4 7 8]\n"
302
+ ]
303
+ }
304
+ ],
305
+ "source": [
306
+ "#half of the elements are printing:\n",
307
+ "print(\"first half of the elements:\",arr1[:len(arr1)//2])"
308
+ ]
309
+ },
310
+ {
311
+ "cell_type": "code",
312
+ "execution_count": 45,
313
+ "id": "55929e0a",
314
+ "metadata": {},
315
+ "outputs": [
316
+ {
317
+ "name": "stdout",
318
+ "output_type": "stream",
319
+ "text": [
320
+ "1\n",
321
+ "2\n",
322
+ "4\n",
323
+ "7\n",
324
+ "8\n",
325
+ "9\n",
326
+ "4\n",
327
+ "5\n",
328
+ "6\n",
329
+ "7\n",
330
+ "9\n"
331
+ ]
332
+ }
333
+ ],
334
+ "source": [
335
+ "for i in arr1:\n",
336
+ " print(i)"
337
+ ]
338
+ },
339
+ {
340
+ "cell_type": "code",
341
+ "execution_count": 49,
342
+ "id": "0e15e750",
343
+ "metadata": {},
344
+ "outputs": [
345
+ {
346
+ "name": "stdout",
347
+ "output_type": "stream",
348
+ "text": [
349
+ "1\n",
350
+ "2\n",
351
+ "3\n",
352
+ "4\n",
353
+ "10\n",
354
+ "20\n",
355
+ "30\n",
356
+ "40\n",
357
+ "100\n",
358
+ "200\n",
359
+ "300\n",
360
+ "400\n"
361
+ ]
362
+ }
363
+ ],
364
+ "source": [
365
+ "#iterating the 3-D array in one loop:\n",
366
+ "for i in np.nditer(arr3[:,::1]):\n",
367
+ " print(i)"
368
+ ]
369
+ },
370
+ {
371
+ "cell_type": "code",
372
+ "execution_count": 67,
373
+ "id": "141305fd",
374
+ "metadata": {},
375
+ "outputs": [
376
+ {
377
+ "data": {
378
+ "text/plain": [
379
+ "(2, 4)"
380
+ ]
381
+ },
382
+ "execution_count": 67,
383
+ "metadata": {},
384
+ "output_type": "execute_result"
385
+ }
386
+ ],
387
+ "source": [
388
+ "arr2.shape"
389
+ ]
390
+ },
391
+ {
392
+ "cell_type": "code",
393
+ "execution_count": 68,
394
+ "id": "a8cf04d8",
395
+ "metadata": {},
396
+ "outputs": [
397
+ {
398
+ "data": {
399
+ "text/plain": [
400
+ "(1, 3, 4)"
401
+ ]
402
+ },
403
+ "execution_count": 68,
404
+ "metadata": {},
405
+ "output_type": "execute_result"
406
+ }
407
+ ],
408
+ "source": [
409
+ "#doubt:ask to sir:\n",
410
+ "arr3.shape"
411
+ ]
412
+ },
413
+ {
414
+ "cell_type": "code",
415
+ "execution_count": 69,
416
+ "id": "661a05b5",
417
+ "metadata": {},
418
+ "outputs": [
419
+ {
420
+ "data": {
421
+ "text/plain": [
422
+ "3"
423
+ ]
424
+ },
425
+ "execution_count": 69,
426
+ "metadata": {},
427
+ "output_type": "execute_result"
428
+ }
429
+ ],
430
+ "source": [
431
+ "np.ndim(arr3)"
432
+ ]
433
+ },
434
+ {
435
+ "cell_type": "code",
436
+ "execution_count": 70,
437
+ "id": "2e53a428",
438
+ "metadata": {},
439
+ "outputs": [
440
+ {
441
+ "data": {
442
+ "text/plain": [
443
+ "(11,)"
444
+ ]
445
+ },
446
+ "execution_count": 70,
447
+ "metadata": {},
448
+ "output_type": "execute_result"
449
+ }
450
+ ],
451
+ "source": [
452
+ "arr1.shape"
453
+ ]
454
+ },
455
+ {
456
+ "cell_type": "code",
457
+ "execution_count": 73,
458
+ "id": "f52621d0",
459
+ "metadata": {},
460
+ "outputs": [
461
+ {
462
+ "name": "stdout",
463
+ "output_type": "stream",
464
+ "text": [
465
+ "10\n"
466
+ ]
467
+ }
468
+ ],
469
+ "source": [
470
+ "#accessign the nd arry elements:\n",
471
+ "#element - 1st row and first column\n",
472
+ "print(arr2[0,0])"
473
+ ]
474
+ },
475
+ {
476
+ "cell_type": "code",
477
+ "execution_count": 76,
478
+ "id": "78f2bb26",
479
+ "metadata": {},
480
+ "outputs": [
481
+ {
482
+ "name": "stdout",
483
+ "output_type": "stream",
484
+ "text": [
485
+ "300\n"
486
+ ]
487
+ }
488
+ ],
489
+ "source": [
490
+ "#3-d arrray:\n",
491
+ "print(arr3[0,2,2])"
492
+ ]
493
+ },
494
+ {
495
+ "cell_type": "code",
496
+ "execution_count": 81,
497
+ "id": "f1dcee88",
498
+ "metadata": {},
499
+ "outputs": [
500
+ {
501
+ "name": "stdout",
502
+ "output_type": "stream",
503
+ "text": [
504
+ "[ 10 20 400 183 142]\n"
505
+ ]
506
+ }
507
+ ],
508
+ "source": [
509
+ "#for the absolute we willl create another array with -ve value\n",
510
+ "arr4=np.array([-10,-20,-400,-183,142])\n",
511
+ "print(abs(arr4))"
512
+ ]
513
+ },
514
+ {
515
+ "cell_type": "code",
516
+ "execution_count": 82,
517
+ "id": "27348c65",
518
+ "metadata": {},
519
+ "outputs": [],
520
+ "source": [
521
+ "#reshaping\n",
522
+ "#add or remove from the existing array"
523
+ ]
524
+ },
525
+ {
526
+ "cell_type": "code",
527
+ "execution_count": 84,
528
+ "id": "6248d02a",
529
+ "metadata": {},
530
+ "outputs": [
531
+ {
532
+ "data": {
533
+ "text/plain": [
534
+ "[array([1, 2, 4, 7, 8, 9]), array([4, 5, 6, 7, 9])]"
535
+ ]
536
+ },
537
+ "execution_count": 84,
538
+ "metadata": {},
539
+ "output_type": "execute_result"
540
+ }
541
+ ],
542
+ "source": [
543
+ "np.array_split(arr1,2)"
544
+ ]
545
+ },
546
+ {
547
+ "cell_type": "code",
548
+ "execution_count": 87,
549
+ "id": "072c9d81",
550
+ "metadata": {},
551
+ "outputs": [
552
+ {
553
+ "data": {
554
+ "text/plain": [
555
+ "[array([[10, 20, 30, 40]]), array([[100, 200, 300, 400]])]"
556
+ ]
557
+ },
558
+ "execution_count": 87,
559
+ "metadata": {},
560
+ "output_type": "execute_result"
561
+ }
562
+ ],
563
+ "source": [
564
+ "np.array_split(arr2,2)"
565
+ ]
566
+ },
567
+ {
568
+ "cell_type": "code",
569
+ "execution_count": 88,
570
+ "id": "4883ec53",
571
+ "metadata": {},
572
+ "outputs": [
573
+ {
574
+ "data": {
575
+ "text/plain": [
576
+ "[array([[[ 1, 2, 3, 4],\n",
577
+ " [ 10, 20, 30, 40],\n",
578
+ " [100, 200, 300, 400]]]),\n",
579
+ " array([], shape=(0, 3, 4), dtype=int64),\n",
580
+ " array([], shape=(0, 3, 4), dtype=int64)]"
581
+ ]
582
+ },
583
+ "execution_count": 88,
584
+ "metadata": {},
585
+ "output_type": "execute_result"
586
+ }
587
+ ],
588
+ "source": [
589
+ "np.array_split(arr3,3)"
590
+ ]
591
+ },
592
+ {
593
+ "cell_type": "code",
594
+ "execution_count": 90,
595
+ "id": "02a8971e",
596
+ "metadata": {},
597
+ "outputs": [
598
+ {
599
+ "data": {
600
+ "text/plain": [
601
+ "[array([1, 2, 4]), array([7, 8]), array([9, 4]), array([5, 6]), array([7, 9])]"
602
+ ]
603
+ },
604
+ "execution_count": 90,
605
+ "metadata": {},
606
+ "output_type": "execute_result"
607
+ }
608
+ ],
609
+ "source": [
610
+ "np.array_split(arr1,5)"
611
+ ]
612
+ },
613
+ {
614
+ "cell_type": "code",
615
+ "execution_count": 103,
616
+ "id": "e5a999ce",
617
+ "metadata": {},
618
+ "outputs": [
619
+ {
620
+ "name": "stdout",
621
+ "output_type": "stream",
622
+ "text": [
623
+ "5\n"
624
+ ]
625
+ }
626
+ ],
627
+ "source": [
628
+ "print(len(arr4))"
629
+ ]
630
+ },
631
+ {
632
+ "cell_type": "code",
633
+ "execution_count": 106,
634
+ "id": "7b58a5c0",
635
+ "metadata": {},
636
+ "outputs": [
637
+ {
638
+ "name": "stdout",
639
+ "output_type": "stream",
640
+ "text": [
641
+ "[[[ 1 2]\n",
642
+ " [ 3 4]\n",
643
+ " [ 5 6]]\n",
644
+ "\n",
645
+ " [[ 7 8]\n",
646
+ " [ 9 10]\n",
647
+ " [11 12]]]\n"
648
+ ]
649
+ }
650
+ ],
651
+ "source": [
652
+ "arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])\n",
653
+ "\n",
654
+ "newarr = arr.reshape(2, 3, 2)\n",
655
+ "print(newarr)"
656
+ ]
657
+ },
658
+ {
659
+ "cell_type": "code",
660
+ "execution_count": 107,
661
+ "id": "4bb019ce",
662
+ "metadata": {},
663
+ "outputs": [
664
+ {
665
+ "name": "stdout",
666
+ "output_type": "stream",
667
+ "text": [
668
+ "[1 2 4 7 8 9 4 5 6 7 9]\n"
669
+ ]
670
+ }
671
+ ],
672
+ "source": [
673
+ "#splitting the array:\n",
674
+ "print(arr1)"
675
+ ]
676
+ },
677
+ {
678
+ "cell_type": "code",
679
+ "execution_count": 109,
680
+ "id": "6fbdf5b6",
681
+ "metadata": {},
682
+ "outputs": [],
683
+ "source": [
684
+ "newarr2=np.array_split(arr1,3)"
685
+ ]
686
+ },
687
+ {
688
+ "cell_type": "code",
689
+ "execution_count": 111,
690
+ "id": "a254994b",
691
+ "metadata": {},
692
+ "outputs": [
693
+ {
694
+ "ename": "TypeError",
695
+ "evalue": "only integer scalar arrays can be converted to a scalar index",
696
+ "output_type": "error",
697
+ "traceback": [
698
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
699
+ "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
700
+ "Input \u001b[0;32mIn [111]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m newarr2:\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mnewarr2\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m)\n",
701
+ "\u001b[0;31mTypeError\u001b[0m: only integer scalar arrays can be converted to a scalar index"
702
+ ]
703
+ }
704
+ ],
705
+ "source": [
706
+ "for i in newarr2:\n",
707
+ " print(newarr2[i])"
708
+ ]
709
+ },
710
+ {
711
+ "cell_type": "code",
712
+ "execution_count": 117,
713
+ "id": "45ede19a",
714
+ "metadata": {},
715
+ "outputs": [
716
+ {
717
+ "name": "stdout",
718
+ "output_type": "stream",
719
+ "text": [
720
+ "[1 2 4 7]\n",
721
+ "[8 9 4 5]\n",
722
+ "[6 7 9]\n"
723
+ ]
724
+ }
725
+ ],
726
+ "source": [
727
+ "print(newarr2[0])\n",
728
+ "print(newarr2[1])\n",
729
+ "print(newarr2[2])"
730
+ ]
731
+ },
732
+ {
733
+ "cell_type": "code",
734
+ "execution_count": 118,
735
+ "id": "b6dc5efb",
736
+ "metadata": {},
737
+ "outputs": [],
738
+ "source": [
739
+ "##searching arrays"
740
+ ]
741
+ },
742
+ {
743
+ "cell_type": "code",
744
+ "execution_count": 126,
745
+ "id": "db8ca2e0",
746
+ "metadata": {},
747
+ "outputs": [
748
+ {
749
+ "name": "stdout",
750
+ "output_type": "stream",
751
+ "text": [
752
+ "[1 2 4 7 8 9 4 5 6 7 9]\n",
753
+ "(array([], dtype=int64),)\n"
754
+ ]
755
+ }
756
+ ],
757
+ "source": [
758
+ "arr1"
759
+ ]
760
+ },
761
+ {
762
+ "cell_type": "code",
763
+ "execution_count": 129,
764
+ "id": "b0d63d87",
765
+ "metadata": {},
766
+ "outputs": [
767
+ {
768
+ "name": "stdout",
769
+ "output_type": "stream",
770
+ "text": [
771
+ "(array([0, 2, 3]),)\n"
772
+ ]
773
+ }
774
+ ],
775
+ "source": [
776
+ "arr = np.array([1, 2, 1, 1, 5, 6, 7, 8])\n",
777
+ "\n",
778
+ "x = np.where(arr == 1)\n",
779
+ "\n",
780
+ "print(x)"
781
+ ]
782
+ },
783
+ {
784
+ "cell_type": "code",
785
+ "execution_count": 134,
786
+ "id": "32dd0731",
787
+ "metadata": {},
788
+ "outputs": [
789
+ {
790
+ "name": "stdout",
791
+ "output_type": "stream",
792
+ "text": [
793
+ "(array([4, 7]),)\n"
794
+ ]
795
+ }
796
+ ],
797
+ "source": [
798
+ "arr=np.array((1,2,3,4,5,2,3,5,6,7,8))\n",
799
+ "x=np.where(arr==5)\n",
800
+ "print(x)"
801
+ ]
802
+ },
803
+ {
804
+ "cell_type": "code",
805
+ "execution_count": 136,
806
+ "id": "709be631",
807
+ "metadata": {},
808
+ "outputs": [
809
+ {
810
+ "name": "stdout",
811
+ "output_type": "stream",
812
+ "text": [
813
+ "8\n"
814
+ ]
815
+ }
816
+ ],
817
+ "source": [
818
+ "x = np.searchsorted(arr, 6)\n",
819
+ "print(x)"
820
+ ]
821
+ },
822
+ {
823
+ "cell_type": "code",
824
+ "execution_count": 138,
825
+ "id": "eaa35f66",
826
+ "metadata": {},
827
+ "outputs": [
828
+ {
829
+ "name": "stdout",
830
+ "output_type": "stream",
831
+ "text": [
832
+ "[0 2 2 3 5 6 8 8]\n"
833
+ ]
834
+ }
835
+ ],
836
+ "source": [
837
+ "#sorting:\n",
838
+ "arr=np.array((2,5,2,8,0,3,6,8))\n",
839
+ "print(np.sort(arr))"
840
+ ]
841
+ },
842
+ {
843
+ "cell_type": "code",
844
+ "execution_count": 139,
845
+ "id": "66b658a1",
846
+ "metadata": {},
847
+ "outputs": [
848
+ {
849
+ "name": "stdout",
850
+ "output_type": "stream",
851
+ "text": [
852
+ "['azar' 'parmesh' 'ravi' 'sanath']\n"
853
+ ]
854
+ }
855
+ ],
856
+ "source": [
857
+ "arr=np.array((\"sanath\",\"ravi\",\"parmesh\",\"azar\"))\n",
858
+ "print(np.sort(arr))"
859
+ ]
860
+ },
861
+ {
862
+ "cell_type": "code",
863
+ "execution_count": 140,
864
+ "id": "2aef5294",
865
+ "metadata": {},
866
+ "outputs": [
867
+ {
868
+ "name": "stdout",
869
+ "output_type": "stream",
870
+ "text": [
871
+ "[[10 20 29]\n",
872
+ " [ 0 20 83]]\n"
873
+ ]
874
+ }
875
+ ],
876
+ "source": [
877
+ "#we can also sort the 2-d array:\n",
878
+ "arr=np.array([[20,10,29],[0,20,83]])\n",
879
+ "print(np.sort(arr))"
880
+ ]
881
+ },
882
+ {
883
+ "cell_type": "code",
884
+ "execution_count": 143,
885
+ "id": "60eeeed6",
886
+ "metadata": {},
887
+ "outputs": [
888
+ {
889
+ "name": "stdout",
890
+ "output_type": "stream",
891
+ "text": [
892
+ "[1 3 4]\n"
893
+ ]
894
+ }
895
+ ],
896
+ "source": [
897
+ "arr=np.array([1,2,3,4])\n",
898
+ "x=[True,False,True,True]\n",
899
+ "print(arr[x])"
900
+ ]
901
+ },
902
+ {
903
+ "cell_type": "code",
904
+ "execution_count": 144,
905
+ "id": "35d8dba5",
906
+ "metadata": {},
907
+ "outputs": [],
908
+ "source": [
909
+ "#nupy rondom class working:\n",
910
+ "#we can predict theese numbers:\n",
911
+ "#machine will generate this:\n",
912
+ "from numpy import random"
913
+ ]
914
+ },
915
+ {
916
+ "cell_type": "code",
917
+ "execution_count": 153,
918
+ "id": "859d5dbb",
919
+ "metadata": {},
920
+ "outputs": [
921
+ {
922
+ "data": {
923
+ "text/plain": [
924
+ "77"
925
+ ]
926
+ },
927
+ "execution_count": 153,
928
+ "metadata": {},
929
+ "output_type": "execute_result"
930
+ }
931
+ ],
932
+ "source": [
933
+ "x=random.randint(100)\n",
934
+ "x"
935
+ ]
936
+ },
937
+ {
938
+ "cell_type": "code",
939
+ "execution_count": 148,
940
+ "id": "7cfa7040",
941
+ "metadata": {},
942
+ "outputs": [
943
+ {
944
+ "name": "stdout",
945
+ "output_type": "stream",
946
+ "text": [
947
+ "88\n"
948
+ ]
949
+ }
950
+ ],
951
+ "source": [
952
+ "print(x)"
953
+ ]
954
+ },
955
+ {
956
+ "cell_type": "code",
957
+ "execution_count": 154,
958
+ "id": "a1748723",
959
+ "metadata": {},
960
+ "outputs": [
961
+ {
962
+ "data": {
963
+ "text/plain": [
964
+ "array([0.88562907, 0.22932804, 0.90857795, 0.2838597 , 0.76885629,\n",
965
+ " 0.87201837, 0.5121362 , 0.89441225, 0.67149354, 0.03825343])"
966
+ ]
967
+ },
968
+ "execution_count": 154,
969
+ "metadata": {},
970
+ "output_type": "execute_result"
971
+ }
972
+ ],
973
+ "source": [
974
+ "x=random.rand(10)\n",
975
+ "x"
976
+ ]
977
+ },
978
+ {
979
+ "cell_type": "code",
980
+ "execution_count": 170,
981
+ "id": "62ef85d8",
982
+ "metadata": {},
983
+ "outputs": [
984
+ {
985
+ "data": {
986
+ "text/plain": [
987
+ "array([ 6, 16, 29, 50, 6, 0, 99, 11, 88, 33])"
988
+ ]
989
+ },
990
+ "execution_count": 170,
991
+ "metadata": {},
992
+ "output_type": "execute_result"
993
+ }
994
+ ],
995
+ "source": [
996
+ "#generating 10 randoom numbers in given range:\n",
997
+ "#and this method is also called as the creating the 2-d array:\n",
998
+ "x=random.randint(100,size=10)\n",
999
+ "x"
1000
+ ]
1001
+ },
1002
+ {
1003
+ "cell_type": "code",
1004
+ "execution_count": 158,
1005
+ "id": "34d5e7ce",
1006
+ "metadata": {},
1007
+ "outputs": [
1008
+ {
1009
+ "data": {
1010
+ "text/plain": [
1011
+ "array([[40, 55, 86, 42, 85, 35, 18, 13, 11, 35],\n",
1012
+ " [66, 31, 61, 65, 23, 49, 65, 30, 90, 3],\n",
1013
+ " [90, 53, 95, 32, 45, 85, 31, 10, 52, 69]])"
1014
+ ]
1015
+ },
1016
+ "execution_count": 158,
1017
+ "metadata": {},
1018
+ "output_type": "execute_result"
1019
+ }
1020
+ ],
1021
+ "source": [
1022
+ "#creating the 3-d array from the rndom :\n",
1023
+ "x=random.randint(100,size=(3,10))\n",
1024
+ "x"
1025
+ ]
1026
+ },
1027
+ {
1028
+ "cell_type": "code",
1029
+ "execution_count": 159,
1030
+ "id": "8ae2661e",
1031
+ "metadata": {},
1032
+ "outputs": [
1033
+ {
1034
+ "data": {
1035
+ "text/plain": [
1036
+ "(3, 10)"
1037
+ ]
1038
+ },
1039
+ "execution_count": 159,
1040
+ "metadata": {},
1041
+ "output_type": "execute_result"
1042
+ }
1043
+ ],
1044
+ "source": [
1045
+ "x.shape"
1046
+ ]
1047
+ },
1048
+ {
1049
+ "cell_type": "code",
1050
+ "execution_count": 171,
1051
+ "id": "37d6319d",
1052
+ "metadata": {},
1053
+ "outputs": [
1054
+ {
1055
+ "data": {
1056
+ "text/plain": [
1057
+ "29"
1058
+ ]
1059
+ },
1060
+ "execution_count": 171,
1061
+ "metadata": {},
1062
+ "output_type": "execute_result"
1063
+ }
1064
+ ],
1065
+ "source": [
1066
+ "#upto now we have created random array:\n",
1067
+ "# but now we can get the random element from the generatd arry:\n",
1068
+ "#only for the 1-d array\n",
1069
+ "y=random.choice(x)\n",
1070
+ "y"
1071
+ ]
1072
+ },
1073
+ {
1074
+ "cell_type": "code",
1075
+ "execution_count": 174,
1076
+ "id": "7decc1e0",
1077
+ "metadata": {},
1078
+ "outputs": [
1079
+ {
1080
+ "data": {
1081
+ "text/plain": [
1082
+ "array([[5, 7, 9, 7, 3],\n",
1083
+ " [9, 9, 5, 7, 7],\n",
1084
+ " [5, 5, 5, 9, 5]])"
1085
+ ]
1086
+ },
1087
+ "execution_count": 174,
1088
+ "metadata": {},
1089
+ "output_type": "execute_result"
1090
+ }
1091
+ ],
1092
+ "source": [
1093
+ "#now we can get the choice of number from the 2-d array by using the size:\n",
1094
+ "arr2 = random.choice([3, 5, 7, 9], size=(3, 5))\n",
1095
+ "arr2"
1096
+ ]
1097
+ },
1098
+ {
1099
+ "cell_type": "code",
1100
+ "execution_count": 179,
1101
+ "id": "415e52b4",
1102
+ "metadata": {},
1103
+ "outputs": [
1104
+ {
1105
+ "name": "stdout",
1106
+ "output_type": "stream",
1107
+ "text": [
1108
+ "[1 3 2 4]\n"
1109
+ ]
1110
+ }
1111
+ ],
1112
+ "source": [
1113
+ "#rndom permutatiions\n",
1114
+ "arr=np.array([1,2,3,4])\n",
1115
+ "random.shuffle(arr)\n",
1116
+ "print(arr)"
1117
+ ]
1118
+ },
1119
+ {
1120
+ "cell_type": "code",
1121
+ "execution_count": 181,
1122
+ "id": "40dbec86",
1123
+ "metadata": {},
1124
+ "outputs": [
1125
+ {
1126
+ "name": "stdout",
1127
+ "output_type": "stream",
1128
+ "text": [
1129
+ "[4 1 2 3]\n"
1130
+ ]
1131
+ }
1132
+ ],
1133
+ "source": [
1134
+ "print(random.permutation(arr))"
1135
+ ]
1136
+ },
1137
+ {
1138
+ "cell_type": "code",
1139
+ "execution_count": 189,
1140
+ "id": "bb115669",
1141
+ "metadata": {},
1142
+ "outputs": [
1143
+ {
1144
+ "name": "stdout",
1145
+ "output_type": "stream",
1146
+ "text": [
1147
+ "[5, 5, 5, 96, 86]\n"
1148
+ ]
1149
+ }
1150
+ ],
1151
+ "source": [
1152
+ "#zip method:\n",
1153
+ "x1=[1,2,3,93,82]\n",
1154
+ "x2=[4,3,2,3,4,]\n",
1155
+ "z=[]\n",
1156
+ "for i,j in zip(x1,x2):\n",
1157
+ " z.append(i+j)\n",
1158
+ "print(z)"
1159
+ ]
1160
+ },
1161
+ {
1162
+ "cell_type": "code",
1163
+ "execution_count": 199,
1164
+ "id": "2dfc2e91",
1165
+ "metadata": {},
1166
+ "outputs": [
1167
+ {
1168
+ "name": "stdout",
1169
+ "output_type": "stream",
1170
+ "text": [
1171
+ "[ 5 5 5 96 86]\n",
1172
+ "[-3 -1 1 90 78]\n",
1173
+ "[ 4 6 6 279 328]\n",
1174
+ "[ 0.25 0.66666667 1.5 31. 20.5 ]\n",
1175
+ "[ 1 8 9 804357 45212176]\n",
1176
+ "[1 2 1 0 2]\n",
1177
+ "[1 2 1 0 2]\n",
1178
+ "(array([ 0, 0, 1, 31, 20]), array([1, 2, 1, 0, 2]))\n"
1179
+ ]
1180
+ }
1181
+ ],
1182
+ "source": [
1183
+ "#arithmatic operations:\n",
1184
+ "print(np.add(x1,x2))\n",
1185
+ "print(np.subtract(x1,x2))\n",
1186
+ "print(np.multiply(x1,x2))\n",
1187
+ "print(np.divide(x1,x2))\n",
1188
+ "print(np.power(x1,x2))\n",
1189
+ "print(np.mod(x1,x2))\n",
1190
+ "print(np.remainder(x1,x2))\n",
1191
+ "print(np.divmod(x1,x2))\n",
1192
+ "print(np.(x1,x2))"
1193
+ ]
1194
+ },
1195
+ {
1196
+ "cell_type": "code",
1197
+ "execution_count": null,
1198
+ "id": "6b93fdd9",
1199
+ "metadata": {},
1200
+ "outputs": [],
1201
+ "source": []
1202
+ }
1203
+ ],
1204
+ "metadata": {
1205
+ "kernelspec": {
1206
+ "display_name": "Python 3 (ipykernel)",
1207
+ "language": "python",
1208
+ "name": "python3"
1209
+ },
1210
+ "language_info": {
1211
+ "codemirror_mode": {
1212
+ "name": "ipython",
1213
+ "version": 3
1214
+ },
1215
+ "file_extension": ".py",
1216
+ "mimetype": "text/x-python",
1217
+ "name": "python",
1218
+ "nbconvert_exporter": "python",
1219
+ "pygments_lexer": "ipython3",
1220
+ "version": "3.9.12"
1221
+ }
1222
+ },
1223
+ "nbformat": 4,
1224
+ "nbformat_minor": 5
1225
+ }