0% found this document useful (0 votes)
134 views20 pages

Matrices Parte 2 - Jupyter Notebook

This document discusses various matrix applications in Python including: 1. Loading an image and converting it to a NumPy array to manipulate pixel values. Sections of the image array are displayed to show the RGB channels. 2. Examples of copying arrays and modifying them to show that changes don't propagate between shallow and deep copies. 3. Applying Gram-Schmidt orthonormalization and QR decomposition to matrices. Examples show reconstructing the original matrix from the factored forms.

Uploaded by

luisrrg2267
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views20 pages

Matrices Parte 2 - Jupyter Notebook

This document discusses various matrix applications in Python including: 1. Loading an image and converting it to a NumPy array to manipulate pixel values. Sections of the image array are displayed to show the RGB channels. 2. Examples of copying arrays and modifying them to show that changes don't propagate between shallow and deep copies. 3. Applying Gram-Schmidt orthonormalization and QR decomposition to matrices. Examples show reconstructing the original matrix from the factored forms.

Uploaded by

luisrrg2267
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

28/1/2021 matrices parte 2 - Jupyter Notebook

Aplicaciones de las matrices con python


1.- Busque una imagen en google de 200 x 200 pixeles

imagesize:200x200 perro

In [1]:

import PIL
print('Pillow Version:', PIL.__version__)

Pillow Version: 7.1.2

In [3]:

import numpy
print('numpy Version:', numpy.__version__)

numpy Version: 1.18.1

127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 1/20


28/1/2021 matrices parte 2 - Jupyter Notebook

In [4]:

from PIL import Image


image = Image.open('perro.jpg')

print(image.format)
print(image.size)
print(image.mode)

JPEG
(200, 200)
RGB

In [5]:

image.show()

Convertimos el objeto imagen en un array

127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 2/20


28/1/2021 matrices parte 2 - Jupyter Notebook

In [6]:

import numpy as np

img_data = np.asarray(image)
print(type(img_data))
print(img_data.shape)

<class 'numpy.ndarray'>
(200, 200, 3)

In [9]:

127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 3/20


28/1/2021 matrices parte 2 - Jupyter Notebook

In [7]:

img_data[0:10,0:10,0]

Out[7]:

array([[255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
[254, 254, 254, 254, 254, 254, 254, 254, 254, 254],
[254, 254, 254, 254, 254, 254, 254, 254, 254, 254],
[254, 254, 254, 254, 254, 254, 254, 254, 254, 254],
[255, 255, 255, 255, 255, 255, 255, 255, 254, 254],
[255, 255, 255, 255, 255, 255, 255, 255, 254, 254]], dtype=u
int8)

In [18]:

red_im = Image.fromarray(img_data[:,:,0])

In [19]:

red_im.show()

127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 4/20


28/1/2021 matrices parte 2 - Jupyter Notebook

Ejemplo de efectos en la copia de arreglos


matA = np.array([[1,2],[3,4]]); matB=matA; matA[0,0]=10

In [20]:

matA = np.array([[1,2],[3,4]]); matB=matA; matA[0,0]=10

In [21]:

matB

Out[21]:

array([[10, 2],
[ 3, 4]])

In [22]:

matA = np.array([[1,2],[3,4]]); matB=np.array(matA); matA[0,0]=10; matB

Out[22]:

array([[1, 2],
[3, 4]])

127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 5/20


28/1/2021 matrices parte 2 - Jupyter Notebook

Mostramos la imagen en tonos de rojo


In [29]:

red_data = np.array(img_data)
red_data[:,:,0]=img_data[:,:,0]
red_data[:,:,1:3]=0
Image.fromarray(red_data).show()

In [ ]:

In [30]:

gr_im = Image.fromarray(img_data)
gr_im.show()

In [12]:

In [ ]:

127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 6/20


28/1/2021 matrices parte 2 - Jupyter Notebook

In [18]:

gr_im = Image.fromarray(img_data).save('otro_perro.png')

In [ ]:

Ortogonalizacion de matrices

127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 7/20


28/1/2021 matrices parte 2 - Jupyter Notebook

In [70]:

def gs(A):
(m,n) = A.shape
Q = np.zeros_like(A)
R = np.zeros((n,n))
for j in range(n):
print(f"Columna {j+1}")
v = A[:,j]
for i in range(j):
R[i,j] = np.dot(Q[:,i],A[:,j])
v = v - R[i,j]*Q[:,i]
R[j,j] = np.linalg.norm(v)
Q[:,j] = v/R[j,j]
#print(v/R[j,j],Q[:,j] )
return Q,R

In [71]:

A= np.array([[1,2,3],[3,4,5],[5,6,7]],dtype='f4')

127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 8/20


28/1/2021 matrices parte 2 - Jupyter Notebook

In [72]:

Q,R = gs(A); print(Q.dtype)

Columna 1
Columna 2
Columna 3
float32

In [73]:

Q@R

Out[73]:

array([[1. , 1.99999996, 2.99999995],


[3.00000001, 4.00000009, 4.99999992],
[5.00000001, 5.99999998, 6.99999987]])

127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 9/20


28/1/2021 matrices parte 2 - Jupyter Notebook

In [75]:

Q.T@Q

Out[75]:

array([[ 9.9999994e-01, 8.9406967e-07, -7.7666223e-02],


[ 8.9406967e-07, 9.9999994e-01, -9.9671614e-01],
[-7.7666223e-02, -9.9671614e-01, 9.9999994e-01]], dtype=flo
at32)

In [76]:

Out[76]:

array([[5.91608000e+00, 7.43735695e+00, 8.95863438e+00],


[0.00000000e+00, 8.28078687e-01, 1.65616584e+00],
[0.00000000e+00, 0.00000000e+00, 8.49679873e-06]])

In [ ]:

127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 10/20


28/1/2021 matrices parte 2 - Jupyter Notebook

In [34]:

A=np.array( [ [1,2,1],[2,2,-1],[1,1,2],[1,-2,1] ])
gs(A)

Columna 1
Columna 2
Columna 3

Out[34]:

array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])

