pwd
'/Users/bb/Documents/GitHub/MTH548/preps/week_1'
import numpy as np
x = np.array([1, 2, 3, 4, 5])
x
array([1, 2, 3, 4, 5])
5*x
array([ 5, 10, 15, 20, 25])
x**2
array([ 1, 4, 9, 16, 25])
y = np.array([10, 10, 10, 10, 10])
y
array([10, 10, 10, 10, 10])
x+y
array([11, 12, 13, 14, 15])
v = np.arange(5)
v
array([0, 1, 2, 3, 4])
v = np.arange(1, 6)
v
array([1, 2, 3, 4, 5])
z = np.ones(5)
z
array([1., 1., 1., 1., 1.])
10*z
array([10., 10., 10., 10., 10.])
z.dtype
dtype('float64')
y.dtype
dtype('int64')
z = np.ones(5, dtype=int)
z
array([1, 1, 1, 1, 1])
z.dtype
dtype('int64')
z*10
array([10, 10, 10, 10, 10])
Note: Vectorized operations on numpy arrays are much faster than Python loops:
x = np.arange(10)
x**2
array([ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81])
%%timeit -n5 -r1
x = np.arange(10**6)
y = x**2
1.97 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 5 loops each)
%%timeit -n5 -r1
x = range(10**6)
y = []
for i in x:
y.append(i**2)
302 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 5 loops each)
%%timeit -n5 -r1
y = [i**2 for i in range(10**6)]
275 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 5 loops each)
x = np.arange(10)
x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
x[0]
0
x[0] = 100
x
array([100, 1, 2, 3, 4, 5, 6, 7, 8, 9])
x[0] = 3.14
x
array([3, 1, 2, 3, 4, 5, 6, 7, 8, 9])
x.dtype
dtype('int64')
xf = np.arange(10, dtype=float)
xf
array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
xf.dtype
dtype('float64')
xf[0] = 3.14
xf
array([3.14, 1. , 2. , 3. , 4. , 5. , 6. , 7. , 8. , 9. ])
x[0] = "hello"
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /var/folders/vd/9gpvwb493r52y4sgtl_fvtvm0000gn/T/ipykernel_22730/583455524.py in <module> ----> 1 x[0] = "hello" ValueError: invalid literal for int() with base 10: 'hello'
xf[0] = "hi"
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /var/folders/vd/9gpvwb493r52y4sgtl_fvtvm0000gn/T/ipykernel_22730/181643466.py in <module> ----> 1 xf[0] = "hi" ValueError: could not convert string to float: 'hi'
x
array([3, 1, 2, 3, 4, 5, 6, 7, 8, 9])
x[:5]
array([3, 1, 2, 3, 4])
x[2:5]
array([2, 3, 4])
x[3:9:2]
array([3, 5, 7])
x[5:]
array([5, 6, 7, 8, 9])
Note: Changing an array slice will change the original array.
x
array([3, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = x[:5]
y
array([3, 1, 2, 3, 4])
y[0] = 100
y
array([100, 1, 2, 3, 4])
x
array([100, 1, 2, 3, 4, 5, 6, 7, 8, 9])
This is different than list slicing:
m = [1, 2, 3, 4, 5]
m
[1, 2, 3, 4, 5]
n = m[:3]
n
[1, 2, 3]
n[0] = 100
n
[100, 2, 3]
m
[1, 2, 3, 4, 5]
Create a copy of a slice to keep the original array unchanged:
x
array([100, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = x[:5].copy()
y
array([100, 1, 2, 3, 4])
y[0] = -50
y
array([-50, 1, 2, 3, 4])
x
array([100, 1, 2, 3, 4, 5, 6, 7, 8, 9])
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [2, 4 , 1], 'bo-');
x = np.linspace(-10, 10, 100)
plt.plot(x, x**2)
[<matplotlib.lines.Line2D at 0x7fc2dd32d250>]
x = np.linspace(-10, 10, 1000)
plt.plot(x, np.sin(5*x));
x = np.arange(20)
x
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
y = x.reshape(5, 4)
y
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]])
y.shape
(5, 4)
x.reshape(5, -1)
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]])
x.reshape(6, -1)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /var/folders/vd/9gpvwb493r52y4sgtl_fvtvm0000gn/T/ipykernel_22730/2067418947.py in <module> ----> 1 x.reshape(6, -1) ValueError: cannot reshape array of size 20 into shape (6,newaxis)
x.reshape(-1, 4)
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]])
z = y.flatten()
z
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
z.shape
(20,)
y
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]])
2*y
array([[ 0, 2, 4, 6], [ 8, 10, 12, 14], [16, 18, 20, 22], [24, 26, 28, 30], [32, 34, 36, 38]])
y**2
array([[ 0, 1, 4, 9], [ 16, 25, 36, 49], [ 64, 81, 100, 121], [144, 169, 196, 225], [256, 289, 324, 361]])
z = np.ones((5, 4), dtype=int)
z
array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]])
y + z
array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]])
y
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]])
np.sin(y)
array([[ 0. , 0.84147098, 0.90929743, 0.14112001], [-0.7568025 , -0.95892427, -0.2794155 , 0.6569866 ], [ 0.98935825, 0.41211849, -0.54402111, -0.99999021], [-0.53657292, 0.42016704, 0.99060736, 0.65028784], [-0.28790332, -0.96139749, -0.75098725, 0.14987721]])
y
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]])
y[2, 1]
9
y[2, 1] = -3
y
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, -3, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]])
y[:3, :2]
array([[ 0, 1], [ 4, 5], [ 8, -3]])
y[:3, :]
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, -3, 10, 11]])
y[:3]
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, -3, 10, 11]])
y[: , :2]
array([[ 0, 1], [ 4, 5], [ 8, -3], [12, 13], [16, 17]])
y[2:4, 1:3]
array([[-3, 10], [13, 14]])
import numpy as np
import matplotlib.pyplot as plt
a = np.linspace(0, 0.9, 10).reshape(1, -1)
a
array([[0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]])
plt.figure(figsize=(10, 10))
plt.imshow(a, cmap='winter')
<matplotlib.image.AxesImage at 0x7f85425bf430>
b = a.reshape(2, -1)
b
array([[0. , 0.1, 0.2, 0.3, 0.4], [0.5, 0.6, 0.7, 0.8, 0.9]])
plt.figure(figsize=(10, 10))
plt.imshow(b, cmap='winter')
<matplotlib.image.AxesImage at 0x7f854263db20>
import matplotlib.pyplot as plt
import numpy as np
def show(a, size=6, cmap="seismic", text='limegreen', fontsize="large"):
'''
Display an image of a 2-dimensional numpy array.
It is assumed that values of the array are between
0 and 1.
'''
h, w = a.shape
plt.figure(figsize=(size, size))
ax = plt.subplot(111)
plt.xticks(range(w + 1))
plt.yticks(range(h + 1))
for (j, i), v in np.ndenumerate(a):
ax.text(i,
j,
f"{v:.1f}",
ha='center',
va='center',
color=text,
weight=900,
fontsize=fontsize)
plt.imshow(a, cmap=cmap, vmin=0, vmax=1)
plt.show()
show(b)
b = np.zeros((10, 10))
b
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
show(b)
b[2:5, 3:6] = 1
show(b)
b = np.zeros((10, 10))
b[2, :] = 1
show(b)
b = np.zeros((10, 10))
b[2] = 1
show(b)
b = np.zeros((10, 10))
b[:, 3] = 1
show(b)
b = np.zeros((10, 10))
b[::2, ::2] = 1
show(b)
rng = np.random.default_rng(0)
b = rng.random((5, 5))
b
array([[0.63696169, 0.26978671, 0.04097352, 0.01652764, 0.81327024], [0.91275558, 0.60663578, 0.72949656, 0.54362499, 0.93507242], [0.81585355, 0.0027385 , 0.85740428, 0.03358558, 0.72965545], [0.17565562, 0.86317892, 0.54146122, 0.29971189, 0.42268722], [0.02831967, 0.12428328, 0.67062441, 0.64718951, 0.61538511]])
np.set_printoptions(precision=2)
b
array([[0.64, 0.27, 0.04, 0.02, 0.81], [0.91, 0.61, 0.73, 0.54, 0.94], [0.82, 0. , 0.86, 0.03, 0.73], [0.18, 0.86, 0.54, 0.3 , 0.42], [0.03, 0.12, 0.67, 0.65, 0.62]])
b > 0.5
array([[ True, False, False, False, True], [ True, True, True, True, True], [ True, False, True, False, True], [False, True, True, False, False], [False, False, True, True, True]])
b[b > 0.5] = 5
b
array([[5.00e+00, 2.70e-01, 4.10e-02, 1.65e-02, 5.00e+00], [5.00e+00, 5.00e+00, 5.00e+00, 5.00e+00, 5.00e+00], [5.00e+00, 2.74e-03, 5.00e+00, 3.36e-02, 5.00e+00], [1.76e-01, 5.00e+00, 5.00e+00, 3.00e-01, 4.23e-01], [2.83e-02, 1.24e-01, 5.00e+00, 5.00e+00, 5.00e+00]])
rng = np.random.default_rng(0)
b = rng.random((5, 5))
c = rng.random((5, 5))
b
array([[0.64, 0.27, 0.04, 0.02, 0.81], [0.91, 0.61, 0.73, 0.54, 0.94], [0.82, 0. , 0.86, 0.03, 0.73], [0.18, 0.86, 0.54, 0.3 , 0.42], [0.03, 0.12, 0.67, 0.65, 0.62]])
c
array([[0.38, 1. , 0.98, 0.69, 0.65], [0.69, 0.39, 0.14, 0.72, 0.53], [0.31, 0.49, 0.89, 0.93, 0.36], [0.57, 0.32, 0.59, 0.34, 0.39], [0.89, 0.23, 0.62, 0.08, 0.83]])
b > c
array([[ True, False, False, False, True], [ True, True, True, False, True], [ True, False, False, False, True], [False, True, False, False, True], [False, False, True, True, False]])
b[b > c] = 1
b
array([[1. , 0.27, 0.04, 0.02, 1. ], [1. , 1. , 1. , 0.54, 1. ], [1. , 0. , 0.86, 0.03, 1. ], [0.18, 1. , 0.54, 0.3 , 1. ], [0.03, 0.12, 1. , 1. , 0.62]])
b = np.zeros((10, 10))
b[[1, 2, 3], [4, 2, 0]] = 1
show(b)
x = np.arange(25).reshape(5, 5)
x
array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]])
np.sin(x)
array([[ 0. , 0.84, 0.91, 0.14, -0.76], [-0.96, -0.28, 0.66, 0.99, 0.41], [-0.54, -1. , -0.54, 0.42, 0.99], [ 0.65, -0.29, -0.96, -0.75, 0.15], [ 0.91, 0.84, -0.01, -0.85, -0.91]])
b = np.zeros((10, 10))
b[[1, 2, 4], 4:] = 1
show(b)
a = rng.integers(low=0, high=20, size=5)
a
array([ 8, 15, 6, 4, 15])
a.sum(), a.prod(), a.min(), a.max()
(48, 43200, 4, 15)
a = np.arange(9).reshape(3, 3)
a
array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
a.sum(), a.prod(), a.min(), a.max()
(36, 0, 0, 8)
a.sum(axis=0)
array([ 9, 12, 15])
a.sum(axis=1)
array([ 3, 12, 21])
a.min(axis=1)
array([0, 3, 6])
a.max(axis=0)
array([6, 7, 8])
a = rng.integers(0, 20, 5)
a
array([17, 1, 1, 13, 6])
np.sort(a)
array([ 1, 1, 6, 13, 17])
np.argsort(a)
array([1, 2, 4, 3, 0])
b = rng.random((10, 10))
show(b)
c = np.sort(b, axis=0)
show(c)
c = np.sort(b, axis=1)
show(c)
grades = np.zeros((8, 2), dtype=int)
grades[:, 0] = np.arange(5000, 5008)
grades[:, 1] = rng.integers(0, 100, 8)
grades
array([[5000, 95], [5001, 70], [5002, 63], [5003, 94], [5004, 78], [5005, 12], [5006, 3], [5007, 86]])
np.sort(grades, axis=0)
array([[5000, 3], [5001, 12], [5002, 63], [5003, 70], [5004, 78], [5005, 86], [5006, 94], [5007, 95]])
order = grades[:, 1].argsort()
order
array([6, 5, 2, 1, 4, 7, 3, 0])
grades[order]
array([[5006, 3], [5005, 12], [5002, 63], [5001, 70], [5004, 78], [5007, 86], [5003, 94], [5000, 95]])
a = np.arange(9).reshape(3, 3)
a
array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
a + 1
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
a
array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
b = np.array([10, 20, 30])
b
array([10, 20, 30])
a + b
array([[10, 21, 32], [13, 24, 35], [16, 27, 38]])
c = np.array([100, 200, 300]).reshape(3, 1)
c
array([[100], [200], [300]])
a + c
array([[100, 101, 102], [203, 204, 205], [306, 307, 308]])
b
array([10, 20, 30])
c
array([[100], [200], [300]])
c+b
array([[110, 120, 130], [210, 220, 230], [310, 320, 330]])