@@ -56,5 +56,100 @@ b.swapaxes(0,1)
56
56
57
57
a = np.arange(0,6)
58
58
a = np.arange(0,6).reshape(2,3)
59
+ ========================================================================================
60
+ import numpy as np
61
+
62
+ pip install numpy
63
+ pip install numpy --upgrade
64
+
65
+ import numpy as np
66
+
67
+ a = np.array([2,3,4])
68
+
69
+ a = np.arange(1, 12, 2) # (from, to, step)
70
+
71
+ a = np.linspace(1, 12, 6) # (first, last, num_elements) float data type
72
+
73
+ a.reshape(3,2)
74
+ a = a.reshape(3,2)
75
+
76
+ a.size
77
+
78
+ a.shape
79
+
80
+ a.dtype
81
+
82
+ a.itemsize
83
+
84
+ # this works:
85
+ b = np.array([(1.5,2,3), (4,5,6)])
86
+
87
+ # but this does not work:
88
+ b = np.array(1,2,3) # square brackets are required
89
+
90
+ a < 4 # prints True/False
91
+
92
+ a * 3 # multiplies each element by 3
93
+ a *= 3 # saves result to a
94
+
95
+ a = np.zeros((3,4))
96
+
97
+ a = np.ones((2,3))
98
+
99
+ a = np.array([2,3,4], dtype=np.int16)
100
+
101
+ a = np.random.random((2,3))
102
+
103
+ np.set_printoptions(precision=2, suppress=True) # show 2 decimal places, suppress scientific notation
104
+
105
+ a = np.random.randint(0,10,5)
106
+ a.sum()
107
+ a.min()
108
+ a.max()
109
+ a.mean()
110
+ a.var() # variance
111
+ a.std() # standard deviation
112
+
113
+
114
+ a.sum(axis=1)
115
+ a.min(axis=0)
116
+
117
+ a.argmin() # index of min element
118
+ a.argmax() # index of max element
119
+ a.argsort() # returns array of indices that would put the array in sorted order
120
+ a.sort() # in place sort
121
+
122
+ # indexing, slicing, iterating
123
+ a = np.arange(10)**2
124
+ a[2]
125
+ a[2:5]
126
+
127
+ for i in a:
128
+ print (i ** 2)
129
+ a[::-1] # reverses array
130
+
131
+ for i in a.flat:
132
+ print (i)
133
+
134
+
135
+ a.transpose()
136
+
137
+ a.ravel() # flattens to 1D
138
+
139
+ # read in csv data file
140
+ data = np.loadtxt("data.txt", dtype=np.uint8, delimiter=",", skiprows=1 )
141
+ # loadtxt does not handle missing values. to handle such exceptions use genfromtxt instead.
142
+
143
+ data = np.loadtxt("data.txt", dtype=np.uint8, delimiter=",", skiprows=1, usecols=[0,1,2,3])
144
+
145
+ np.random.shuffle(a)
146
+
147
+ a = np.random.random(5)
148
+
149
+ np.random.choice(a)
150
+
151
+ np.random.random_integers(5,10,2) # (low, high inclusive, size)
152
+
153
+
59
154
60
-
155
+
0 commit comments