Skip to content

Commit 2817abe

Browse files
committed
add numpy and scipy/matplotlib examples
1 parent 8be549e commit 2817abe

7 files changed

Lines changed: 370 additions & 0 deletions

File tree

cnn_python_tutorial/numpy/array.py

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#!/usr/bin/env python
2+
import numpy as np
3+
4+
a = np.array([1, 2, 3])
5+
print type(a)
6+
print a.shape
7+
print a[0], a[1], a[2]
8+
a[0] = 5
9+
print a
10+
11+
b = np.array([[1,2,3], [4,5,6]])
12+
print b.shape # (2, 3), 2 lines, 3 columns, means two vectors, each have three dimensions
13+
print b[0,0], b[0,1], b[1,0]
14+
15+
a = np.zeros((2,2)) # Create an array of all zeros
16+
print a # Prints "[[ 0. 0.]
17+
# [ 0. 0.]]"
18+
19+
b = np.ones((1,2)) # Create an array of all ones
20+
print b # Prints "[[ 1. 1.]]"
21+
22+
c = np.full((2,2), 7) # Create a constant array
23+
print c # Prints "[[ 7. 7.]
24+
# [ 7. 7.]]"
25+
26+
d = np.eye(2) # Create a 2x2 identity matrix
27+
print d # Prints "[[ 1. 0.]
28+
# [ 0. 1.]]"
29+
30+
e = np.random.random((2,2)) # Create an array filled with random values
31+
print e # Might print "[[ 0.91940167 0.08143941]
32+
# [ 0.68744134 0.87236687]]"
33+
34+
35+
# Create the following rank 2 array with shape (3, 4)
36+
# [[ 1 2 3 4]
37+
# [ 5 6 7 8]
38+
# [ 9 10 11 12]]
39+
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
40+
41+
# Use slicing to pull out the subarray consisting of the first 2 rows
42+
# and columns 1 and 2; b is the following array of shape (2, 2):
43+
# [[2 3]
44+
# [6 7]]
45+
b = a[:2, 1:3]
46+
47+
# A slice of an array is a view into the same data, so modifying it
48+
# will modify the original array.
49+
print a[0, 1] # Prints "2"
50+
b[0, 0] = 77 # b[0, 0] is the same piece of data as a[0, 1]
51+
print a[0, 1] # Prints "77"
52+
53+
# Create the following rank 2 array with shape (3, 4)
54+
# [[ 1 2 3 4]
55+
# [ 5 6 7 8]
56+
# [ 9 10 11 12]]
57+
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
58+
59+
# Two ways of accessing the data in the middle row of the array.
60+
# Mixing integer indexing with slices yields an array of lower rank,
61+
# while using only slices yields an array of the same rank as the
62+
# original array:
63+
row_r1 = a[1, :] # Rank 1 view of the second row of a
64+
row_r2 = a[1:2, :] # Rank 2 view of the second row of a
65+
print row_r1, row_r1.shape # Prints "[5 6 7 8] (4,)"
66+
print row_r2, row_r2.shape # Prints "[[5 6 7 8]] (1, 4)"
67+
68+
# We can make the same distinction when accessing columns of an array:
69+
col_r1 = a[:, 1]
70+
col_r2 = a[:, 1:2]
71+
print col_r1, col_r1.shape # Prints "[ 2 6 10] (3,)"
72+
print col_r2, col_r2.shape # Prints "[[ 2]
73+
# [ 6]
74+
# [10]] (3, 1)"
75+
76+
77+
a = np.array([[1,2], [3, 4], [5, 6]])
78+
79+
# An example of integer array indexing.
80+
# The returned array will have shape (3,) and
81+
print a[[0, 1, 2], [0, 1, 0]] # Prints "[1 4 5]"
82+
83+
# The above example of integer array indexing is equivalent to this:
84+
print np.array([a[0, 0], a[1, 1], a[2, 0]]) # Prints "[1 4 5]"
85+
86+
87+
# When using integer array indexing, you can reuse the same
88+
# element from the source array:
89+
print a[[0, 0], [1, 1]] # Prints "[2 2]"
90+
91+
# Equivalent to the previous integer array indexing example
92+
print np.array([a[0, 1], a[0, 1]]) # Prints "[2 2]"
93+
94+
95+
a = np.array([[1,2], [3, 4], [5, 6]])
96+
97+
bool_idx = (a > 2) # Find the elements of a that are bigger than 2;
98+
# this returns a numpy array of Booleans of the same
99+
# shape as a, where each slot of bool_idx tells
100+
# whether that element of a is > 2.
101+
102+
print bool_idx # Prints "[[False False]
103+
# [ True True]
104+
# [ True True]]"
105+
106+
# We use boolean array indexing to construct a rank 1 array
107+
# consisting of the elements of a corresponding to the True values
108+
# of bool_idx
109+
print a[bool_idx] # Prints "[3 4 5 6]"
110+
111+
# We can do all of the above in a single concise statement:
112+
print a[a > 2] # Prints "[3 4 5 6]"
113+
114+
115+
x = np.array([1, 2]) # Let numpy choose the datatype
116+
print x.dtype # Prints "int64"
117+
118+
x = np.array([1.0, 2.0]) # Let numpy choose the datatype
119+
print x.dtype # Prints "float64"
120+
121+
x = np.array([1, 2], dtype=np.int64) # Force a particular datatype
122+
print x.dtype # Prints "int64"xample of integer array indexing.
123+
# The returned array will have shape (3,) and
124+
print a[[0, 1, 2], [0, 1, 0]] # Prints "[1 4 5]"
125+
126+
# The above example of integer array indexing is equivalent to this:
127+
print np.array([a[0, 0], a[1, 1], a[2, 0]]) # Prints "[1 4 5]"
128+
129+
130+
# When using integer array indexing, you can reuse the same
131+
# element from the source array:
132+
print a[[0, 0], [1, 1]] # Prints "[2 2]"
133+
134+
# Equivalent to the previous integer array indexing example
135+
print np.array([a[0, 1], a[0, 1]]) # Prints "[2 2]"
136+
137+
a = np.array([[1,2], [3, 4], [5, 6]])
138+
139+
bool_idx = (a > 2) # Find the elements of a that are bigger than 2;
140+
# this returns a numpy array of Booleans of the same
141+
# shape as a, where each slot of bool_idx tells
142+
# whether that element of a is > 2.
143+
144+
print bool_idx # Prints "[[False False]
145+
# [ True True]
146+
# [ True True]]"
147+
148+
# We use boolean array indexing to construct a rank 1 array
149+
# consisting of the elements of a corresponding to the True values
150+
# of bool_idx
151+
print a[bool_idx] # Prints "[3 4 5 6]"
152+
153+
# We can do all of the above in a single concise statement:
154+
print a[a > 2] # Prints "[3 4 5 6]"
155+
156+

