from platform import python_version
print(python_version())
scalar = 5.678
print(scalar)
scalar_python = True
print(scalar_python)
print(type(scalar_python))
import numpy as np
vector = np.array([1, 2, 3, 4])
print(vector)
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matrix)
tensor = np.array([
[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[11, 12, 13], [14, 15, 16], [17, 18, 19]],
[[21, 22, 23], [24, 25, 26], [27, 28, 29]],
])
print(tensor)
%matplotlib inline
import matplotlib.pyplot as plt
plt.imshow(tensor, interpolation='nearest')
plt.show()
tensor = np.array([
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[128, 128, 128], [128, 128, 128], [128, 128, 128]],
[[255, 255, 255], [255, 255, 255], [255, 255, 255]],
])
plt.imshow(tensor, interpolation='nearest')
plt.show
#Pacman
b = [0, 0, 0] # black
w = [255, 255, 255] # white
y = [255, 255, 0] # yellow
pacman_tensor = np.array([
[w, w, w, w, w, w, w, w, w, w, w, w, w, w, w, w,],
[w, w, w, w, w, w, b, b, b, b, b, w, w, w, w, w,],
[w, w, w, w, b, b, y, y, y, y, y, b, b, w, w, w,],
[w, w, w, b, y, y, y, y, y, y, y, y, y, b, w, w,],
[w, w, b, y, y, y, y, y, y, y, y, y, y, y, b, w,],
[w, w, b, y, y, y, y, y, y, y, y, y, y, y, b, w,],
[w, b, y, y, y, y, y, y, y, y, y, y, b, b, w, w,],
[w, b, y, y, y, y, y, y, y, b, b, b, w, w, w, w,],
[w, b, y, y, y, y, b, b, b, w, w, w, w, w, w, w,],
[w, b, y, y, y, y, y, y, y, b, b, b, w, w, w, w,],
[w, b, y, y, y, y, y, y, y, y, y, y, b, b, w, w,],
[w, w, b, y, y, y, y, y, y, y, y, y, y, y, b, w,],
[w, w, b, y, y, y, y, y, y, y, y, y, y, y, b, w,],
[w, w, w, b, y, y, y, y, y, y, y, y, y, b, w, w,],
[w, w, w, w, b, b, y, y, y, y, y, b, b, w, w, w,],
[w, w, w, w, w, w, b, b, b, b, b, w, w, w, w, w,],
[w, w, w, w, w, w, w, w, w, w, w, w, w, w, w, w,],
])
plt.imshow(pacman_tensor, interpolation='nearest')
plt.show()
print(pacman_tensor.shape)
print(pacman_tensor.size)
# NOTE: This Pixel art was not made by me
# Credits: https://platzi.com/comentario/2663271/
G=[0,68,47]
R=[200,12,29]
W=[255,255,255]
B=[133,59,22]
LG=[101,108,38]
LB=[7,122,125]
O=[249,162,69]
tensorMexico=np.array([
# Parte de arriba
[G,G,G,G,G,G,G,G,G,W,W,W,W,W,W,W,W,W,R,R,R,R,R,R,R,R,R],
[G,G,G,G,G,G,G,G,G,W,W,W,W,W,W,W,W,W,R,R,R,R,R,R,R,R,R],
[G,G,G,G,G,G,G,G,G,W,W,W,W,W,W,W,W,W,R,R,R,R,R,R,R,R,R],
[G,G,G,G,G,G,G,G,G,W,W,W,W,W,W,W,W,W,R,R,R,R,R,R,R,R,R],
# Logo
[G,G,G,G,G,G,G,G,G,W,W,W,W,B,W,W,W,W,R,R,R,R,R,R,R,R,R],
[G,G,G,G,G,G,G,G,G,W,W,W,W,W,B,W,W,W,R,R,R,R,R,R,R,R,R],
[G,G,G,G,G,G,G,G,G,W,LG,B,B,W,B,B,W,W,R,R,R,R,R,R,R,R,R],
[G,G,G,G,G,G,G,G,G,W,W,LG,B,B,B,B,B,W,R,R,R,R,R,R,R,R,R],
[G,G,G,G,G,G,G,G,G,W,W,LG,W,B,B,B,B,W,R,R,R,R,R,R,R,R,R],
[G,G,G,G,G,G,G,G,G,W,W,W,B,B,B,B,W,W,R,R,R,R,R,R,R,R,R],
[G,G,G,G,G,G,G,G,G,W,LG,R,W,B,B,R,LG,W,R,R,R,R,R,R,R,R,R],
[G,G,G,G,G,G,G,G,G,W,LG,W,G,R,G,W,LG,W,R,R,R,R,R,R,R,R,R],
[G,G,G,G,G,G,G,G,G,W,W,LG,LB,O,LB,LG,W,W,R,R,R,R,R,R,R,R,R],
[G,G,G,G,G,G,G,G,G,W,W,W,LG,LG,LG,W,W,W,R,R,R,R,R,R,R,R,R],
# Parte de abajo
[G,G,G,G,G,G,G,G,G,W,W,W,W,W,W,W,W,W,R,R,R,R,R,R,R,R,R],
[G,G,G,G,G,G,G,G,G,W,W,W,W,W,W,W,W,W,R,R,R,R,R,R,R,R,R],
[G,G,G,G,G,G,G,G,G,W,W,W,W,W,W,W,W,W,R,R,R,R,R,R,R,R,R],
[G,G,G,G,G,G,G,G,G,W,W,W,W,W,W,W,W,W,R,R,R,R,R,R,R,R,R],
])
plt.imshow(tensorMexico, interpolation='nearest')
plt.show
import numpy as np
scalar = 5.679
vector = np.array([1, 2, 3])
matrix = np.array([[1, 2], [3, 4]])
tensor = np.array([
[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[11, 12, 13], [14, 15, 16], [17, 18, 19]],
[[21, 22, 23], [24, 25, 26], [27, 28, 29]],
])
print(scalar)
print(vector.shape)
print(len(vector))
print(matrix.shape)
print(len(matrix))
matrix.size
print(tensor.shape)
print(len(tensor))
import numpy as np
scalar = 5.679
vector = np.array([3, 4, 5, 6])
matrix = np.array([[1, 2], [3, 4], [5, 6]])
tensor = np.array([
[[1, 2, 3, 30], [4, 5, 6, 31], [7, 8, 9, 32]],
[[11, 12, 13, 33], [14, 15, 16, 34], [17, 18, 19, 35]],
[[21, 22, 23, 36], [24, 25, 26, 37], [27, 28, 29, 38]],
])
# scalar_t = scalar.T
vector_t = vector.T
vector_t
matrix_t = matrix.T
matrix_t
tensor_t = tensor.T
tensor_t
matrix_t_t = matrix_t.T
matrix_t_t
A = np.array([[1, 2], [3, 4], [5, 6]])
B = np.array([[6, 5], [4, 3], [2, 1]])
C = A + B
print(A, '\n')
print(B, '\n')
print(C, '\n')
D = matrix + scalar
D
# scalar sum by scalar with nested while loops
A = np.array([[1, 2], [3, 4], [5, 6]])
B = np.array([[6, 5], [4, 3], [2, 1]])
C = np.array([[0, 0], [0, 0], [0, 0]])
i = 0
while i < 3:
j = 0
while j < 2:
C[i][j] = A[i][j] + B[i][j]
j += 1
i += 1
print(C)
A = np.array([
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25]
])
A.shape
A_t = np.zeros(shape=(5, 5))
A_t
# Making the transpose
i = 0
while i < 5:
j = 0
while j < 5:
A_t[i][j] = A[j][i]
j += 1
i += 1
print(A_t)
# Adding the matrix and its transpose
B = np.zeros(shape = (5, 5))
i = 0
while i < 5:
j = 0
while j < 5:
B[i][j] = A[i][j] + A_t[i][j]
j += 1
i += 1
print(B)
scalar = 5.679
vector = np.array([3 , 4, 5])
matrix = np.array([[1, 2], [3, 4], [5, 6]])
A = matrix.T + vector
A
C = matrix + 42
C
D = matrix + np.array([[42], [42], [42]])
D
# First way
vector5 = np.array([23, 45, 54, 25, 74])
matrix5x5 = np.array([
[25, 64, 63, 27, 85],
[55, 67, 34, 36, 16],
[83, 47, 34, 37, 51],
[28, 54, 57, 72, 28],
[59, 17, 26, 92, 27],
])
adding = vector5 + matrix5x5
adding
# Second way
vector5 = np.arange(5)
matrix5x5 = np.arange(25).reshape(5,5)
adding = vector5 + matrix5x5
adding
# Third way
vector5 = np.random.randint(low=3, high=5, size=(1,5))
matrix5x5 = np.random.randint(low=1, high=9, size =(5,5))
adding = vector5 + matrix5x5
adding
scalar = 5.679
vector = np.array([2, 3])
matrix = np.array([[1, 2], [3, 4], [5, 6]])
A = matrix * vector
print(A)
# Internal product
B = matrix.dot(vector)
C = np.dot(matrix, vector)
print(B)
print(C)
A = np.array([[1,2,3], [4,5,6], [7,8,9], [10,11,12]])
B = np.array([[2,3], [5,7], [11,13]])
print(A.shape)
print(B.shape)
C = A.dot(B)
print(C)
A = np.array([[2,3], [5,7], [11,13]])
B = np.array([[1,3], [2,1]])
C = np.array([[3,1], [4,2]])
# Associative
ABC = A.dot(B.dot(C))
print(ABC)
AB_C = A.dot(B).dot(C)
AB_C
# Distributive
D = A.dot(B + C)
E = (A.dot(B)) + (A.dot(C))
print(D, '\n')
print(E, '\n')
print(D==E)
F = B.dot(C)
G = C.dot(B)
print(F==C)
print(F)
print(G)
v1 = np.array([[2], [7]])
v2 = np.array([[3], [5]])
v1_tv2 = v1.T.dot(v2)
v2_tv1 = v2.T.dot(v1)
print(v1_tv2)
print(v2_tv1)
import numpy as np
A = np.array([[2,3], [5,7], [11,13]])
B = np.array([[1,3], [2,1]])
print(A)
print(B)
AB_t = A.dot(B).T
B_tA_t = B.T.dot(A.T)
print(AB_t)
print(B_tA_t)
print(AB_t == B_tA_t)
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-5, 5)
y_1 = 3*x + 5
y_2 = 2*x + 3
plt.figure()
plt.plot(x, y_1)
plt.plot(x, y_2)
plt.xlim(-5, 5)
plt.ylim(-5, 5)
plt.axvline(x=0, color='grey')
plt.axhline(y=0, color='grey')
A = np.array([[-3, 1], [-2, 1]])
print(A)
b = np.array([[5], [3]])
print(b)
sol_1 = np.array([-2,-1])
print(sol_1)
print(A.dot(sol_1))
import numpy as np
identity = np.eye(4)
print(identity)
vector = np.array([[2], [3], [5], [7]])
print(vector)
print(identity.dot(vector))
A = np.array([[1,0,1], [0,1,1], [-1,1,1]])
print(A)
inverse_A = np.linalg.inv(A)
print(inverse_A)
print(A.dot(inverse_A))
singular = np.array([[1,1], [1,1]])
print(singular)
# print(np.linalg.inv(singular)) # <---- Error
import numpy as np
np.set_printoptions(suppress=True) # To round the numbers close to zero, to zero
A = np.array([[3,1], [2,1]])
print(A)
b = np.array([[1], [1]])
print(b)
inverse_A = np.linalg.inv(A)
print(inverse_A)
x = inverse_A.dot(b) # x = 0; y = 1
print(x)
print(A.dot(x)) # Checking the previous answer
sol_2 = inverse_A.dot(np.array([[3], [7]]))
print(sol_2)
print(A.dot(sol_2))
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-6, 6)
y_1 = 3*x + 5
y_2 = -1*x + 3
y_3 = 2*x + 1
plt.figure()
plt.plot(x, y_1)
plt.plot(x, y_2)
plt.plot(x, y_3)
plt.xlim(-8, 8)
plt.ylim(-8, 8)
plt.axvline(x=0, color='grey')
plt.axhline(y=0, color='grey')
plt.show()
x = np.arange(-6, 6)
y_2 = -1*x + 3
y_3 = 2*x + 1
plt.figure()
plt.plot(x, y_2)
plt.plot(x, y_3)
plt.xlim(-8, 8)
plt.ylim(-8, 8)
plt.axvline(x=0, color='grey')
plt.axhline(y=0, color='grey')
plt.show()
x = np.arange(-6, 6)
y_3 = 2*x + 1
plt.figure()
plt.plot(x, y_3)
plt.xlim(-8, 8)
plt.ylim(-8, 8)
plt.axvline(x=0, color='grey')
plt.axhline(y=0, color='grey')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
v1 = np.array([2,5])
v2 = np.array([3,2])
#def graph_vectors(vecs, cols, alpha=1):
# plt.figure()
# plt.axvline(x=0, color='grey', zorder=0)
# plt.axhline(y=0, color='grey', zorder=0)
# for i in range(len(vecs)):
# x = np.concatenate([[0,0], vecs[i]])
# plt.quiver(
# [x[0]],
# [x[1]],
# [x[2]],
# [x[3]],
# angles='xy', scale_units='xy', scale=1, color=cols[i], alpha=alpha)
%run '/work/graph-vectors.ipynb'
graph_vectors([v1, v2], ['orange', 'blue'])
plt.xlim(-1, 8)
plt.ylim(-1, 8)
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
%run '/work/graph-vectors.ipynb'
v1 = np.array([2,5])
v2 = np.array([3,2])
v1v2 = (2*v1) + (v2)
print(v1v2)
graph_vectors([v1, v2, v1v2], ['orange', 'blue', 'red'])
plt.xlim(-1, 8)
plt.ylim(-1, 13)
for a in range(-10, 10):
for b in range(-10, 10):
plt.scatter((v1[0]*a) + (v2[0]*b), (v1[1]*a) + (v2[1]*b),
marker='.',
color='orange')
plt.xlim(-100, 100)
plt.ylim(-100, 100)
plt.axvline(x=0, color='grey')
plt.axhline(y=0, color='grey')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
%run '/work/graph-vectors.ipynb'
v1 = np.array([1,1])
v2 = np.array([-1,-1])
linear_combinations(v1, v2)
v1 = np.array([1, 0])
v2 = np.array([2, -3])
linear_combinations(v1, v2)
from mpl_toolkits.mplot3d import Axes3D
v1 = np.array([1, 0, 0])
v2 = np.array([2, -3, 0])
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for a in range(-10, 10):
for b in range(-10, 10):
ax.scatter((v1[0]*a) + (v2[0]*b), (v1[1]*a) + (v2[1]*b),
(v1[2]*a) + (v2[2]*b),
marker='.',
color='orange')
ax.set_xlabel('Eje X')
ax.set_ylabel('Eje Y')
ax.set_zlabel('Eje Z')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
v1 = np.array([1,1])
v2 = np.array([-1, -1])
print(v1)
print(v2)
for a in range(-10, 10):
for b in range(-10, 10):
plt.scatter((v1[0]*a) + (v2[0]*b), (v1[1]*a) + (v2[1]*b),
marker='.',
color = 'orange')
plt.xlim(-25, 25)
plt.ylim(-25, 25)
plt.axvline(x=0, color='grey')
plt.axhline(y=0, color='grey')
plt.show()
import numpy as np
A = np.array([
[0,1,0,0],
[0,0,1,0],
[0,1,1,0],
[1,0,0,1]
])
lambdas, V = np.linalg.eig(A.T)
print(A[lambdas == 0, :])
#np.linalg.inv(A)
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
v1 = np.array([2,7])
v2 = np.array([3,5])
print(v1, v2)
v1v2 = v1 + v2
print(v1v2)
np.linalg.norm(v1v2)
# norm (v1v2) <= norm(v1) + norm(v2)
norm_v1 = np.linalg.norm(v1)
norm_v2 = np.linalg.norm(v2)
norm_v1v2 = np.linalg.norm(v1v2)
print(norm_v1v2 <= (norm_v1 + norm_v2))
print(norm_v1 + norm_v2)
v1 = np.array([0,0,2,7])
v2 = np.array([0,0,3,5])
v1_aux = np.array([v1[2], v1[3], v2[2], v2[3]])
v1v2 = np.array([0,0,5,12])
plt.quiver(
[v1[0], v1_aux[0], v1v2[0]],
[v1[1], v1_aux[1], v1v2[1]],
[v1[2], v1_aux[2], v1v2[2]],
[v1[3], v1_aux[3], v1v2[3]],
angles='xy', scale_units='xy', scale=1,
color=sns.color_palette()
)
plt.xlim(-0.5, 6)
plt.ylim(-0.5, 15)
plt.show()
import numpy as np
# Calculating norm 0
vector = np.array([1,2,0,5,6,0])
print(np.linalg.norm(vector, ord=0))
# Calculating norm 1
vector = np.array([1, -1, 1, -1, 1])
print(np.linalg.norm(vector, ord=1))
# Calculating norm 2
vector = np.array([1,1]) # This would be: np.sqrt(pow(1, 2) + pow(1, 2))
print(np.linalg.norm(vector, ord=2))
# Norm L2 vs L2^2
vector = np.array([1,2,3,4,5,6])
print(vector)
print(np.linalg.norm(vector, ord=2)**2)
# Calculating norm 1 (L2^2)
print(vector.T.dot(vector))
# Calculating the infinite norm
vector = np.array([1,2,3,-100])
print(np.linalg.norm(vector, ord=np.inf))
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
v1 = np.array([0,0,0,3])
v2 = np.array([0,0,3,3])
plt.xlim(-2,6)
plt.ylim(-2,6)
plt.quiver(
[v1[0], v2[0]],
[v1[1], v2[1]],
[v1[2], v2[2]],
[v1[3], v2[3]],
angles='xy', scale_units='xy', scale=1, color=sns.color_palette())
plt.show()
v1 = np.array([0,3])
v2 = np.array([3,3])
print(v1.T.dot(v2))
norm_v1 = np.linalg.norm(v1, ord=2)
norm_v2 = np.linalg.norm(v2, ord=2)
print(norm_v1 * norm_v2 * np.cos(np.deg2rad(45))) # There is a 45° angle between our 2 vectors, so we have to switch rad to degrees with np.deg2rad()
import numpy as np
vector = np.array([1,2,3,4,5])
matrix = np.diag(vector)
print(matrix)
print(matrix[0:4, 0:3])
print(matrix[0:3, 0:4])
A = np.diag([2,3,4,5])
print(A)
v1 = np.array([1,1,1,1])
print(v1)
print(A.dot(v1.T))
A_inv = np.diag([1/2, 1/3, 1/4, 1/5])
print(A_inv)
identity = A.dot(A_inv)
print(identity)
A_inv_calc = np.linalg.inv(A)
print(A_inv_calc)
# Simetric = A * A.T
print(A)
print(A.T)
simetric = np.array([
[1,2,3],
[2,-1,7],
[3,7,11]
])
print(simetric)
print(simetric.T)
# (A*B).T = B*A
import numpy as np
import matplotlib.pyplot as plt
x = np.array([0,0,2,2])
y = np.array([0,0,2,-2])
plt.quiver(
[x[0], y[0]],
[x[1], y[1]],
[x[2], y[2]],
[x[3], y[3]],
angles='xy', scale_units='xy', scale=1)
plt.xlim(-3.5,5.5)
plt.ylim(-3,3)
plt.show()
v1 = np.array([2,2])
v2 = np.array([2,-2])
print(v1, v2)
print(v1.dot(v2.T))
print(np.linalg.norm(v1))
print(np.linalg.norm(v2))
v1 = np.array([1,0])
v2 = np.array([0,-1])
print(v1.dot(v2.T))
print(np.linalg.norm(v1))
print(np.linalg.norm(v2))
import numpy as np
matrix = np.eye(3)
print(matrix)
print(matrix[:,0].dot(matrix[:,1]))
print(matrix[:,0].dot(matrix[:,2]))
print(matrix[:,1].dot(matrix[:,2]))
print(np.linalg.norm(matrix[:,0]))
print(np.linalg.norm(matrix[:,1]))
print(np.linalg.norm(matrix[:,2]))
print(matrix[0,:].dot(matrix[1,:]))
print(matrix[0,:].dot(matrix[2,:]))
print(matrix[1,:].dot(matrix[2,:]))
print(np.linalg.norm(matrix[0,:]))
print(np.linalg.norm(matrix[1,:]))
print(np.linalg.norm(matrix[2:,2]))
A = np.array([
[np.cos(100), -np.sin(100)],
[np.sin(100), np.cos(100)]])
print(A)
print(np.linalg.norm(A[0,:]))
print(np.linalg.norm(A[1,:]))
print(np.linalg.norm(A[:,0]))
print(np.linalg.norm(A[:,1]))
print(A[0,:].dot(A[1,:]))
print(A[:,0].dot(A[:,1]))
A_t = A.T
print(A_t.dot(A))
print(A.dot(A_t))
A_inv = np.linalg.inv(A)
print(A_inv)
print(A_t)
print(1/A_t.dot(A))
%run '/work/graph-vectors.ipynb'
import numpy as np
import matplotlib.pyplot as plt
matrix = np.array([[1,2,3], [4,5,6], [7,8,9]])
print(matrix)
trace = np.trace(matrix)
print(trace)
v1 = np.array([0,1])
v2 = np.array([1,0])
graph_vectors([v1, v2], ['blue', 'red'])
plt.xlim(-0.25, 2)
plt.ylim(-0.25, 2)
plt.show()
A = np.array([[2,0], [0,2]])
print(A)
v1_turned = A.dot(v1)
v2_turned = A.dot(v2)
graph_vectors([v1_turned, v2_turned], ['orange', 'green'])
plt.xlim(-0.25, 3)
plt.ylim(-0.25, 3)
plt.show()
det_A = np.linalg.det(A)
print(det_A)
area_turned = abs(v1_turned[0] - v2_turned[0]) * abs(v1_turned[1] - v2_turned[1])
print(area_turned)
B = A * [-1, 1]
print(B)
det_B = np.linalg.det(B)
print(det_B)
v1 = np.array([0,1])
v2 = np.array([1,0])
v1_turned = B.dot(v1)
v2_turned = B.dot(v2)
graph_vectors([v1_turned, v2_turned, v1, v2], ['blue', 'red', 'orange', 'green'])
plt.xlim(-3, 1.5)
plt.ylim(-0.5, 2.5)
plt.show()
print(np.linalg.norm([1/2,1/2,1/2,1/2], ord=2))