In [1]:
import numpy as np
In [2]:
print(np.__version__)
1.18.0
In [3]:
x = np.array([1,2,3,4,5])

print('x =', x)
x = [1 2 3 4 5]
In [4]:
print('x =', x)

y = x + 5
print('y =', y)

z = x * 2
print('z =', z)
x = [1 2 3 4 5]
y = [ 6  7  8  9 10]
z = [ 2  4  6  8 10]
In [5]:
A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

print(A)
[[1 2 3]
 [4 5 6]
 [7 8 9]]
In [6]:
C = A + 10
print(C)

print('')

D = A * 3
print(D)
[[11 12 13]
 [14 15 16]
 [17 18 19]]

[[ 3  6  9]
 [12 15 18]
 [21 24 27]]
In [7]:
A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

x = np.array([1, 2, 3])

print(A + x)
[[ 2  4  6]
 [ 5  7  9]
 [ 8 10 12]]
In [8]:
x = np.array([1, 2, 3])

y = np.array([1, 2, 3])

inner = np.dot(x, y)

print(inner)

outer = np.outer(x,y)

print(outer)
14
[[1 2 3]
 [2 4 6]
 [3 6 9]]
In [9]:
A = np.random.rand(5,3)
print(A)

print('')

x = np.random.rand(5)
print(x)

# Will not work
#print(A + x)
[[0.26439691 0.33976199 0.89112435]
 [0.12607841 0.32759399 0.09814409]
 [0.070014   0.41831987 0.08510287]
 [0.78511167 0.65547192 0.37613667]
 [0.68505677 0.56913773 0.77395354]]

[0.87144544 0.51154527 0.75554258 0.28429846 0.82750664]
In [10]:
x = np.random.rand(5)
print(x)
y = np.reshape(x,(5,1))
print(y)
[0.19459997 0.17137523 0.72216018 0.3795981  0.62270815]
[[0.19459997]
 [0.17137523]
 [0.72216018]
 [0.3795981 ]
 [0.62270815]]
In [11]:
A = np.random.rand(5,3)
print(A)

print('')

x = np.random.rand(5)
print(x)

# Will not work
#print(A + x)

y = np.reshape(x,(5,1))
print(y)

print(A + y)
[[0.57183073 0.22634576 0.55533222]
 [0.2781263  0.0830806  0.49770606]
 [0.65425905 0.5366975  0.94329437]
 [0.25494422 0.52894995 0.2594962 ]
 [0.43281698 0.02313577 0.27809515]]

[0.99700076 0.28544275 0.62999351 0.71035145 0.03682954]
[[0.99700076]
 [0.28544275]
 [0.62999351]
 [0.71035145]
 [0.03682954]]
[[1.56883148 1.22334652 1.55233298]
 [0.56356904 0.36852335 0.78314881]
 [1.28425256 1.16669101 1.57328788]
 [0.96529567 1.2393014  0.96984764]
 [0.46964652 0.0599653  0.31492469]]
In [12]:
x = np.random.rand(3)
print(x)
y = np.random.rand(3)
print(y)

z = np.vstack((x, y))
print(z)
[0.23542241 0.28288298 0.85617956]
[0.81968958 0.23574895 0.04755549]
[[0.23542241 0.28288298 0.85617956]
 [0.81968958 0.23574895 0.04755549]]
In [13]:
A = np.random.rand(5,3)
print(A.ndim)
print(A.shape)

print('')

x = np.random.rand(5)
print(x.ndim)
print(x.shape)
2
(5, 3)

1
(5,)
In [14]:
print(np.zeros((3,2)))

print('')

print(np.ones((2,3)))

print('')

print(np.eye(3))
[[0. 0.]
 [0. 0.]
 [0. 0.]]

[[1. 1. 1.]
 [1. 1. 1.]]

