[Python Machine Learning] Experiment 01 Numpy and Visualization Review

1. Basic knowledge of Numpy

  • create list
import numpy as np
np.array([1,2,3])
array([1, 2, 3])
np.array([[1,2],[2,3]])
array([[1, 2],
       [2, 3]])
  • Shortcut creation list
np.arange(1,10),np.arange(10,1,-1)
(array([1, 2, 3, 4, 5, 6, 7, 8, 9]),
 array([10,  9,  8,  7,  6,  5,  4,  3,  2]))
range(10,1,-1)
range(10, 1, -1)
np.linspace(1,10,5)
array([ 1.  ,  3.25,  5.5 ,  7.75, 10.  ])
np.zeros((2,2))
array([[0., 0.],
       [0., 0.]])
np.ones((1,1))
array([[1.]])
np.diag([1,2])
array([[1, 0],
       [0, 2]])
  • Generate an array with random numbers
import numpy.random as rd
rd.uniform(2,3,[3,4])
array([[2.00870568, 2.84081335, 2.56773483, 2.31232497],
       [2.4091653 , 2.22513678, 2.62473312, 2.20786884],
       [2.8608431 , 2.04426497, 2.73712184, 2.73669482]])
rd.random((1,3))
array([[0.33035627, 0.1179577 , 0.68061576]])
rd.normal(2,6,(2,4))
array([[ 5.6250594 ,  8.07709039,  1.92724817, -4.75702484],
       [-1.71722434,  2.69880337, -6.20162398, -0.62033363]])
  • Generate images using random numbers
import numpy as np
import numpy.random as rd
import matplotlib.pyplot as plt
plt.figure(figsize=(2,2))
img=rd.randint(0,255,(10,10))
plt.imshow(img)
<matplotlib.image.AxesImage at 0x243604a2250>

1

arr1=rd.randn(1,3)
arr1.astype("float32")
array([[ 0.47883075, -0.5455359 , -1.2719026 ]], dtype=float32)
  • Array Common Properties
arr1.shape,arr1.T,arr1.dtype,arr1.ndim
((1, 3),
 array([[ 0.47883076],
        [-0.54553593],
        [-1.27190261]]),
 dtype('float64'),
 2)
  • array access
arr=np.arange(1,10).reshape(3,3)
arr
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
plt.figure(figsize=(2,2))
plt.imshow(arr)
<matplotlib.image.AxesImage at 0x24360c1ed00>

2

arr[:2,:2]
array([[1, 2],
       [4, 5]])
arr[[0,2],:2]
array([[1, 2],
       [7, 8]])
arr.T
plt.figure(figsize=(2,2))
plt.imshow(arr.T)
<matplotlib.image.AxesImage at 0x24360c78af0>

3

arr[::-1,]
plt.figure(figsize=(2,2))
plt.imshow(arr[::-1,])
<matplotlib.image.AxesImage at 0x24360ccdd30>

4

arr[::-1,].T
plt.figure(figsize=(2,2))
plt.imshow(arr[::-1,].T)
<matplotlib.image.AxesImage at 0x24360d24e20>

5

arr.T[::-1,]
plt.figure(figsize=(2,2))
plt.imshow(arr.T[::-1,])
<matplotlib.image.AxesImage at 0x24360d78a30>

6

arr.T[::,::-1]
plt.figure(figsize=(2,2))
plt.imshow(arr.T[::,::-1])
<matplotlib.image.AxesImage at 0x24360dcc730>

7

  • Visualize a picture of 2*2 pixels
import matplotlib.pyplot as plt
plt.figure(figsize=(2,2))
plt.imshow([[0,1],[1,0]])
<matplotlib.image.AxesImage at 0x24360e24340>

8

  • Array application np.insert, np.concatenate, np.stack, np.tile
from scipy import misc
plt.figure(figsize=(2,2))
img = misc.face()
plt.imshow(img)
<matplotlib.image.AxesImage at 0x24360ef4be0>

9

img.shape
(768, 1024, 3)
plt.figure(figsize=(2,2))
plt.imshow(img[:,:512,:])
<matplotlib.image.AxesImage at 0x24361197ca0>

10