In [ ]:

In [ ]:

127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 11/20


28/1/2021 matrices parte 2 - Jupyter Notebook

In [65]:

def qr(A):
m, n = A.shape
Q = np.zeros((m, n))
R = np.zeros((n, n))

for j in range(n):
v = A[:, j]

for i in range(j - 1):


q = Q[:, i]
R[i, j] = q.dot(v)
v = v - R[i, j] * q

norm = np.linalg.norm(v)
Q[:, j] = v / norm
R[j, j] = norm
return Q, R
#A = np.random.rand(4,4) * 1000
A= np.array([[1,2,3],[3,4,5],[5,6,7]])
Q, R = qr(A)

127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 12/20


28/1/2021 matrices parte 2 - Jupyter Notebook

In [66]:

np.linalg.norm(A-Q@R)

Out[66]:

0.0

Solucion de minimos cuadrados

Ejemplo
127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 13/20
28/1/2021 matrices parte 2 - Jupyter Notebook

Resolver

𝑥 + 2𝑦 + 𝑧 = 1
2𝑥 + 𝑦 − 𝑧 = 4
𝑥 + 𝑦 + 2𝑧 = 2
𝑥 − 2𝑦 + 𝑧 = 0
In [67]:

A=np.array( [ [1,2,1],[2,2,-1],[1,1,2],[1,-2,1] ])

In [68]:

b = np.array( [1,4,2,0])

In [69]:

Q, R = qr(A)

127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 14/20


28/1/2021 matrices parte 2 - Jupyter Notebook

In [70]:

Out[70]:

array([[ 0.37796447, 0.5547002 , 0.28171808],


[ 0.75592895, 0.5547002 , -0.61977979],
[ 0.37796447, 0.2773501 , 0.6761234 ],
[ 0.37796447, -0.5547002 , 0.28171808]])

In [71]:

Out[71]:

array([[2.64575131, 0. , 0.75592895],
[0. , 3.60555128, 0. ],
[0. , 0. , 2.53546276]])

127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 15/20


28/1/2021 matrices parte 2 - Jupyter Notebook

In [73]:

Q.T@Q

Out[73]:

array([[ 1.00000000e+00, 5.24142418e-01, 1.66533454e-16],


[ 5.24142418e-01, 1.00000000e+00, -1.56269077e-01],
[ 1.66533454e-16, -1.56269077e-01, 1.00000000e+00]])

In [76]:

sol=np.linalg.solve(R,Q.T@b)

In [77]:

print(sol)

[ 1.66666667 0.92307692 -0.33333333]

Factorizacion Cholesky

127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 16/20


28/1/2021 matrices parte 2 - Jupyter Notebook

127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 17/20


28/1/2021 matrices parte 2 - Jupyter Notebook

In [88]:

def chol(A):
m,n = A.shape
L = np.zeros_like(A)
L[0,0]=np.sqrt(A[0,0])
for k in range(n-1):
L[k+1,0] = A[k+1,0]/L[0,0]

for i in range(1,k-1):
L[k+1,i] = ( A[k+1,i] - np.sum( L[k+1,1:i-1]*L[i,1:i-1] ) )/L[i,i]

L[k+1,1:k] = np.sqrt(A[k+1,k+1]-np.sum( L[k+1,1:k]**2))


return L

In [92]:

A = 2*np.eye(4)+np.diag([-1,-1,-1],-1)+np.diag([-1,-1,-1],1)

127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 18/20


28/1/2021 matrices parte 2 - Jupyter Notebook

In [90]:

Out[90]:

array([[ 2., -1., 0., 0.],


[-1., 2., -1., 0.],
[ 0., -1., 2., -1.],
[ 0., 0., -1., 2.]])

In [91]:

chol(A)

Out[91]:

array([[ 1.41421356, 0. , 0. , 0. ],
[-0.70710678, 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. ],
[ 0. , 1.41421356, 0. , 0. ]])

In [ ]:

127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 19/20


28/1/2021 matrices parte 2 - Jupyter Notebook

127.0.0.1:8888/notebooks/Documentos/2020-1/matrices parte 2.ipynb 20/20

You might also like