Skip to content

Commit 52ac502

Browse files
authoredJun 15, 2018
Uploaded Numpy tutorial file
1 parent a1e41b2 commit 52ac502

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
 

Diff for: ‎numpy_tutorial.txt

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import numpy as np
2+
3+
pip install numpy
4+
pip install numpy --upgrade
5+
6+
import numpy as np
7+
8+
a = np.array([2,3,4])
9+
10+
a = np.arange(1, 12, 2) # (from, to, step)
11+
12+
a = np.linspace(1, 12, 6) # (first, last, num_elements) float data type
13+
14+
a.reshape(3,2)
15+
a = a.reshape(3,2)
16+
17+
a.size
18+
19+
a.shape
20+
21+
a.dtype
22+
23+
a.itemsize
24+
25+
# this works:
26+
b = np.array([(1.5,2,3), (4,5,6)])
27+
28+
# but this does not work:
29+
b = np.array(1,2,3) # square brackets are required
30+
31+
a < 4 # prints True/False
32+
33+
a * 3 # multiplies each element by 3
34+
a *= 3 # saves result to a
35+
36+
a = np.zeros((3,4))
37+
38+
a = np.ones((2,3))
39+
40+
a = np.array([2,3,4], dtype=np.int16)
41+
42+
a = np.random.random((2,3))
43+
44+
np.set_printoptions(precision=2, suppress=True) # show 2 decimal places, suppress scientific notation
45+
46+
a = np.random.randint(0,10,5)
47+
a.sum()
48+
a.min()
49+
a.max()
50+
a.mean()
51+
a.var() # variance
52+
a.std() # standard deviation
53+
54+
55+
a.sum(axis=1)
56+
a.min(axis=0)
57+
58+
a.argmin() # index of min element
59+
a.argmax() # index of max element
60+
a.argsort() # returns array of indices that would put the array in sorted order
61+
a.sort() # in place sort
62+
63+
# indexing, slicing, iterating
64+
a = np.arange(10)**2
65+
a[2]
66+
a[2:5]
67+
68+
for i in a:
69+
print (i ** 2)
70+
a[::-1] # reverses array
71+
72+
for i in a.flat:
73+
print (i)
74+
75+
76+
a.transpose()
77+
78+
a.ravel() # flattens to 1D
79+
80+
# read in csv data file
81+
data = np.loadtxt("data.txt", dtype=np.uint8, delimiter=",", skiprows=1 )
82+
# loadtxt does not handle missing values. to handle such exceptions use genfromtxt instead.
83+
84+
data = np.loadtxt("data.txt", dtype=np.uint8, delimiter=",", skiprows=1, usecols=[0,1,2,3])
85+
86+
np.random.shuffle(a)
87+
88+
a = np.random.random(5)
89+
90+
np.random.choice(a)
91+
92+
np.random.random_integers(5,10,2) # (low, high inclusive, size)
93+
94+
95+
96+
97+
98+
99+
100+
101+
102+
103+
104+
105+

0 commit comments

Comments
 (0)
Please sign in to comment.