plt.figure(figsize=(2,2))
plt.imshow(img[:384,:,:])
<matplotlib.image.AxesImage at 0x2436131cd00>

11

plt.figure(figsize=(2,2))
plt.imshow(img[:,:,2])
<matplotlib.image.AxesImage at 0x2436149bcd0>

12

img_r=img[:,:,2]
plt.figure(figsize=(2,2))
plt.imshow(img_r[::-1,:])
<matplotlib.image.AxesImage at 0x243614f7250>

13

img_r=img[:,:,2]
plt.figure(figsize=(2,2))
plt.imshow(img_r[::,::-1])
<matplotlib.image.AxesImage at 0x2436154b4c0>

14

img_new=np.insert(img_r,0,img_r[:50],axis=0)
plt.figure(figsize=(2,2))
plt.imshow(img_new)
<matplotlib.image.AxesImage at 0x2436159b790>

15

img_new=np.insert(img_r,0,img_r[:,:100].T,axis=1)
plt.figure(figsize=(2,2))
plt.imshow(img_new)
<matplotlib.image.AxesImage at 0x243615f0f40>

16

plt.figure(figsize=(2,2))
plt.imshow(np.concatenate([img_r,img_r],axis=1))
<matplotlib.image.AxesImage at 0x24362a74c10>

17

plt.figure(figsize=(2,2))
plt.imshow(np.concatenate([img_r,img_r],axis=0))
<matplotlib.image.AxesImage at 0x243628f9be0>

18

plt.figure(figsize=(2,2))
plt.imshow(np.stack([img_r,img_r],axis=0)[0])
<matplotlib.image.AxesImage at 0x2436294cdf0>

19

Experiment 1 generates a three-channel image composed of random numbers, displays each dimension of the image separately, and fills the surrounding pixels of the three channels, and fills some data from the top, bottom, left, and right respectively.

programming

#利用随机数生成图片
import numpy as np
import numpy.random as rd
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(4,4))

#四张子图
ax1=fig.add_subplot(221)
ax2=fig.add_subplot(222)
ax3=fig.add_subplot(223)
ax4=fig.add_subplot(224)

#用随机数数组填充子图
img=rd.randint(0,255,(10,10))
ax1.imshow(img)
ax2.imshow(img) 
ax3.imshow(img)  
ax4.imshow(img)  

#从上方填充
img1=np.insert(img,0,img[0,:],axis=0)
ax1.imshow(img1)

#从下面填充
img2=np.insert(img,-1,img[-1,:],axis=0)
ax2.imshow(img2)

#从左边填充
img3=np.insert(img,0,img[:,0],axis=1)
ax3.imshow(img3)

#从右边填充
img4=np.insert(img,-1,img[:,-1],axis=1)
ax4.imshow(img4)

plt.tight_layout()
plt.show()

20

Specific analysis

This code is to use random numbers to generate pictures and display them in filling the pictures into four subgraphs. The following is a specific analysis of the code:

  1. Import the numpy library for generating random numbers and manipulating arrays; import the matplotlib library for drawing images.
  2. Create a Figure object of size 4x4, i.e. a canvas containing 4 subfigures.
  3. Create four subplot objects ax1, ax2, ax3, and ax4 using the add_subplot() function.
  4. Use the randint() function to generate a 10x10 random number array img, pass it as a parameter to the imshow() function and draw it on the four subgraphs respectively.
  5. Fill subplot 1 (ax1) from above: use the insert() function to insert the first row before the first row of the array img, and assign the result to img1. Then use the imshow() function to display img1 on subfigure 1.
  6. Fill subfigure 2 (ax2) from below: use the insert() function to insert the last row before the penultimate row of the array img, and assign the result to img2. Then use the imshow() function to display img2 on subfigure 2.
  7. Fill subfigure 3 (ax3) from the left: use the insert() function to insert the first column before the first column of the array img, and assign the result to img3. Then use the imshow() function to display img3 on subfigure 3.
  8. Fill subfigure 4 (ax4) from the right: use the insert() function to insert the last column before the penultimate column of the array img, and assign the result to img4. Then use the imshow() function to display img4 on subfigure 4.
  9. Use the tight_layout() function to adjust the layout of the subplot so that it fits the canvas.
  10. Use the show() function to display the canvas and subplots.

