import numpy as np
print(np.__version__)
x = np.array([1,2,3,4,5])
print('x =', x)
print('x =', x)
y = x + 5
print('y =', y)
z = x * 2
print('z =', z)
A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(A)
C = A + 10
print(C)
print('')
D = A * 3
print(D)
A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
x = np.array([1, 2, 3])
print(A + x)
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)
A = np.random.rand(5,3)
print(A)
print('')
x = np.random.rand(5)
print(x)
# Will not work
#print(A + x)
x = np.random.rand(5)
print(x)
y = np.reshape(x,(5,1))
print(y)
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)
x = np.random.rand(3)
print(x)
y = np.random.rand(3)
print(y)
z = np.vstack((x, y))
print(z)
A = np.random.rand(5,3)
print(A.ndim)
print(A.shape)
print('')
x = np.random.rand(5)
print(x.ndim)
print(x.shape)
print(np.zeros((3,2)))
print('')
print(np.ones((2,3)))
print('')
print(np.eye(3))
A = np.arange(9)
print(A)
A = np.arange(9)
B = np.reshape(A, (3,3))
print(B)
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))
x = np.array([5,4,1,8,7,3,9,2,6])
print(np.mean(x))
print(np.var(x))
x = np.random.rand(3,3)
print(x)
print(x>0.5)
x = np.random.rand(3,3)
print(x)
mask = x > 0.5
print(x[mask])
x = np.random.rand(3,3)
mask = x > 0.5
x[mask] = 0
print(x)
r, c = np.where(x==0)
print(r, c)
import matplotlib
print(matplotlib.__version__)
import matplotlib.pyplot as plt
# A basic plot: A sinusoid
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.show()
# 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()
# 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)
# Supported image file types to save to
plt.gcf().canvas.get_supported_filetypes()
# 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()