cnn_python_tutorial/numpy/image.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
3+
from scipy.misc import imread, imsave, imresize, imshow
4+
5+
img = imread('vim.png')
6+
print img.dtype, img.shape
7+
8+
img_tinted = img * [1, 0.95, 0.9, 0.9] # here we multiply by 4-dim instead of 3-dim as it has 4 for svg images
9+
img_tinted = imresize(img_tinted, (300, 300))
10+
11+
imsave('vim_tinted.png', img_tinted)
12+
imshow(img)
13+
imshow(img_tinted)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
3+
import numpy as np
4+
from scipy.spatial.distance import pdist, squareform
5+
6+
### For matlab interface
7+
# scipy.io.loadmat and scipy.io.savemat
8+
9+
10+
# Create the following array where each row is a point in 2D space:
11+
# [[0 1]
12+
# [1 0]
13+
# [2 0]]
14+
x = np.array([[0, 1], [1, 0], [2, 0]])
15+
print x
16+
17+
# Compute the Euclidean distance between all rows of x.
18+
# d[i, j] is the Euclidean distance between x[i, :] and x[j, :],
19+
# and d is the following array:
20+
# [[ 0. 1.41421356 2.23606798]
21+
# [ 1.41421356 0. 1. ]
22+
# [ 2.23606798 1. 0. ]]
23+
d = squareform(pdist(x, 'euclidean'))
24+
print d