[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
In [15]:
A = np.arange(9)

print(A)
[0 1 2 3 4 5 6 7 8]
In [16]:
A = np.arange(9)
B = np.reshape(A, (3,3))

print(B)
[[0 1 2]
 [3 4 5]
 [6 7 8]]
In [17]:
x = np.array([5,4,1,8,7,3,9,2,6])

print(np.max(x))
print(np.argmax(x))

print('')
print(np.min(x))
print(np.argmin(x))

print('')
print(np.sort(x))
print(np.argsort(x))
9
6

1
2

[1 2 3 4 5 6 7 8 9]
[2 7 5 1 0 8 4 3 6]
In [18]:
x = np.array([5,4,1,8,7,3,9,2,6])

print(np.mean(x))
print(np.var(x))
5.0
6.666666666666667
In [19]:
x = np.random.rand(3,3)
print(x)

print(x>0.5)
[[0.07196709 0.01201142 0.44603067]
 [0.95398169 0.19906105 0.15415054]
 [0.66052142 0.61432222 0.90650095]]
[[False False False]
 [ True False False]
 [ True  True  True]]
In [20]:
x = np.random.rand(3,3)
print(x)

mask = x > 0.5
print(x[mask])
[[0.87018748 0.26194116 0.51467681]
 [0.87026777 0.21424564 0.52837798]
 [0.98104726 0.69100863 0.34998356]]
[0.87018748 0.51467681 0.87026777 0.52837798 0.98104726 0.69100863]
In [21]:
x = np.random.rand(3,3)

mask = x > 0.5
x[mask] = 0
print(x)

r, c = np.where(x==0)
print(r, c)
[[0.         0.26595968 0.        ]
 [0.         0.19755343 0.        ]
 [0.47915742 0.48579825 0.3557907 ]]
[0 0 1 1] [0 2 0 2]


In [22]:
import matplotlib

print(matplotlib.__version__)
3.1.1
In [23]:
import matplotlib.pyplot as plt
In [24]:
# A basic plot: A sinusoid

x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.show()
In [25]:
# Customizing the plot

x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.figure(figsize=(11,6), dpi=200)
[line_handle] = plt.plot(x, y, c='g', linewidth=4, label='sine')
y2 = np.cos(x)
[line_handle2] = plt.plot(x, y2, c='#ffadcd', linewidth=2, label='cos')
plt.legend(handles=[line_handle, line_handle2], fontsize=30, loc='lower right', shadow=True)
plt.xlabel('x axis', fontsize=30)
plt.ylabel('y axis', fontsize=30)
plt.title('A plot', fontsize=30)
plt.xticks(fontsize=20)
plt.yticks([-1, -0.5, 0, 0.5, 1], [-1, -0.5, 0, 0.5, 1], fontsize=20)
plt.show()
In [26]:
# Scatter Plots with customized legends

x1 = np.random.normal(loc=[0,0], scale=1, size=(100,2))
x2 = np.random.normal(loc=[10,0], scale=1, size=(100,2))
x3 = np.random.normal(loc=[5,5], scale=1, size=(100,2))

plt.figure(figsize=(9,6), dpi=150)

g1 = plt.scatter(
    x1[:,0], x1[:,1], marker='x', s=80, c='b',
    label='Gaussian #1'
)
g2 = plt.scatter(
    x2[:,0], x2[:,1], marker='o', s=80, c='w', edgecolor='r',
    label='Gaussian #2'
)
g3 = plt.scatter(
    x3[:,0], x3[:,1], marker='d', s=80, c='w', edgecolor='g',
    label='Gaussian #3'
)

plt.legend(
    handles=[g1, g2, g3], fontsize=30, shadow=True, 
    bbox_to_anchor=(1.03, 1), borderaxespad=0
)

plt.xlabel('Feature 1', fontsize=30)
plt.ylabel('Feature 2', fontsize=30)
plt.title('Mixture of 3 Gaussians', fontsize=30)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.show()

#plt.savefig('scatter_demo.png', dpi=300, bbox_inches='tight', pad_inches=0.05)
In [27]:
# Supported image file types to save to
plt.gcf().canvas.get_supported_filetypes()
Out[27]:
{'ps': 'Postscript',
 'eps': 'Encapsulated Postscript',
 'pdf': 'Portable Document Format',
 'pgf': 'PGF code for LaTeX',
 'png': 'Portable Network Graphics',
 'raw': 'Raw RGBA bitmap',
 'rgba': 'Raw RGBA bitmap',
 'svg': 'Scalable Vector Graphics',
 'svgz': 'Scalable Vector Graphics',
 'jpg': 'Joint Photographic Experts Group',
 'jpeg': 'Joint Photographic Experts Group',
 'tif': 'Tagged Image File Format',
 'tiff': 'Tagged Image File Format'}
<Figure size 432x288 with 0 Axes>
In [28]:
# Plotting in 3D

%matplotlib notebook

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

n = 100
xs = np.random.rand(n)
ys = np.random.rand(n)
zs = np.random.rand(n)
mask = zs < 0.5
c = ['r', 'b']
m = ['o', '^']
ax.scatter(xs[mask==0], ys[mask==0], zs[mask==0], c=c[0], marker=m[0])
ax.scatter(xs[mask==1], ys[mask==1], zs[mask==1], c=c[1], marker=m[1])


ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()