-
Notifications
You must be signed in to change notification settings - Fork 0
/
numpy.txt
1274 lines (909 loc) · 35.2 KB
/
numpy.txt
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. Arrays
1.1 rank
The number of dimensions is the rank of the array.
1.2 shape
the shape of an array is a tuple of integers giving the size of the array along each dimension.
1.3 acces array
We can initialize numpy arrays from nested Python lists, and access elements using square brackets
------------------------
---------------------------------------------
2. create array
a = np.zeros((2,2)) # a = np.zeros([2,2]) both ok
b = np.ones((1,2)) # b = np.ones([1,2]) both ok
c = np.full((2,2), 7) # fill 2x2 array with element 7
d = np.eye(2) #
e = np.random.random((2,2))
------------------------
---------------------------------------------
3. Array indexing -- Slicing
3.1 # Create the following rank 2 array with shape (3, 4)
# [[ 1 2 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
# Use slicing to pull out the subarray consisting of the first 2 rows
# and columns 1 and 2; b is the following array of shape (2, 2):
# [[2 3]
# [6 7]]
b = a[:2, 1:3]
# A slice of an array is a view into the same data, so modifying it
# will modify the original array.
print(a[0, 1]) # Prints "2"
b[0, 0] = 77 # b[0, 0] is the same piece of data as a[0, 1]
print(a[0, 1]) # Prints "77"
------------------------
---------------------------------------------
4. Array indexing -- Slice and Integer
4.1 mix integer indexing with slice indexing
# Create the following rank 2 array with shape (3, 4)
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
# Mixing integer indexing with slices yields an array of lower rank,
# while using only slices yields an array of the same rank as the
# original array:
row_r1 = a[1, :] # Rank 1 view of the second row of a
row_r2 = a[1:2, :] # Rank 2 view of the second row of a
print(row_r1, row_r1.shape) # Prints "[5 6 7 8] (4,)"
print(row_r2, row_r2.shape) # Prints "[[5 6 7 8]] (1, 4)"
# We can make the same distinction when accessing columns of an array:
col_r1 = a[:, 1]
col_r2 = a[:, 1:2]
print(col_r1, col_r1.shape) # Prints "[ 2 6 10] (3,)"
print(col_r2, col_r2.shape) # Prints "[[ 2]
# [ 6]
# [10]] (3, 1)"
------------------------
---------------------------------------------
5. Integer array indexing
When you index into numpy arrays using slicing, the resulting array view will always be a subarray of the original array. In contrast, integer array indexing allows you to construct arbitrary arrays using the data from another array. Here is an example:
5.1
a = np.array([[1,2], [3, 4], [5, 6]])
# An example of integer array indexing.
# The returned array will have shape (3,) and
print(a[[0, 1, 2], [0, 1, 0]]) # Prints "[1 4 5]"
Equivalent to
print(np.array([a[0, 0], a[1, 1], a[2, 0]])) # Prints "[1 4 5]"
5.2
# When using integer array indexing, you can reuse the same
# element from the source array:
print(a[[0, 0], [1, 1]]) # Prints "[2 2]"
Equivalent to
print(np.array([a[0, 1], a[0, 1]])) # Prints "[2 2]"
5.3 可以取出或者改变每一行的任何元素
# 取出
a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
b = np.array([0, 2, 0, 1])
print(a[np.arange(4), b]) #等价于a[[0 1 2 3],[0 2 0 1]],即5.1中提到的
# 修改
a[np.arange(4), b] += 10
------------------------
---------------------------------------------
6. Boolean array indexing
Boolean array indexing lets you pick out arbitrary elements of an array. Frequently this type of indexing is used to select the elements of an array that satisfy some condition.
6.1
# Here goes code
a = np.array([[1,2], [3, 4], [5, 6]])
bool_idx = (a > 2) # Find the elements of a that are bigger than 2;
# this returns a numpy array of Booleans of the same
# shape as a, where each slot of bool_idx tells
# whether that element of a is > 2.
print(bool_idx) # Prints "[[False False]
# [ True True]
# [ True True]]"
# We use boolean array indexing to construct a rank 1 array
# consisting of the elements of a corresponding to the True values
# of bool_idx
print(a[bool_idx]) # Prints "[3 4 5 6]"
6.2 # We can do all of the above in a single concise statement:
print(a[a > 2]) # Prints "[3 4 5 6]"
------------------------
---------------------------------------------
7. Datatypes
Every numpy array is a grid of elements of the same type. Numpy provides a large set of numeric datatypes that you can use to construct arrays. Numpy tries to guess a datatype when you create an array, but functions that construct arrays usually also include an optional argument to explicitly specify the datatype.
# Here goes code
x = np.array([1, 2]) # Let numpy choose the datatype
print(x.dtype) # Prints "int64" or "int32"
x = np.array([1.0, 2.0]) # Let numpy choose the datatype
print(x.dtype) # Prints "float64"
x = np.array([1, 2], dtype=np.int64) # Force a particular datatype
print(x.dtype) # Prints "int64"
------------------------
---------------------------------------------
8. Array math - add/sub/mul/div
Basic mathematical functions operate elementwise on arrays, and are available both as operator overloads and as functions in the numpy module.
# Here goes code
x = np.array([[1,2],[3,4]], dtype=np.float64)
y = np.array([[5,6],[7,8]], dtype=np.float64)
# add
# print x+y
# print np.add(x,y)
# print x.__add__(y)
#subtract
# print x-y
# print np.subtract(x,y)
# print x.__sub__(y)
# multiply
# print x*y
# print np.multiply(x,y)
# print x.__mul__(y)
# divide
print x/y
print np.divide(x,y)
print x.__div__(y)
# sqrt
print np.sqrt(x)
------------------------
---------------------------------------------
9. Array math - inner products: dot
Note that unlike MATLAB, * is elementwise multiplication, not matrix multiplication. We instead use the dot function to compute inner products of vectors, to multiply a vector by a matrix, and to multiply matrices. dot is available both as a function in the numpy module and as an instance method of array objects:
# Here goes code
# matrices
x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])
# vector
v = np.array([9,10])
w = np.array([11, 12])
# Inner product of vectors; both produce 219
print(v.dot(w))
print(np.dot(v, w))
# Matrix / vector product; both produce the rank 1 array [29 67]
print(x.dot(v))
print(np.dot(x, v))
# vector / Matrices product; both produce the rank 1 array [39 58]
print v.dot(x)
print np.dot(v,x)
Note: Like matrices product, but result is rank 1
# Matrix / matrix product; both produce the rank 2 array
# [[19 22]
# [43 50]]
print(x.dot(y))
print(np.dot(x, y))
Note: x.dot(y) is different with y.dot(x)
------------------------
---------------------------------------------
10. axis
10.1
data = np.array([[1,2,1],[0,3,1],[2,1,4],[1,3,1]])
# 这个数组代表了样本数据的特征,其中每一行代表一个样本的三个特征,每一列是不同样本的特征。如果在分析样本的过程中需要对每个样本的三个特征求和,该如何处理?
np.sum(data, axis=1)
-->array([4, 4, 7, 5])
# 那如果想求每种特征的最小值,该如何处理
np.min(data, axis=0)
-->array([0, 1, 1])
# 又如果想得知所有样本所有特征的平均值呢
np.average(data)
注:由此可以看出,通过不同的axis,numpy会沿着不同的方向进行操作:如果不设置,那么对所有的元素操作;如果axis=0,则沿着纵轴进行操作;axis=1,则沿着横轴进行操作。但这只是简单的二位数组,如果是多维的呢?可以总结为一句话:设axis=i,则numpy沿着第i个下标变化的放下进行操作。例如刚刚的例子,可以将表示为:data =[[a00, a01],[a10,a11]],所以axis=0时,沿着第0个下标变化的方向进行操作,也就是a00->a10, a01->a11,也就是纵坐标的方向,axis=1时也类似。
------------------------
---------------------------------------------
11. transpose a matrix : T
# Note that taking the transpose of a rank 1 array does nothing:
v = np.array([1,2,3])
print(v) # Prints "[1 2 3]"
print(v.T) # Prints "[1 2 3]"
------------------------
---------------------------------------------
12. Broadcasting
Broadcasting is a powerful mechanism that allows numpy to work with arrays of different shapes when performing arithmetic operations. Frequently we have a smaller array and a larger array, and we want to use the smaller array multiple times to perform some operation on the larger array.
12.1 create new matrix with same shape as other matrix
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
y = np.empty_like(x) # Create an empty matrix with the same shape as x
12.2 how to stack vector
v = np.array([1, 0, 1])
vv = np.tile(v, (4, 1)) # Stack 4 copies of v on top of each other
12.3 broadcast rules
Broadcasting two arrays together follows these rules:
1. If the arrays do not have the same rank, prepend the shape of the lower rank array with 1s until both shapes have the same length.
2. The two arrays are said to be compatible in a dimension if they have the same size in the dimension, or if one of the arrays has size 1 in that dimension.
3. The arrays can be broadcast together if they are compatible in all dimensions.
4. After broadcasting, each array behaves as if it had shape equal to the elementwise maximum of shapes of the two input arrays.
5. In any dimension where one array had size 1 and the other array had size greater than 1, the first array behaves as if it were copied along that dimension
12.4 compute an outer product
v = np.array([1,2,3]) # v has shape (3,)
w = np.array([4,5]) # w has shape (2,)
print np.reshape(v,(3,1)) * w
OR
print v.reshape(3,1) * w
# [[ 4 5]
# [ 8 10]
# [12 15]]
12.5
# Add a vector to each column of a matrix
# x has shape (2, 3) and w has shape (2,).
# If we transpose x then it has shape (3, 2) and can be broadcast
# against w to yield a result of shape (3, 2); transposing this result
# yields the final result of shape (2, 3) which is the matrix x with
# the vector w added to each column. Gives the following matrix:
# [[ 5 6 7]
# [ 9 10 11]]
print((x.T + w).T)
Equivalent to
print x+w.reshape(-1,1)
12.6
# Multiply a matrix by a constant:
# x has shape (2, 3). Numpy treats scalars as arrays of shape ();
# these can be broadcast together to shape (2, 3), producing the
# following array:
# [[ 2 4 6]
# [ 8 10 12]]
print(x * 2)
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------
------------------------
---------------------------------------------