cnn_python_tutorial/numpy/math.py

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#!/usr/bin/env python
2+
import numpy as np
3+
4+
x = np.array([[1,2],[3,4]], dtype=np.float64)
5+
y = np.array([[5,6],[7,8]], dtype=np.float64)
6+
7+
# Elementwise sum; both produce the array
8+
# [[ 6.0 8.0]
9+
# [10.0 12.0]]
10+
print x + y
11+
print np.add(x, y)
12+
13+
# Elementwise difference; both produce the array
14+
# [[-4.0 -4.0]
15+
# [-4.0 -4.0]]
16+
print x - y
17+
print np.subtract(x, y)
18+
19+
# Elementwise product; both produce the array
20+
# [[ 5.0 12.0]
21+
# [21.0 32.0]]
22+
print x * y
23+
print np.multiply(x, y)
24+
25+
# Elementwise division; both produce the array
26+
# [[ 0.2 0.33333333]
27+
# [ 0.42857143 0.5 ]]
28+
print x / y
29+
print np.divide(x, y)
30+
31+
# Elementwise square root; produces the array
32+
# [[ 1. 1.41421356]
33+
# [ 1.73205081 2. ]]
34+
print np.sqrt(x)
35+
36+
37+
x = np.array([[1,2],[3,4]])
38+
y = np.array([[5,6],[7,8]])
39+
40+
v = np.array([9,10])
41+
w = np.array([11, 12])
42+
43+
# Inner product of vectors; both produce 219
44+
print v.dot(w)
45+
print np.dot(v, w)
46+
47+
# Matrix / vector product; both produce the rank 1 array [29 67]
48+
print x.dot(v)
49+
print np.dot(x, v)
50+
51+
# Matrix / matrix product; both produce the rank 2 array
52+
# [[19 22]
53+
# [43 50]]
54+
print x.dot(y)
55+
print np.dot(x, y)
56+
57+
58+
x = np.array([[1,2],[3,4]])
59+
60+
print np.sum(x) # Compute sum of all elements; prints "10"
61+
print np.sum(x, axis=0) # Compute sum of each column; prints "[4 6]"
62+
print np.sum(x, axis=1) # Compute sum of each row; prints "[3 7]"
63+
64+
65+
x = np.array([[1,2], [3,4]])
66+
print x # Prints "[[1 2]
67+
# [3 4]]"
68+
print x.T # Prints "[[1 3]
69+
# [2 4]]"
70+
71+
# Note that taking the transpose of a rank 1 array does nothing:
72+
v = np.array([1,2,3])
73+
print v # Prints "[1 2 3]"
74+
print v.T # Prints "[1 2 3]"
75+
76+
77+
78+
79+
# We will add the vector v to each row of the matrix x,
80+
# storing the result in the matrix y
81+
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
82+
v = np.array([1, 0, 1])
83+
y = np.empty_like(x) # Create an empty matrix with the same shape as x
84+
85+
# Add the vector v to each row of the matrix x with an explicit loop
86+
for i in range(4):
87+
y[i, :] = x[i, :] + v
88+
89+
# Now y is the following
90+
# [[ 2 2 4]
91+
# [ 5 5 7]
92+
# [ 8 8 10]
93+
# [11 11 13]]
94+
print y
95+
96+
97+
# We will add the vector v to each row of the matrix x,
98+
# storing the result in the matrix y
99+
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
100+
v = np.array([1, 0, 1])
101+
vv = np.tile(v, (4, 1)) # Stack 4 copies of v on top of each other
102+
print vv # Prints "[[1 0 1]
103+
# [1 0 1]
104+
# [1 0 1]
105+
# [1 0 1]]"
106+
y = x + vv # Add x and vv elementwise
107+
print y # Prints "[[ 2 2 4
108+
# [ 5 5 7]
109+
# [ 8 8 10]
110+
# [11 11 13]]"
111+
112+
113+
# We will add the vector v to each row of the matrix x,
114+
# storing the result in the matrix y
115+
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
116+
v = np.array([1, 0, 1])
117+
y = x + v # Add v to each row of x using broadcasting
118+
print y # Prints "[[ 2 2 4]
119+
# [ 5 5 7]
120+
# [ 8 8 10]
121+
# [11 11 13]]"
122+
123+
124+
# Compute outer product of vectors
125+
v = np.array([1,2,3]) # v has shape (3,)
126+
w = np.array([4,5]) # w has shape (2,)
127+
# To compute an outer product, we first reshape v to be a column
128+
# vector of shape (3, 1); we can then broadcast it against w to yield
129+
# an output of shape (3, 2), which is the outer product of v and w:
130+
# [[ 4 5]
131+
# [ 8 10]
132+
# [12 15]]
133+
print np.reshape(v, (3, 1)) * w
134+
135+
# Add a vector to each row of a matrix
136+
x = np.array([[1,2,3], [4,5,6]])
137+
# x has shape (2, 3) and v has shape (3,) so they broadcast to (2, 3),
138+
# giving the following matrix:
139+
# [[2 4 6]
140+
# [5 7 9]]
141+
print x + v
142+
143+
# Add a vector to each column of a matrix
144+
# x has shape (2, 3) and w has shape (2,).
145+
# If we transpose x then it has shape (3, 2) and can be broadcast
146+
# against w to yield a result of shape (3, 2); transposing this result
147+
# yields the final result of shape (2, 3) which is the matrix x with
148+
# the vector w added to each column. Gives the following matrix:
149+
# [[ 5 6 7]
150+
# [ 9 10 11]]
151+
print (x.T + w).T
152+
# Another solution is to reshape w to be a row vector of shape (2, 1);
153+
# we can then broadcast it directly against x to produce the same
154+
# output.
155+
print x + np.reshape(w, (2, 1))
156+
157+
# Multiply a matrix by a constant:
158+
# x has shape (2, 3). Numpy treats scalars as arrays of shape ();
159+
# these can be broadcast together to shape (2, 3), producing the
160+
# following array:
161+
# [[ 2 4 6]
162+
# [ 8 10 12]]
163+
print x * 2
164+
165+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python
2+
3+
# for matplotlib to plotting the graph
4+
5+
import numpy as np
6+
import matplotlib.pyplot as plt
7+
8+
x = np.arange(0, 3 * np.pi, 0.1)
9+
y = np.sin(x)
10+
11+
plt.plot(x, y)
12+
plt.show()

cnn_python_tutorial/numpy/vim.png

191 KB
Loading
14.9 KB
Loading

0 commit comments

Comments
 (0)