import numpy as np
np.set_printoptions(precision=4, suppress=True)
a = np.array([-1, 3])
b = np.array([3, 5])
a+b
array([2, 8])
a-b
array([-4, -2])
2*np.array([2, 3])
array([4, 6])
a = np.array([3, 5, 7])
a.T
array([3, 5, 7])
v1 = np.array([2, -1, 6])
v2 = np.array([3, 2, 1])
np.dot(v1, v2)
10
v1 = np.array([2, -1, 4, -3, 8])
np.around(np.linalg.norm(v1), 4)
9.6954
va = np.array([4, 1, 3, 2])
vb = np.array([-3, 1, -2])
np.outer(va, vb)
array([[-12, 4, -8], [ -3, 1, -2], [ -9, 3, -6], [ -6, 2, -4]])
v = np.array([3, 4])
v/np.linalg.norm(v)
array([0.6, 0.8])
A = np.array([[3, 9, -1, 2], [8, 2,0, -7], [-4, 7, 9, 2]])
A.T
array([[ 3, 8, -4], [ 9, 2, 7], [-1, 0, 9], [ 2, -7, 2]])
v = np.array([1, -1, 3])
np.diag(v)
array([[ 1, 0, 0], [ 0, -1, 0], [ 0, 0, 3]])
A = np.array([[3, 9, -1, 2], [8, 2, 0, -7], [-4, 7, 9, 2]])
B = np.array([[-1, 0, 2, 4], [-3, 1, 0, 6], [7, 0, -3, 4]])
A+B
array([[ 2, 9, 1, 6], [ 5, 3, 0, -1], [ 3, 7, 6, 6]])
A-B
array([[ 4, 9, -3, -2], [ 11, 1, 0, -13], [-11, 7, 12, -2]])
A = np.array([[3, 9], [-1, 2], [8, 2], [0, -7]])
B = np.array([[2, 3], [-1, 4]])
np.dot(A, B)
array([[ -3, 45], [ -4, 5], [ 14, 32], [ 7, -28]])
a = np.array([[2, 1, -2], [1, -3, 1], [4, -1, -2]]) ### Coefficients
b = np.array([-3, 8, 3]) ### Constants
np.linalg.solve(a, b)
array([ 2., -1., 3.])
a = np.array([[1, -2, 3], [-1, 3, 0], [2, -5, 5]]) ### Coefficients
b = np.array([9, -4, 17]) ### Constants
np.linalg.solve(a, b)
array([ 1., -1., 2.])
a = np.array([[1, -1, 2], [1, 0, 1], [2, -3, 5]]) ### Coefficients
b = np.array([4, 6, 4]) ### Constants
try:
np.linalg.solve(a, b) ### No solution - will result in Singular Matrix error
except np.linalg.LinAlgError as le:
print('ERROR:', le)
ERROR: Singular matrix
A = np.array([[1, 4], [-1, -3]])
np.linalg.inv(A)
array([[-3., -4.], [ 1., 1.]])
A = np.array([[1, -1, 0], [1, 0, -1], [-6, 2, 3]])
np.linalg.inv(A)
array([[-2., -3., -1.], [-3., -3., -1.], [-2., -4., -1.]])
A = np.array([[0, 2, 1], [3, -1, 2], [4, 0, 1]])
np.around(np.linalg.det(A), 4)
14.0
A = np.array([[2, 2], [5, -1]])
L, V = np.linalg.eig(A)
L, V
(array([ 4., -3.]), array([[ 0.7071, -0.3714], [ 0.7071, 0.9285]]))
A = np.array([[-2, -4, 2], [-2, 1, 2], [4, 2, 5]])
L, V = np.linalg.eig(A)
L, V
(array([-5., 3., 6.]), array([[ 0.8165, 0.5345, 0.0584], [ 0.4082, -0.8018, 0.3505], [-0.4082, -0.2673, 0.9347]]))
A = np.array([[1, 3, 2], [2, 6, 0], [4, 12, 1]])
np.linalg.matrix_rank(A)
2
A = np.array([[1, -1, 3], [3, 1, 1]])
U, S, VT = np.linalg.svd(A)
U, S, VT
(array([[-0.7071, -0.7071], [-0.7071, 0.7071]]), array([4. , 2.4495]), array([[-0.7071, 0. , -0.7071], [ 0.5774, 0.5774, -0.5774], [-0.4082, 0.8165, 0.4082]]))
import matplotlib.pyplot as plt
from PIL import Image
img = Image.open('tomcat.jpg')
plt.title('Original Image')
plt.imshow(img)
plt.show()
img = img.convert('LA')
img.size
(1260, 852)
IMG = np.array(list(img.getdata(band=0)), float)
IMG.resize(img.size[1], img.size[0])
IMG.size, IMG.shape
(1073520, (852, 1260))
plt.imshow(IMG, cmap='gray')
<matplotlib.image.AxesImage at 0x7f4b03d26d00>
U, s, VT = np.linalg.svd(IMG, full_matrices=False)
U.shape, s.shape, VT.shape
((852, 852), (852,), (852, 1260))
S = np.diag(s)
r = 16
UR = U[:, :r]
SR = S[0:r, :r]
VTR = VT[:r, :]
UR.shape, SR.shape, VTR.shape
((852, 16), (16, 16), (16, 1260))
NEW_IMG = np.matrix(UR) * SR * np.matrix(VTR)
plt.title('Rank = 16')
plt.imshow(NEW_IMG, cmap='gray')
plt.show()
r = 32
UR = U[:, :r]
SR = S[0:r, :r]
VTR = VT[:r, :]
UR.shape, SR.shape, VTR.shape
((852, 32), (32, 32), (32, 1260))
NEW_IMG = np.matrix(UR) * SR * np.matrix(VTR)
plt.title('Rank = 32')
plt.imshow(NEW_IMG, cmap='gray')
plt.show()
r = 64
UR = U[:, :r]
SR = S[0:r, :r]
VTR = VT[:r, :]
UR.shape, SR.shape, VTR.shape
((852, 64), (64, 64), (64, 1260))
NEW_IMG = np.matrix(UR) * SR * np.matrix(VTR)
plt.title('Rank = 64')
plt.imshow(NEW_IMG, cmap='gray')
plt.show()
actual_sz = img.size[0] * img.size[1]
new_sz = 64 * img.size[0] + 64 + 64 * img.size[1]
print('Image compression %0.2f percent' % (100 - new_sz/actual_sz * 100))
Image compression 87.40 percent