forked from joeyajames/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumpy commands.txt
More file actions
155 lines (111 loc) · 3.34 KB
/
Numpy commands.txt
File metadata and controls
155 lines (111 loc) · 3.34 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
https://docs.scipy.org/doc/numpy-1.12.0/reference/
Numpy
install Numpy using pip
import numpy as np
ndarray - an N-dimensional array, which describes a collection of “items” of the same type
array(list) # constructor
asarray(a[, dtype, order]) # Convert the input to an array
Constants:
ndarray.shape tuple of array dimensions
ndarray.size number of elements in array
ndarray.itemsize size of one element
ndarray.dtype data type of elements
ndarray.flat 1D iterator over elements of array
Common Functions
np.tolist()
np.reshape(a, (3,2))
np.swapaxes(axis1, axis2)
np.copy()
arange()
Statistics Functions:
np.sum(a, axis)
np.prod
np.min
np.max
np.mean
np.std standard deviation
np.var
np.sort(axis)
Other Functions:
String operations
logical operations - AND, OR, XOR, NOT, >, <, =, ...
trig functions
complex numbers (real + imaginary)
polynomials
2D matrix operations
Fourier transforms
====================================================================================
import numpy as np
x = [0,1,2,3,4,5]
a = np.array(x)
index: a[2]
slice: a[start:stop:step]
a[1:4:2]
a[3:]
a[:3]
a.shape
a.size
a.itemsize
a.dtype
b = np.array([[1,2,3], [4,5,6]])
b.swapaxes(0,1)
a = np.arange(0,6)
a = np.arange(0,6).reshape(2,3)
========================================================================================
import numpy as np
pip install numpy
pip install numpy --upgrade
import numpy as np
a = np.array([2,3,4])
a = np.arange(1, 12, 2) # (from, to, step)
a = np.linspace(1, 12, 6) # (first, last, num_elements) float data type
a.reshape(3,2)
a = a.reshape(3,2)
a.size
a.shape
a.dtype
a.itemsize
# this works:
b = np.array([(1.5,2,3), (4,5,6)])
# but this does not work:
b = np.array(1,2,3) # square brackets are required
a < 4 # prints True/False
a * 3 # multiplies each element by 3
a *= 3 # saves result to a
a = np.zeros((3,4))
a = np.ones((2,3))
a = np.array([2,3,4], dtype=np.int16)
a = np.random.random((2,3))
np.set_printoptions(precision=2, suppress=True) # show 2 decimal places, suppress scientific notation
a = np.random.randint(0,10,5)
a.sum()
a.min()
a.max()
a.mean()
a.var() # variance
a.std() # standard deviation
a.sum(axis=1)
a.min(axis=0)
a.argmin() # index of min element
a.argmax() # index of max element
a.argsort() # returns array of indices that would put the array in sorted order
a.sort() # in place sort
# indexing, slicing, iterating
a = np.arange(10)**2
a[2]
a[2:5]
for i in a:
print (i ** 2)
a[::-1] # reverses array
for i in a.flat:
print (i)
a.transpose()
a.ravel() # flattens to 1D
# read in csv data file
data = np.loadtxt("data.txt", dtype=np.uint8, delimiter=",", skiprows=1 )
# loadtxt does not handle missing values. to handle such exceptions use genfromtxt instead.
data = np.loadtxt("data.txt", dtype=np.uint8, delimiter=",", skiprows=1, usecols=[0,1,2,3])
np.random.shuffle(a)
a = np.random.random(5)
np.random.choice(a)
np.random.random_integers(5,10,2) # (low, high inclusive, size)