Skip to content

Commit e832e8e

Browse files
committed
Initial commit
0 parents  commit e832e8e

File tree

161 files changed

+6458
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+6458
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.ipynb_checkpoints
2+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env/python
2+
3+
import sys
4+
from datetime import datetime
5+
import numpy as np
6+
7+
"""
8+
This program demonstrates vector addition the Python way.
9+
Run from the command line as follows
10+
11+
python vectorsum.py n
12+
13+
where n is an integer that specifies the size of the vectors.
14+
15+
The first vector to be added contains the squares of 0 up to n.
16+
The second vector contains the cubes of 0 up to n.
17+
The program prints the last 2 elements of the sum and the elapsed time.
18+
"""
19+
20+
def numpysum(n):
21+
a = np.arange(n) ** 2
22+
b = np.arange(n) ** 3
23+
c = a + b
24+
25+
return c
26+
27+
def pythonsum(n):
28+
a = range(n)
29+
b = range(n)
30+
c = []
31+
32+
for i in range(len(a)):
33+
a[i] = i ** 2
34+
b[i] = i ** 3
35+
c.append(a[i] + b[i])
36+
37+
return c
38+
39+
40+
size = int(sys.argv[1])
41+
42+
start = datetime.now()
43+
c = pythonsum(size)
44+
delta = datetime.now() - start
45+
print "The last 2 elements of the sum", c[-2:]
46+
print "PythonSum elapsed time in microseconds", delta.microseconds
47+
48+
start = datetime.now()
49+
c = numpysum(size)
50+
delta = datetime.now() - start
51+
print "The last 2 elements of the sum", c[-2:]
52+
print "NumPySum elapsed time in microseconds", delta.microseconds
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import numpy as np
2+
3+
# Demonstrates the dtype and shape attributes
4+
# of ndarray.
5+
#
6+
# Run from the commandline with
7+
#
8+
# python arrayattributes.py
9+
a = np.arange(5)
10+
print "In: a = arange(5)"
11+
12+
print "In: a.dtype"
13+
print a.dtype
14+
#Out: dtype('int64')
15+
print
16+
17+
print "In: a.shape"
18+
print a.shape
19+
#Out: (5,)
20+
print
21+
22+
print "In: a"
23+
print a
24+
#Out[4]: array([0, 1, 2, 3, 4])
25+
print
26+
27+
m = np.array([np.arange(2), np.arange(2)])
28+
29+
print "In: m"
30+
print m
31+
#Out:
32+
#array([[0, 1],
33+
# [0, 1]])
34+
print
35+
36+
print "In: m.shape"
37+
print m.shape
38+
#Out: (2, 2)
39+
40+
print "In: m.dtype"
41+
print m.dtype
42+
#Out: dtype('int64')
43+
print
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import numpy as np
2+
3+
# Demonstrates ndarray attributes.
4+
#
5+
# Run from the commandline with
6+
#
7+
# python arrayattributes2.py
8+
b = np.arange(24).reshape(2, 12)
9+
print "In: b"
10+
print b
11+
#Out:
12+
#array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
13+
# [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]])
14+
15+
print "In: b.ndim"
16+
print b.ndim
17+
#Out: 2
18+
19+
print "In: b.size"
20+
print b.size
21+
#Out: 24
22+
23+
print "In: b.itemsize"
24+
print b.itemsize
25+
#Out: 8
26+
27+
print "In: b.nbytes"
28+
print b.nbytes
29+
#Out: 192
30+
31+
print "In: b.size * b.itemsize"
32+
print b.size * b.itemsize
33+
#Out: 192
34+
35+
print "In: b.resize(6,4)"
36+
print b.resize(6,4)
37+
38+
print "In: b"
39+
print b
40+
#Out:
41+
#array([[ 0, 1, 2, 3],
42+
# [ 4, 5, 6, 7],
43+
# [ 8, 9, 10, 11],
44+
# [12, 13, 14, 15],
45+
# [16, 17, 18, 19],
46+
# [20, 21, 22, 23]])
47+
48+
print "In: b.T"
49+
print b.T
50+
#Out:
51+
#array([[ 0, 4, 8, 12, 16, 20],
52+
# [ 1, 5, 9, 13, 17, 21],
53+
# [ 2, 6, 10, 14, 18, 22],
54+
# [ 3, 7, 11, 15, 19, 23]])
55+
56+
print "In: b.ndim"
57+
print b.ndim
58+
#Out: 1
59+
60+
print "In: b.T"
61+
print b.T
62+
#Out: array([0, 1, 2, 3, 4])
63+
64+
65+
print "In: b = array([1.j + 1, 2.j + 3])"
66+
b = np.array([1.j + 1, 2.j + 3])
67+
68+
print "In: b"
69+
print b
70+
#Out: array([ 1.+1.j, 3.+2.j])
71+
72+
print "In: b.real"
73+
print b.real
74+
#Out: array([ 1., 3.])
75+
76+
print "In: b.imag"
77+
print b.imag
78+
#Out: array([ 1., 2.])
79+
80+
print "In: b.dtype"
81+
print b.dtype
82+
#Out: dtype('complex128')
83+
84+
print "In: b.dtype.str"
85+
print b.dtype.str
86+
#Out: '<c16'
87+
88+
89+
print "In: b = arange(4).reshape(2,2)"
90+
b = np.arange(4).reshape(2,2)
91+
92+
print "In: b"
93+
print b
94+
#Out:
95+
#array([[0, 1],
96+
# [2, 3]])
97+
98+
print "In: f = b.flat"
99+
f = b.flat
100+
101+
print "In: f"
102+
print f
103+
#Out: <numpy.flatiter object at 0x103013e00>
104+
105+
print "In: for it in f: print it"
106+
for it in f:
107+
print it
108+
#0
109+
#1
110+
#2
111+
#3
112+
113+
print "In: b.flat[2]"
114+
print b.flat[2]
115+
#Out: 2
116+
117+
118+
print "In: b.flat[[1,3]]"
119+
print b.flat[[1,3]]
120+
#Out: array([1, 3])
121+
122+
123+
print "In: b"
124+
print b
125+
#Out:
126+
#array([[7, 7],
127+
# [7, 7]])
128+
129+
print "In: b.flat[[1,3]] = 1"
130+
b.flat[[1,3]] = 1
131+
132+
print "In: b"
133+
print b
134+
#Out:
135+
#array([[7, 1],
136+
# [7, 1]])
137+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import numpy as np
2+
3+
# Demonstrates the NumPy record data type.
4+
#
5+
# Run from the commandline with
6+
#
7+
# python arrayconversion.py
8+
b = np.array([ 1.+1.j, 3.+2.j])
9+
print "In: b"
10+
print b
11+
#Out: array([ 1.+1.j, 3.+2.j])
12+
13+
print "In: b.tolist()"
14+
print b.tolist()
15+
#Out: [(1+1j), (3+2j)]
16+
17+
print "In: b.tostring()"
18+
print b.tostring()
19+
#Out: '\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x00@'
20+
21+
print "In: fromstring('\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x00@', dtype=complex)"
22+
print np.fromstring('\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x00@', dtype=complex)
23+
#Out: array([ 1.+1.j, 3.+2.j]
24+
25+
print "In: fromstring('20:42:52',sep=':', dtype=int)"
26+
print np.fromstring('20:42:52',sep=':', dtype=int)
27+
#Out: array([20, 42, 52])
28+
29+
print "In: b"
30+
print b
31+
#Out: array([ 1.+1.j, 3.+2.j])
32+
33+
print "In: b.astype(int)"
34+
print b.astype(int)
35+
#/usr/local/bin/ipython:1: ComplexWarning: Casting complex values to real discards the imaginary part
36+
# #!/usr/bin/python
37+
#Out: array([1, 3])
38+
39+
print "In: b.astype('complex')"
40+
print b.astype('complex')
41+
#Out: array([ 1.+1.j, 3.+2.j])
42+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import scipy.misc
2+
import matplotlib.pyplot as plt
3+
import numpy as np
4+
5+
# Load the Lena array
6+
lena = scipy.misc.lena()
7+
8+
def get_indices(size):
9+
arr = np.arange(size)
10+
return arr % 4 == 0
11+
12+
# Plot Lena
13+
lena1 = lena.copy()
14+
xindices = get_indices(lena.shape[0])
15+
yindices = get_indices(lena.shape[1])
16+
lena1[xindices, yindices] = 0
17+
plt.subplot(211)
18+
plt.imshow(lena1)
19+
20+
lena2 = lena.copy()
21+
# Between quarter and 3 quarters of the max value
22+
lena2[(lena > lena.max()/4) & (lena < 3 * lena.max()/4)] = 0
23+
plt.subplot(212)
24+
plt.imshow(lena2)
25+
26+
27+
plt.show()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import scipy.io.wavfile
2+
import matplotlib.pyplot as plt
3+
import urllib2
4+
import numpy as np
5+
6+
response = urllib2.urlopen('http://www.thesoundarchive.com/austinpowers/smashingbaby.wav')
7+
print response.info()
8+
WAV_FILE = 'smashingbaby.wav'
9+
filehandle = open(WAV_FILE, 'w')
10+
filehandle.write(response.read())
11+
filehandle.close()
12+
sample_rate, data = scipy.io.wavfile.read(WAV_FILE)
13+
print "Data type", data.dtype, "Shape", data.shape
14+
15+
plt.subplot(2, 1, 1)
16+
plt.title("Original")
17+
plt.plot(data)
18+
19+
newdata = data * 0.2
20+
newdata = newdata.astype(np.uint8)
21+
print "Data type", newdata.dtype, "Shape", newdata.shape
22+
23+
scipy.io.wavfile.write("quiet.wav",
24+
sample_rate, newdata)
25+
26+
plt.subplot(2, 1, 2)
27+
plt.title("Quiet")
28+
plt.plot(newdata)
29+
30+
plt.show()
31+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import numpy as np
2+
3+
# Demonstrates the NumPy dtype character codes.
4+
#
5+
# Run from the commandline with
6+
#
7+
# python charcodes.py
8+
9+
print "In: arange(7, dtype='f')"
10+
print np.arange(7, dtype='f')
11+
#Out: array([ 0., 1., 2., 3., 4., 5., 6.], dtype=float32)
12+
13+
print "In: arange(7, dtype='D')"
14+
print np.arange(7, dtype='D')
15+
#Out: array([ 0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j, 5.+0.j, 6.+0.j])
16+
17+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import scipy.misc
2+
import matplotlib.pyplot as plt
3+
4+
lena = scipy.misc.lena()
5+
print lena.shape
6+
acopy = lena.copy()
7+
aview = lena.view()
8+
9+
# Plot the Lena array
10+
plt.subplot(221)
11+
plt.imshow(lena)
12+
13+
#Plot the copy
14+
plt.subplot(222)
15+
plt.imshow(acopy)
16+
17+
#Plot the view
18+
plt.subplot(223)
19+
plt.imshow(aview)
20+
21+
# Plot the view after changes
22+
aview.flat = 0
23+
plt.subplot(224)
24+
plt.imshow(aview)
25+
26+
plt.show()
27+

0 commit comments

Comments
 (0)