文章目录
- 一、Numpy的基础知识
-
- [实验1 生成由随机数组成的三通道图片,分别显示每个维度图片,并将三个通道的像素四周进行填充,分别从上下左右各填充若干数据。](#实验1 生成由随机数组成的三通道图片,分别显示每个维度图片,并将三个通道的像素四周进行填充,分别从上下左右各填充若干数据。)
- 二、Numpy的线性代数运算
-
- [实验2 请准备一张图片,按照上面的过程进行矩阵奇异分解,要求保存前50个特征值进行压缩。](#实验2 请准备一张图片,按照上面的过程进行矩阵奇异分解,要求保存前50个特征值进行压缩。)
一、Numpy的基础知识
- 创建列表
python
import numpy as np
np.array([1,2,3])
array([1, 2, 3])
python
np.array([[1,2],[2,3]])
array([[1, 2],
[2, 3]])
- 快捷方式创建列表
python
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]))
python
range(10,1,-1)
range(10, 1, -1)
python
np.linspace(1,10,5)
array([ 1. , 3.25, 5.5 , 7.75, 10. ])
python
np.zeros((2,2))
array([[0., 0.],
[0., 0.]])
python
np.ones((1,1))
array([[1.]])
python
np.diag([1,2])
array([[1, 0],
[0, 2]])
- 采用随机数生成数组
python
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]])
python
rd.random((1,3))
array([[0.33035627, 0.1179577 , 0.68061576]])
python
rd.normal(2,6,(2,4))
array([[ 5.6250594 , 8.07709039, 1.92724817, -4.75702484],
[-1.71722434, 2.69880337, -6.20162398, -0.62033363]])
- 利用随机数生成图片
python
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>

python
arr1=rd.randn(1,3)
arr1.astype("float32")
array([[ 0.47883075, -0.5455359 , -1.2719026 ]], dtype=float32)
- 数组常见属性
python
arr1.shape,arr1.T,arr1.dtype,arr1.ndim
((1, 3),
array([[ 0.47883076],
[-0.54553593],
[-1.27190261]]),
dtype('float64'),
2)
- 数组的访问
python
arr=np.arange(1,10).reshape(3,3)
arr
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
python
plt.figure(figsize=(2,2))
plt.imshow(arr)
<matplotlib.image.AxesImage at 0x24360c1ed00>

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

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

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

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

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

- 可视化2*2像素的一张图
python
import matplotlib.pyplot as plt
plt.figure(figsize=(2,2))
plt.imshow([[0,1],[1,0]])
<matplotlib.image.AxesImage at 0x24360e24340>

- 数组的应用np.insert,np.concatenate,np.stack,np.tile
python
from scipy import misc
plt.figure(figsize=(2,2))
img = misc.face()
plt.imshow(img)
<matplotlib.image.AxesImage at 0x24360ef4be0>

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

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

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

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

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

python
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>

python
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>

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

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

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

实验1 生成由随机数组成的三通道图片,分别显示每个维度图片,并将三个通道的像素四周进行填充,分别从上下左右各填充若干数据。
程序设计
python
#利用随机数生成图片
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()

具体分析
这段代码是利用随机数生成图片,并在将图片填充到四个子图中展示。以下是代码的具体分析:
- 导入numpy库,用于生成随机数和操作数组;导入matplotlib库,用于绘制图像。
- 创建一个大小为4x4的Figure对象,即一个包含4个子图的画布。
- 使用add_subplot()函数创建四个子图对象ax1、ax2、ax3和ax4。
- 使用randint()函数生成一个10x10的随机数数组img,并将其作为参数传递给imshow()函数并分别绘制到四个子图上。
- 从上方填充子图1(ax1):使用insert()函数在数组img的第一行之前插入第一行,并将结果赋给img1。然后使用imshow()函数在子图1上展示img1。
- 从下方填充子图2(ax2):使用insert()函数在数组img的倒数第一行之前插入最后一行,并将结果赋给img2。然后使用imshow()函数在子图2上展示img2。
- 从左边填充子图3(ax3):使用insert()函数在数组img的第一列之前插入第一列,并将结果赋给img3。然后使用imshow()函数在子图3上展示img3。
- 从右边填充子图4(ax4):使用insert()函数在数组img的倒数第一列之前插入最后一列,并将结果赋给img4。然后使用imshow()函数在子图4上展示img4。
- 使用tight_layout()函数调整子图的布局,使其适应画布。
- 使用show()函数显示画布和子图。
二、Numpy的线性代数运算
python
import numpy.linalg as la
arr1=np.arange(1,5).reshape(2,2)
arr1
array([[1, 2],
[3, 4]])
python
la.det(arr1)
-2.0000000000000004
python
la.inv(arr1)
array([[-2. , 1. ],
[ 1.5, -0.5]])
python
arr1@la.inv(arr1)
array([[1.00000000e+00, 1.11022302e-16],
[0.00000000e+00, 1.00000000e+00]])
python
np.dot(arr1,la.inv(arr1))
array([[1.00000000e+00, 1.11022302e-16],
[0.00000000e+00, 1.00000000e+00]])
python
#矩阵奇异分解
U,s,V=la.svd(arr1)
python
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]]))
注意, s是个对角方阵,这里用一维数组做了简写。
np.diag(s) 是其本该有的样子。
python
#重构矩阵
U@np.diag(s)@V
array([[1., 2.],
[3., 4.]])
python
plt.figure(figsize=(2,2))
plt.imshow(img_r,cmap="hot")
<matplotlib.image.AxesImage at 0x24362cde2b0>

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

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

结论: 使用奇异值分解可以获得图像的近似表示。此技术可以用于图像压缩或者,图像的主成分分析。
python
appro_imag.shape
(768, 1024)
实验2 请准备一张图片,按照上面的过程进行矩阵奇异分解,要求保存前50个特征值进行压缩。
程序设计
python
from PIL import Image
image = misc.ascent()
plt.imshow(image)
<matplotlib.image.AxesImage at 0x243661b3340>

python
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>
具体分析
这段代码使用了PIL库中的Image模块,通过其ascent()函数生成了一个图像。然后使用numpy和scipy的线性代数函数对图像进行奇异值分解(SVD)处理。以下是代码的具体分析:
- 导入PIL库中的Image模块。
- 使用ascent()函数生成一个图像image。
- 使用numpy的线性代数函数la.svd()对图像进行奇异值分解,将结果分别赋给U、s和V三个变量。
- 使用U.shape、s.shape和V.shape分别获得U、s和V的形状(维度)信息,并输出。
- 创建一个全零矩阵S,其行数为U的列数,列数为V的行数。
- 使用numpy的fill_diagonal()函数将s数组中的元素按对角线方向填充到S矩阵之中,通过对角线填充的方式将奇异值转化为奇异值矩阵。
- 设置一个参数k为50,表示提取前k个奇异值和对应的奇异向量。
- 使用U、S和V的切片操作,分别选取前k列的奇异向量和前k行的奇异值矩阵,并通过矩阵乘法运算得到近似图像。
- 使用plt的imshow()函数将近似图像显示出来。
总体而言,这段代码是对图像进行奇异值分解,并根据提取到的奇异值和奇异向量重构了一个近似图像,并将其显示出来。