!pip install --upgrade pip
!pip install scipy pillow imageio matplotlib seaborn scikit-learn;
import seaborn as sns
vuelos = sns.load_dataset("flights")
vuelos = vuelos.pivot("month", "year", "passengers")
ax = sns.heatmap(vuelos)
escalar = 5.678
print(escalar)
escalar_python = True
print(escalar_python)
print(type(escalar_python))
import numpy as np
vector = np.array([1,2,3,4])
print(vector)
matriz = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(matriz)
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()
escalar = 5.678
vector = np.array([1,2,3])
matriz = np.array([[1,2,3],[4,5,6],[7,8,9]])
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(
'\nContenido del escalar:\n',escalar,
)
print(
'\nContenido del vector:\n',vector,
'\nDimensiones del vector:\n',vector.shape,
'\nLargo de la primera columna el vector:\n',len(vector),
)
print(
'\nContenido de la matriz:\n',matriz,
'\nDimensiones de la matriz:\n',matriz.shape,
'\nLargo de la primera columna de la matriz:\n',len(matriz),
'\nNumero de contenido dentro de la matriz:\n',matriz.size,
)
print(
'\nContenido de la tensor:\n',tensor,
'\nDimensiones de la tensor:\n',tensor.shape,
'\nLargo de la primera columna de la tensor:\n',len(tensor),
'\nNumero de contenido dentro de la tensor:\n',tensor.size,
)
import numpy as np
escalar = np.array(5.678)
vector = np.array([1,2,3])
matriz = np.array([[1,2,3],[4,5,6],[7,8,9]])
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]],
])
escalar_t = escalar.T
print(escalar_t)
vector_t = vector.T
print(vector_t)
vector_t = vector_t.reshape(-1,1)
print(vector_t)
matriz_t = matriz.T
print(
'Matriz A:',matriz,'',
'Matriz A^t:',matriz_t,
sep='\n')
tensor_t = tensor.T
print('Tensor A^t:\n',tensor_t)
print('Tensor A:\n',tensor)
A = matriz[0:2]
B = matriz_t[0:3,0:2]
print(f'''
Matriz A:\n{A}
Matriz B:\n{B}
''')
C = A+B
C
A = np.array([[1,2],[3,4],[5,6]])
B = np.array([[6,5],[4,3],[2,1]])
print(f'''
Matriz A: \n{A}
Matriz B:\n{B}
''')
C = A+B
print(C)
import numpy as np
escalar = 5.679
vector= np.array([3,4,5])
matriz = np.array([[1,2],[3,4],[5,6]])
A = vector + matriz
A = vector + matriz.T
print(A)
import numpy as np
escalar = 5.679
vector = np.array([2,3])
matriz = np.array([[1,2],[3,4],[5,6]])
A = matriz * vector
print(A)
B = matriz.dot(vector)
print(B)
import numpy as np
A = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
B = np.array([[1, 2], [3, 4], [5, 6]])
C= A.dot(B)
print(C)
print(A.shape,B.shape,sep='\n')
D= B.dot(A)
import numpy as np
A = np.array([[2,3],[5,7],[11,13]])
B = np.array([[1,3],[2,1]])
C = np.array([[3,1],[4,2]])
# Asociativa
ABC = A.dot(B.dot(C))
print(ABC)
AB_C = A.dot(B).dot(C)
print(AB_C)
# Distributiva
D = A.dot(B+C)
E = (A.dot(B))+(A.dot(C))
print(D,E,D==E,sep='\n\n')
# Conmutativa
F = B.dot(C)
G = C.dot(B)
print(F,G,F==G,sep='\n\n')
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,v2_tv1,sep='\n\n')
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(f'''
AB_T\n{AB_T}
B_tA_t\n{B_tA_t}
AB_T==B_tA_t\n{AB_T==B_tA_t}''')
%matplotlib inline
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.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])
A.dot(sol_1)
import numpy as np
identidad = np.eye(4)
print(identidad)
vector = np.array([[2],[3],[5],[7]])
print(identidad.dot(vector))
print(vector)
A = np.array([[1,0,1],[0,1,1],[-1,1,1]])
print(A)
inversa_A = np.linalg.inv(A)
print(inversa_A)
print(A.dot(inversa_A))
singular = np.array([[1,1],[1,1]])
print(singular)
print(np.linalg.det(singular))
print(np.linalg.inv(singular))
import numpy as np
# These options determine the way floating point numbers, arrays and other NumPy objects are displayed.
np.set_printoptions(suppress=True)
A = np.array([[3,1],[2,1]])
print(A)
B = np.array([[1],[1]])
print(B)
inversa_A = np.linalg.inv(A)
print(inversa_A)
x = inversa_A.dot(B)
print(x)
print(A.dot(x))
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-6,6)
y_1 = 3*x+5
y_2 = -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()
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
v1 = np.array([2,5])
v2 = np.array([3,2])
def graficar_vectores( vecs, cols, alpha=1):
plt.figure()
plt.axvline(x=0, color='gray', zorder = 0)
plt.axhline(y=0, color='gray', 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 'utils/aux.py'
graficar_vectores([v1,v2], ['orange', 'blue'])
plt.xlim(-1,8)
plt.ylim(-1,8)
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
%run "utils/aux.py"
v1 = np.array([2,5])
v2 = np.array([3,2])
v1v2 = 2*v1+3*v2
print(v1v2)
graficar_vectores([v1,v2,v1v2], ['orange','blue','red'])
plt.xlim(-1,18)
plt.ylim(-1,18)
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='red')
plt.xlim(-100,100)
plt.ylim(-100,100)
plt.axvline(x=0,color='black')
plt.axhline(y=0,color='black')
import numpy as np
import matplotlib.pyplot as plt
v1 = np.array([1,1])
v2 = np.array([-1,-1])
# R1
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='black')
plt.axhline(y=0,color='black')
v1 = np.array([1,0])
v2 = np.array([2,-3])
v3 = np.array([3,6])
# R2
for a in range(-10,10):
for b in range(-10,10):
plt.scatter(v1[0]*a + v2[0]*b + v3, v1[1]*a + v2[1]*b + v3,
marker = '.',
color='orange')
plt.xlim(-25,25)
plt.ylim(-25,25)
plt.axvline(x=0,color='black')
plt.axhline(y=0,color='black')
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
v1 = np.array([1,1])
v2 = np.array([-1,-1])
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()
print(v1 == -1 * v2)
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)
%matplotlib inline
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)
print(v2)
v1v2 = v1+v2
print(v1v2)
np.linalg.norm(v1v2)
# norma(v1v2) <= norma (v1) + norma (v2)
norma_v1 = np.linalg.norm(v1)
norma_v2 = np.linalg.norm(v2)
norma_v1v2 = np.linalg.norm(v1v2)
print(norma_v1v2 <= (norma_v1 + norma_v2))
print(norma_v1v2 , (norma_v1 + norma_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 = v1+v2
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)
import numpy as np
#L0
vector = np.array([1,2,0,5,6,0])
print(np.linalg.norm(vector, ord=0))
#L1
vector = np.array([1,-1,1,-1,1])
print(np.linalg.norm(vector, ord=1))
#L2
vector = np.array([1,1])
print(np.linalg.norm(vector, ord=2))
#L2**2
vector = np.array([1,2,3,4,5,6])
print(np.linalg.norm(vector, ord=2)**2)
print(vector.T.dot(vector))
#L-infinito
vector = np.array([1,2,3,-100])
print(np.linalg.norm(vector, ord=np.Inf))
#L2**2
vector = np.array([1,2,3,4,5,6])
print(np.linalg.norm(vector, ord=2)**2)
print(vector.T.dot(vector))
%matplotlib inline
import numpy as np
import matplotlib.pylab as plt
import seaborn as sns
v1 = ([0,0,0,3])
v2 = ([0,0,3,3])
plt.xlim(-2,6)
plt.ylim(-2,6)
a = 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()
# Producto interno de una norma
v1 = np.array([0,3])
v2 = np.array([3,3])
print(v1.T.dot(v2))
# Forma 2
v1 = np.array([0,3])
v2 = np.array([3,3])
# determinamos el valor de ATxB
v1T_v2 = v1.T.dot(v2)
#determinamos el valor de la segunda norma para ambos vectores por separado, para luego multiplicar
v1_L2 = np.linalg.norm(v1, ord=2)
v2_L2 = np.linalg.norm(v2, ord=2)
V1L2xV2L2 = v1_L2 * v2_L2
#modelamos nuestras funciones con la base matemática que determinamos más arriba, integrando lo que llevamos
cos_alpha = v1T_v2 / V1L2xV2L2
#añadimos la función arccos para obtener el ángulo(en radianes)
alpha_rad = np.arccos(cos_alpha)
#pasamos radianes a grados
alpha = np.rad2deg(alpha_rad)
# alpha = np.rad2deg(np.arccos(v1T_v2 / (np.linalg.norm(v1, ord=2) * np.linalg.norm(v2, ord=2))))
print(alpha)
norma_v1 = np.linalg.norm(v1)
norma_v2 = np.linalg.norm(v2)
print(norma_v1 * norma_v2 * np.cos(np.deg2rad(alpha)))
import numpy as np
vector = np.array([1,2,3,4,5])
matriz = np.diag(vector)
print(matriz)
A = np.diag([2,3,4,5])
print(A)
v1 = np.array( [ [1,1,1,2] ])
print(v1.T)
print(A.dot(v1.T))
A_inversa = np.diag([1/2,1/3,1/4,1/5])
print(A_inversa)
matriz_identidad = A.dot(A_inversa)
A_inv_calc = np.linalg.inv(A)
print(A_inv_calc)
simetrica = np.array(
[
[8,1,3],
[1,7,4],
[3,4,9]
])
print(simetrica,simetrica.T,sep='\n\n')
%matplotlib inline
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(-2,4)
plt.ylim(-3,3)
v1=np.array([[2,2]])
v2=np.array([[2,-2]])
print(v1)
print(v2)
print(v1.dot(v2.T))
v1=np.array([[2,2]])
v2=np.array([[2,-2]])
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
matriz = np.array([
[1,0,0],
[0,1,0],
[0,0,1],
])
print(matriz)
# Producto escalar columnas
print(matriz[:,0].dot(matriz[:,1]))
print(matriz[:,1].dot(matriz[:,2]))
print(matriz[:,2].dot(matriz[:,0]))
# Norma del vector columnas
print(np.linalg.norm(matriz[:,0]))
print(np.linalg.norm(matriz[:,1]))
print(np.linalg.norm(matriz[:,2]))
# Producto escalar filas
print(matriz[0,:].dot(matriz[1,:]))
print(matriz[1,:].dot(matriz[2,:]))
print(matriz[2,:].dot(matriz[0,:]))
# Norma del vector filas
print(np.linalg.norm(matriz[0,:]))
print(np.linalg.norm(matriz[1,:]))
print(np.linalg.norm(matriz[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_inversa = np.linalg.inv(A)
print(A_inversa)
print(A_t)
print(1/A_t.dot(A))
import numpy as np
matriz = np.array([
[1,2,3],
[4,5,6],
[7,8,9]]
)
print(matriz)
traza = np.trace(matriz)
print(traza)
%matplotlib inline
import matplotlib.pyplot as plt
%run "./utils/aux.py"
v1 = np.array([0,1])
v2 = np.array([1,0])
graficar_vectores([v1,v2], color=['blue','red'])
plt.xlim(-0.25,2)
plt.ylim(-0.25,2)
plt.show()
A = np.array([[2,0],[0,2]])
v1_transfromado = A.dot(v1)
v2_transfromado = A.dot(v2)
graficar_vectores([v1_transfromado,v2_transfromado], color=['blue','red'])
plt.xlim(-0.25,3)
plt.ylim(-0.25,3)
plt.show()
det_A = np.linalg.det(A)
print(det_A)
area_transformada = abs((v1_transfromado[0]-v2_transfromado[0]))*abs((v1_transfromado[1]-v2_transfromado[1]))
area_transformada
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_transformado = B.dot(v1)
v2_transformado = B.dot(v2)
graficar_vectores([v1_transformado,v2_transformado,v1,v2],['blue','red','orange','green'])
plt.xlim(-3, 1.5)
plt.ylim(-0.5, 3)
plt.show()