Two, Numpy's linear algebra operations

import numpy.linalg as la
arr1=np.arange(1,5).reshape(2,2)
arr1
array([[1, 2],
       [3, 4]])
la.det(arr1)
-2.0000000000000004
la.inv(arr1)
array([[-2. ,  1. ],
       [ 1.5, -0.5]])
arr1@la.inv(arr1)
array([[1.00000000e+00, 1.11022302e-16],
       [0.00000000e+00, 1.00000000e+00]])
np.dot(arr1,la.inv(arr1))
array([[1.00000000e+00, 1.11022302e-16],
       [0.00000000e+00, 1.00000000e+00]])
#矩阵奇异分解
U,s,V=la.svd(arr1)
U,s,V
(array([[-0.40455358, -0.9145143 ],
        [-0.9145143 ,  0.40455358]]),
 array([5.4649857 , 0.36596619]),
 array([[-0.57604844, -0.81741556],
        [ 0.81741556, -0.57604844]]))

Note that s is a diagonal square matrix, which is abbreviated as a one-dimensional array here.
np.diag(s) is what it should be.

#重构矩阵
U@np.diag(s)@V
array([[1., 2.],
       [3., 4.]])
plt.figure(figsize=(2,2))
plt.imshow(img_r,cmap="hot")
<matplotlib.image.AxesImage at 0x24362cde2b0>

21

U,s,V=la.svd(img_r)
U.shape,s.shape,V.shape
((768, 768), (768,), (1024, 1024))
#重构图像
S=np.zeros((U.shape[1],V.shape[0]))
np.fill_diagonal(S,s)
S.shape
(768, 1024)
plt.imshow(U@S@V)
<matplotlib.image.AxesImage at 0x24362d45160>

22

#只用一部分来重构图像
k=500
appro_imag=U@S[:,:20]@V[:20,:]
plt.imshow(appro_imag)
<matplotlib.image.AxesImage at 0x2436554cdc0>

23

Conclusion: An approximate representation of the image can be obtained using singular value decomposition. This technique can be used for image compression or, principal component analysis of images.

appro_imag.shape
(768, 1024)

Experiment 2 Please prepare a picture, perform matrix singular decomposition according to the above process, and ask to save the first 50 eigenvalues ​​for compression.

programming

from PIL import Image
image = misc.ascent()
plt.imshow(image)
<matplotlib.image.AxesImage at 0x243661b3340>

24

U,s,V=la.svd(image)
U.shape,s.shape,V.shape
S=np.zeros((U.shape[1],V.shape[0]))
np.fill_diagonal(S,s)
k=50
appro_imag=U@S[:,:k]@V[:k,:]
plt.imshow(appro_imag)
<matplotlib.image.AxesImage at 0x2436348dc10>

25
Specific analysis

This code uses the Image module in the PIL library to generate an image through its ascent() function. The image is then subjected to singular value decomposition (SVD) using numpy and scipy's linear algebra functions. The following is a specific analysis of the code:

  1. Import the Image module in the PIL library.
  2. Use the ascent() function to generate an image image.
  3. Use numpy's linear algebra function la.svd() to perform singular value decomposition on the image, and assign the results to the three variables U, s and V respectively.
  4. Use U.shape, s.shape, and V.shape to obtain the shape (dimension) information of U, s, and V, respectively, and output them.
  5. Create an all-zero matrix S with as many rows as U and as many columns as V.
  6. Use numpy's fill_diagonal() function to fill the elements in the s array into the S matrix in the diagonal direction, and convert the singular value into a singular value matrix by diagonal filling.
  7. Setting a parameter k to 50 means extracting the first k singular values ​​and corresponding singular vectors.
  8. Using the slicing operations of U, S, and V, the singular vectors of the first k columns and the singular value matrix of the first k rows are respectively selected, and an approximate image is obtained by matrix multiplication.
  9. Use the imshow() function of plt to display the approximate image.

In general, this code is to decompose the singular value of the image, and reconstruct an approximate image according to the extracted singular value and singular vector, and display it.

Guess you like

Origin blog.csdn.net/m0_68111267/article/details/131891689