-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathnumpy_array_exercises_python.py
More file actions
1670 lines (1430 loc) · 34.8 KB
/
Copy pathnumpy_array_exercises_python.py
File metadata and controls
1670 lines (1430 loc) · 34.8 KB
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
1. Write a Python program to print the NumPy version in your system.
"""
import numpy as np
print(np.__version__)
"""
2. Write a Python program to convert a list of numeric value into a
one-dimensional NumPy array.
Expected Output:
Original List: [12.23, 13.32, 100, 36.32]
One-dimensional numpy array: [ 12.23 13.32 100. 36.32]
"""
import numpy as np
my_arr = np.array([12.23, 13.32, 100, 36.32])
print(my_arr)
"""
3. Create a 3x3 matrix with values ranging from 2 to 10.
Expected Output:
[[ 2 3 4]
[ 5 6 7]
[ 8 9 10]]
"""
import numpy as np
my_arr = np.arange(2, 11).reshape(3,3)
print(my_arr)
"""
4. Write a Python program to create a null vector of size 10 and update sixth
value to 11.
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
Update sixth value to 11
[ 0. 0. 0. 0. 0. 0. 11. 0. 0. 0.]
"""
import numpy as np
my_arr = np.zeros(10)
print(my_arr)
my_arr[6] = 11
print(my_arr)
"""
5. Write a Python program to create a array with values ranging from 12 to 38.
Expected Output:
[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]
"""
import numpy as np
my_arr = np.arange(12,39)
print(my_arr)
"""
6. Write a Python program to reverse an array (first element becomes last).
Original array:
[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]
Reverse array:
[37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12]
"""
import numpy as np
my_arr = np.arange(12, 38)
print(my_arr[::-1])
"""
7. Write a Python program to an array converted to a float type.
Sample output:
Original array
[1, 2, 3, 4]
Array converted to a float type:
[ 1. 2. 3. 4.]
"""
import numpy as np
my_arr = np.array([1, 2, 3, 4], dtype=float)
print(my_arr)
my_arr = np.asfarray([1, 2, 3, 4])
print(my_arr)
"""
8. Write a Python program to create a 2d array with 1 on the border and 0 inside.
Expected Output:
Original array:
[[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]]
1 on the border and 0 inside in the array
[[ 1. 1. 1. 1. 1.]
[ 1. 0. 0. 0. 1.]
[ 1. 0. 0. 0. 1.]
[ 1. 0. 0. 0. 1.]
[ 1. 1. 1. 1. 1.]]
"""
import numpy as np
my_arr = np.ones((5,5))
print(my_arr)
my_arr[1:4, 1:4] = 0
print(my_arr)
"""
9. Write a Python program to add a border (filled with 0's) around an existing
array.
Expected Output:
Original array:
[[ 1. 1. 1.]
[ 1. 1. 1.]
[ 1. 1. 1.]]
1 on the border and 0 inside in the array
[[ 0. 0. 0. 0. 0.]
[ 0. 1. 1. 1. 0.]
[ 0. 1. 1. 1. 0.]
[ 0. 1. 1. 1. 0.]
[ 0. 0. 0. 0. 0.]]
"""
import numpy as np
my_arr = np.ones((3,3))
my_arr = np.pad(my_arr, 1, 'constant', constant_values=0)
print(my_arr)
"""
10. Write a Python program to create a 8x8 matrix and fill it with a
checkerboard pattern.
Checkerboard pattern:
[[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]]
"""
import numpy as np
my_arr = np.zeros((8,8))
my_arr[::2, 1::2] = 1
my_arr[1::2, ::2] = 1
print(my_arr)
"""
11. Write a Python program to convert a list and tuple into arrays.
List to array:
[1 2 3 4 5 6 7 8]
Tuple to array:
[[8 4 6]
[1 2 3]]
"""
import numpy as np
my_arr = np.array([1,2,3,4,5,6,7,8])
print(my_arr)
my_arr2 = np.array([(8,4,6),(1,2,3)])
print(my_arr2)
"""
12. Write a Python program to append values to the end of an array.
Expected Output:
Original array:
[10, 20, 30]
After append values to the end of the array:
[10 20 30 40 50 60 70 80 90]
"""
import numpy as np
my_arr = np.array([10, 20, 30])
new_arr = np.append(my_arr, [40, 50 , 60, 70, 80, 90])
print(new_arr)
"""
13. Write a Python program to create an empty and a full array.
Expected Output:
[ 6.93270651e-310 1.59262180e-316 6.93270559e-310 6.93270665e-310]
[ 6.93270667e-310 6.93270671e-310 6.93270668e-310 6.93270483e-310]
[ 6.93270668e-310 6.93270671e-310 6.93270370e-310 6.93270488e-310]]
[[6 6 6]
[6 6 6]
[6 6 6]]
"""
import numpy as np
my_arr = np.empty((3,3))
print(my_arr)
my_arr = np.array([6]*9).reshape((3,3))
print(my_arr)
my_arr = np.full((3,3), 6)
print(my_arr)
"""
14. Write a Python program to convert the values of Centigrade degrees into
Fahrenheit degrees. Centigrade values are stored into a NumPy array.
Sample Array [0, 12, 45.21 ,34, 99.91]
Expected Output:
Values in Fahrenheit degrees:
[ 0. 12. 45.21 34. 99.91]
Values in Centigrade degrees:
[-17.77777778 -11.11111111 7.33888889 1.11111111 37.72777778]
"""
import numpy as np
celsius = np.array([0,12,45.21,34,99.91])
print(celsius)
fahrenheit = celsius*9/5 + 32
print(fahrenheit)
"""
16. Write a Python program to find the number of elements of an array, length of
one array element in bytes and total bytes consumed by the elements.
Expected Output:
Size of the array: 3
Length of one array element in bytes: 8
Total bytes consumed by the elements of the array: 24
"""
import numpy as np
my_arr = np.full((3,3), 5)
print(my_arr)
print('Size of the array (number of elements):', my_arr.size)
print('Length of one array element in bytes:', my_arr.itemsize )
print('Total bytes consumed by the elements of the array:', my_arr.nbytes )
"""
17. Write a Python program to test whether each element of a 1-D array is also
present in a second array.
Expected Output:
Array1: [ 0 10 20 40 60]
Array2: [0, 40]
Compare each element of array1 and array2
[ True False False True False]
"""
import numpy as np
my_arr = np.array([0, 10, 20, 40, 60])
my_arr2 = np.array([0, 40])
compare = np.in1d(my_arr, my_arr2)
print(compare)
"""
18. Write a Python program to find common values between two arrays.
Expected Output:
Array1: [ 0 10 20 40 60]
Array2: [10, 30, 40]
Common values between two arrays:
[10 40]
"""
import numpy as np
my_arr = np.array([0, 10, 20, 40, 60])
my_arr2 = np.array([10, 30, 40])
compare = np.intersect1d(my_arr, my_arr2)
print(compare)
"""
19. Write a Python program to get the unique elements of an array.
Expected Output:
Original array:
[10 10 20 20 30 30]
Unique elements of the above array:
[10 20 30]
Original array:
[[1 1]
[2 3]]
Unique elements of the above array:
[1 2 3]
"""
import numpy as np
my_arr = np.array([10, 10, 20, 20, 30, 30])
print(np.unique(my_arr))
my_arr = np.array([[1, 1],[2, 3]])
print(np.unique(my_arr))
"""
20. Write a Python program to find the set difference of two arrays. The set
difference will return the sorted, unique values in array1 that are not in array2.
Expected Output:
Array1: [ 0 10 20 40 60 80]
Array2: [10, 30, 40, 50, 70, 90]
Set difference between two arrays:
[ 0 20 60 80]
"""
import numpy as np
my_arr = np.array([0, 10, 20, 40, 60, 80])
my_arr2 = np.array([10, 30, 40, 50, 70, 90])
print(np.setdiff1d(my_arr, my_arr2))
"""
21. Write a Python program to find the set exclusive-or of two arrays. Set
exclusive-or will return the sorted, unique values that are in only one
(not both) of the input arrays.
Array1: [ 0 10 20 40 60 80]
Array2: [10, 30, 40, 50, 70]
Unique values that are in only one (not both) of the input arrays:
[ 0 20 30 50 60 70 80]
"""
import numpy as np
my_arr = np.array([0, 10, 20, 40, 60, 80])
my_arr2 = np.array([10, 30, 40, 50, 70, 90])
print(np.setxor1d(my_arr, my_arr2))
"""
22. Write a Python program to find the union of two arrays. Union will return the unique, sorted array of values that are in either of the two input arrays. Go to the editor
Array1: [ 0 10 20 40 60 80]
Array2: [10, 30, 40, 50, 70]
Unique sorted array of values that are in either of the two input arrays:
[ 0 10 20 30 40 50 60 70 80]
"""
import numpy as np
my_arr = np.array([0, 10, 20, 40, 60, 80])
my_arr2 = np.array([10, 30, 40, 50, 70, 90])
print(np.union1d(my_arr, my_arr2))
"""
23. Write a Python program to test if all elements in an array evaluate to True.
Note: 0 evaluates to False in python.
"""
import numpy as np
my_arr = np.array([0, 10, 20, 40, 60, 80])
my_arr2 = np.array([10, 30, 40, 50, 70, 90])
print(np.all(my_arr))
print(np.all(my_arr2))
"""
24. Write a Python program to test whether any array element along a given axis
evaluates to True.
Note: 0 evaluates to False in python.
"""
import numpy as np
my_arr = np.array([0, 10, 20, 40, 60, 80])
my_arr2 = np.array([10, 30, 40, 50, 70, 90])
print(np.any(my_arr))
print(np.any(my_arr2))
"""
25. Write a Python program to construct an array by repeating.
Sample array: [1, 2, 3, 4]
Expected Output:
Original array
[1, 2, 3, 4]
Repeating 2 times
[1 2 3 4 1 2 3 4]
Repeating 3 times
[1 2 3 4 1 2 3 4 1 2 3 4]
"""
import numpy as np
my_arr = np.array([1, 2, 3, 4])
# solution 1
print(np.concatenate((my_arr,my_arr)))
print(np.concatenate((my_arr,my_arr,my_arr)))
# solution 2
print(np.tile(my_arr,2))
print(np.tile(my_arr,3))
"""
26. Write a Python program to repeat elements of an array.
Expected Output:
[3 3 3 3]
[1 1 2 2 3 3 4 4]
"""
import numpy as np
print(np.repeat(3, 4))
print(np.repeat([1,2,3,4], 2))
"""
27. Write a Python program to find the indices of the maximum and minimum values
along the given axis of an array.
Original array: [1 2 3 4 5 6]
Maximum Values: 5
Minimum Values: 0
"""
import numpy as np
my_arr = np.array([1,2,3,4,5])
print('Maximum Values:', np.argmax(my_arr))
print('Maximum Values:', np.argmin(my_arr))
"""
28. Write a Python program compare two arrays using numpy.
Array a: [1 2]
Array b: [4 5]
a > b
[False False]
a >= b
[False False]
a < b
[ True True]
a <= b
[ True True]
"""
import numpy as np
arr_a = np.array([1,2])
arr_b = np.array([4,2])
print(np.greater(arr_a,arr_b))
print(np.greater_equal(arr_a,arr_b))
print(np.less(arr_a,arr_b))
print(np.less_equal(arr_a,arr_b))
"""
29. Write a Python program to sort along the first, last axis of an array.
Sample array: [[4,6],[2,1]]
Expected Output:
Original array:
[[4 6]
[2 1]]
Sort along the first axis:
[[2 1]
[4 6]]
Sort along the last axis:
[[1 2]
[4 6]]
"""
import numpy as np
my_arr = np.array([[4,6],[2,1]])
print('Original array:\n', my_arr)
my_arr.sort(axis=0)
print('Sort along the first axis:\n',my_arr)
my_arr.sort()
print('Sort along the last axis:\n',my_arr)
"""
30. Write a Python program to sort pairs of first name and last name return their
indices. (first by last name, then by first name).
first_names = (Betsey, Shelley, Lanell, Genesis, Margery)
last_names = (Battle, Brien, Plotner, Stahl, Woolum)
Expected Output:
[0 1 2 3 4]
[0 3 2 4 1]
"""
import numpy as np
first_names = ('Betsey', 'Shelley', 'Lanell', 'Genesis', 'Margery')
surnames = ('Battle', 'Brien', 'Plotner', 'Stahl', 'Woolum')
ind = np.lexsort((first_names, surnames))
print('by last name:\n',ind)
ind = np.lexsort((surnames, first_names))
print('by first name:\n',ind)
"""
31. Write a Python program to get the values and indices of the elements that are
bigger than 10 in a given array.
Original array:
[[ 0 10 20]
[20 30 40]]
Values bigger than 10 = [20 20 30 40]
Their indices are (array([0, 1, 1, 1]), array([2, 0, 1, 2]))
"""
import numpy as np
my_arr = np.array([0, 10, 20, 20, 30, 40]).reshape((2,3))
print('original array:\n', my_arr)
# solution 1
print(my_arr[my_arr>10])
print(np.nonzero(my_arr>10))
# solution 2
print(my_arr[np.where(my_arr>10)])
print(np.nonzero(my_arr>10))
"""
32. Write a Python program to save a NumPy array to a text file.
"""
import numpy as np
my_arr = np.array([[2,3,4],[5,6,7]])
np.savetxt('myarray.txt', my_arr, fmt='%.2f', delimiter=',')
"""
33. Write a Python program to find the memory size of a NumPy array.
Expected Output:
128 bytes
"""
import numpy as np
my_arr = np.array([[2,3,4],[5,6,7]])
# solution 1
print(my_arr.nbytes)
# solution 2
print(my_arr.size * my_arr.itemsize)
# solution 3
print(np.prod(my_arr.shape) * my_arr.itemsize)
"""
34. Write a Python program to create an array of ones and an array of zeros.
Expected Output:
Create an array of zeros
Default type is float
[[ 0. 0.]]
Type changes to int
[[0 0]]
Create an array of ones
Default type is float
[[ 1. 1.]]
Type changes to int
[[1 1]]
"""
import numpy as np
print(np.zeros((3,3), dtype=int))
print(np.ones((3,3), dtype=int))
print(np.zeros((3,3)))
print(np.ones((3,3)))
"""
35. Write a Python program to change the dimension of an array.
Expected Output:
6 rows and 0 columns
(6,)
(3, 3) -> 3 rows and 3 columns
[[1 2 3]
[4 5 6]
[7 8 9]]
Change array shape to (3, 3) -> 3 rows and 3 columns
[[1 2 3]
[4 5 6]
[7 8 9]]
"""
import numpy as np
my_arr = np.ones((9,))
print(my_arr)
print(my_arr.shape)
my_arr = my_arr.reshape((3,3))
print(my_arr)
print(my_arr.shape)
"""
36. Write a Python program to create a contiguous flattened array.
Original array:
[[10 20 30]
[20 40 50]]
New flattened array:
[10 20 30 20 40 50]
"""
import numpy as np
my_arr = np.array([[10,20,30],[20,40,50]])
print(my_arr, '\n', my_arr.shape)
print(np.ravel(my_arr), '\n', np.ravel(my_arr).shape)
"""
37. Write a Python program to create a 2-dimensional array of size 2 x 3
(composed of 4-byte integer elements), also print the shape, type and data type
of the array.
Expected Output:
(2, 3)
int32
"""
import numpy as np
my_arr = np.ones((2,3), dtype=np.int32)
print(my_arr)
print(my_arr.shape)
print(my_arr.dtype)
print(my_arr.itemsize)
print(type(my_arr))
"""
38. Write a Python program to create a new shape to an array without changing its
data.
Reshape 3x2:
[[1 2]
[3 4]
[5 6]]
Reshape 2x3:
[[1 2 3]
[4 5 6]]
"""
import numpy as np
my_arr = np.array(([1,2],[3,4],[5,6]))
print(my_arr)
my_arr.shape = (2,3)
print(my_arr)
"""
39. Write a Python program to change the data type of an array.
Expected Output:
[[ 2 4 6]
[ 6 8 10]]
Data type of the array x is: int32
New Type: float64
[[ 2. 4. 6.]
[ 6. 8. 10.]]
"""
import numpy as np
my_arr = np.array([[2,4,6],[6,8,10]])
print(my_arr, '\n', my_arr.dtype)
new_arr = my_arr.astype('float64')
print(new_arr, '\n', new_arr.dtype)
"""
40. Write a Python program to create a new array of 3*5, filled with 2. Go to the editor
Expected Output:
[[2 2 2 2 2]
[2 2 2 2 2]
[2 2 2 2 2]]
"""
import numpy as np
my_arr = np.full((3,5),2)
print(my_arr)
"""
41. Write a Python program to create an array of 10's with the same shape and
type of an given array.
Sample array: x = np.arange(4, dtype=np.int64)
Expected Output:
[10 10 10 10]
"""
import numpy as np
x = np.arange(4, dtype=np.int64)
print(x)
y = np.full_like(x, 10)
print(y)
"""
42. Write a Python program to create a 3-D array with ones on the diagonal and
zeros elsewhere.
Expected Output:
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
"""
import numpy as np
my_arr = np.eye(3)
print(my_arr)
my_arr = np.identity(3)
print(my_arr)
"""
43. Write a Python program to create a 2-D array whose diagonal equals
[4, 5, 6, 8] and 0's elsewhere.
Expected Output:
[[4 0 0 0]
[0 5 0 0]
[0 0 6 0]
[0 0 0 8]]
"""
import numpy as np
my_arr = np.diag([4,5,6,8])
print(my_arr)
"""
44. Write a Python program to create a 1-D array going from 0 to 50 and an array
from 10 to 50.
Expected Output:
Array from 0 to 50:
[ 0 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]
Array from 10 to 50:
[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]
"""
import numpy as np
my_arr = np.arange(50, dtype=int)
my_arr2 = np.arange(10,51, dtype=int)
print(my_arr)
print(my_arr2)
"""
45. Write a Python program to Create a 1-D array of 30 evenly spaced elements
between 2.5. and 6.5, inclusive. Go to the editor
Expected Output:
[ 2.5 2.63793103 2.77586207 2.9137931 3.05172414 3.18965517
3.32758621 3.46551724 3.60344828 3.74137931 3.87931034 4.01724138
4.15517241 4.29310345 4.43103448 4.56896552 4.70689655 4.84482759
4.98275862 5.12068966 5.25862069 5.39655172 5.53448276 5.67241379
5.81034483 5.94827586 6.0862069 6.22413793 6.36206897 6.5 ]
"""
import numpy as np
my_arr = np.linspace(2.5, 6.5, num=30, endpoint=True)
print(my_arr)
"""
46. Write a Python program to to create a 1-D array of 20 element spaced evenly
on a log scale between 2. and 5., exclusive.
Expected Output:
[ 100. 141.25375446 199.5262315 281.83829313
398.10717055 562.34132519 794.32823472 1122.0184543
1584.89319246 2238.72113857 3162.27766017 4466.83592151
6309.5734448 8912.50938134 12589.25411794 17782.79410039
25118.8643151 35481.33892336 50118.72336273 70794.57843841]
"""
import numpy as np
my_arr = np.logspace(2.,5.,num=20, endpoint=False)
print(my_arr)
"""
47. Write a Python program to create an array which looks like below array.
Expected Output:
[[ 0. 0. 0.]
[ 1. 0. 0.]
[ 1. 1. 0.]
[ 1. 1. 1.]]
"""
import numpy as np
my_arr = np.tri(4,3,-1)
print(my_arr)
"""
48. Write a Python program to create an array which looks like below array.
Expected Output:
[[ 2 3 4]
[ 5 6 7]
[ 0 9 10]
[ 0 0 13]]
"""
import numpy as np
my_arr = np.arange(2,14).reshape(4,3)
print(np.triu(my_arr,-1))
"""
49. Write a Python program to collapse a 3-D array into one dimension array.
Expected Output:
3-D array:
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
One dimension array:
[ 1. 0. 0. 0. 1. 0. 0. 0. 1.]
"""
import numpy as np
my_arr = np.eye(3)
print(my_arr.ravel())
"""
50. Write a Python program to find the 4th element of a specified array.
Expected Output:
[[ 2 4 6]
[ 6 8 10]]
Forth e1ement of the array:
6
"""
import numpy as np
my_arr = np.array([[2,4,6],[6,8,10]])
print(my_arr.flat[3])
"""
51. Write a Python program to interchange two axes of an array.
Sample array: [[1 2 3]]
Expected Output:
[[1]
[2]
[3]]
"""
import numpy as np
my_arr = np.array([[1,2,3]])
print(np.swapaxes(my_arr,0,1))
"""
52. Write a Python program to move axes of an array to new positions. Other axes
remain in their original order.
Expected Output:
(3, 4, 2)
(4, 2, 3)
"""
import numpy as np
my_arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
print('original:\n',my_arr)
print('transposed:')
# solution 1
print(np.moveaxis(my_arr, 0, 1))
# solution 2
print(np.transpose(my_arr))
# solution 3
print(np.swapaxes(my_arr, 0, 1))
# solution 4
print(np.moveaxis(my_arr, [0, 1], [-1, -2]))
"""
53. Write a Python program to move the specified axis backwards, until it lies
in a given position.
Move the following 3rd array axes to first position.
(2,3,4,5)
Sample Expected Output:
(2, 5, 3, 4)
"""
import numpy as np
# This function continues to be supported for backward compatibility,
# but you should prefer moveaxis. The moveaxis function was added in NumPy 1.11.
my_arr = np.zeros((2,3,4,5))
print(my_arr.shape)
print(np.rollaxis(my_arr, 3, 1).shape)
print(np.moveaxis(my_arr, 3, 1).shape)
"""
54. Write a Python program to convert specified inputs to arrays with at least one
dimension.
Expected Output:
[ 12.]
[[ 0. 1. 2.]
[ 3. 4. 5.]]
[array([1]), array([3, 4])]
"""
import numpy as np
x= 12.
print(np.atleast_1d(x))
x = [[0., 1., 2.], [3., 4., 5.]]
print(np.atleast_1d(x))
x = 1
y = [3, 4]
print(np.atleast_1d(x, y))
"""
55. Write a Python program to view inputs as arrays with at least two dimensions,
three dimensions.
Expected Output:
View inputs as arrays with at least two dimensions:
[10]
[[ 0. 1.]
[ 2. 3.]]
View inputs as arrays with at least three dimensions:
[[[15]]]
[[[ 0.]
[ 1.]
[ 2.]]]
"""
import numpy as np
x= 10
print(np.atleast_2d(x))
x = [[0., 1.], [2., 3.]]
print(np.atleast_2d(x))
x = 15
print(np.atleast_3d(x))
x = [0., 1., 2.]
print(np.atleast_3d(x))
"""
56. Write a Python program to insert a new axis within a 2-D array.
2-D array of shape (3, 4).
Expected Output:
New shape will be will be (3, 1, 4).
"""
import numpy as np
x = np.arange(12).reshape((3,4))
print(x)
print(np.expand_dims(x, 1).shape)
"""
57. Write a Python program to remove single-dimensional entries from a specified
shape.
Specified shape: (3, 1, 4)
Expected Output: (3, 4)
"""
import numpy as np
x = np.zeros(12).reshape((3,1,4))
print(x)
print(np.squeeze(x))
print(np.squeeze(x).shape)
"""
58. Write a Python program to concatenate two 2-dimensional arrays.
Expected Output:
Sample arrays: [[0, 1, 3], [5, 7, 9]], [[0, 2, 4], [6, 8, 10]]
Expected Output:
[[ 0 1 3 0 2 4]
[ 5 7 9 6 8 10]]
"""
import numpy as np
x = np.array([[0, 1, 3], [5, 7, 9]])
y = np.array([[0, 2, 4], [6, 8, 10]])
print(np.concatenate((x,y), 1))
"""
59. Write a Python program to convert 1-D arrays as columns into a 2-D array.
Sample array: (10,20,30), (40,50,60)
Expected Output:
[[10 40]
[20 50]
[30 60]]
"""
import numpy as np
x = np.array((10,20,30))
y = np.array((40,50,60))
print(np.column_stack((x,y)))
"""
60. Write a Python program to convert (in sequence depth wise (along third axis)) two 1-D arrays into a 2-D array. Go to the editor
Sample array: (10,20,30), (40,50,60)
Expected Output:
[[[10 40]]
[[20 50]]
[[30 60]]]
"""
import numpy as np
x = np.array((10,20,30))
y = np.array((40,50,60))
print(np.dstack((x,y)))
"""
61. Write a Python program to split an array of 14 elements into 3 arrays, each of
which has 2, 4, and 8 elements in the original order.
Expected Output:
Original array: [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
After splitting:
[array([1, 2]), array([3, 4, 5, 6]), array([ 7, 8, 9, 10, 11, 12, 13, 14])]
"""
import numpy as np
x = np.arange(1,15)
print(x)
print(np.array_split(x,[2,6]))
print(np.split(x,[2,6]))
"""
62. Write a Python program to split an array of shape 4x4 it into two arrays
along the second axis.
Sample array :
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Expected Output:
[array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]]), array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]]), array([], shape=(4, 0), dtype=int64)]
"""
import numpy as np
x = np.arange(16).reshape(4,4)
print(x)
print(np.split(x, 2, axis=-1))
print(np.hsplit(x, 2))
"""
63. Write a Python program to get the number of nonzero elements in an array.
Expected Output:
Original array:
[[ 0 10 20]
[20 30 40]]
Number of non zero elements in the above array:
5
"""
import numpy as np
x = np.array([[0,10,20],[20,30,40]])
print(np.count_nonzero(x))