【Python机器学习】实验01 Numpy以及可视化回顾

文章目录

  • 一、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()

具体分析

这段代码是利用随机数生成图片,并在将图片填充到四个子图中展示。以下是代码的具体分析:

  1. 导入numpy库,用于生成随机数和操作数组;导入matplotlib库,用于绘制图像。
  2. 创建一个大小为4x4的Figure对象,即一个包含4个子图的画布。
  3. 使用add_subplot()函数创建四个子图对象ax1、ax2、ax3和ax4。
  4. 使用randint()函数生成一个10x10的随机数数组img,并将其作为参数传递给imshow()函数并分别绘制到四个子图上。
  5. 从上方填充子图1(ax1):使用insert()函数在数组img的第一行之前插入第一行,并将结果赋给img1。然后使用imshow()函数在子图1上展示img1。
  6. 从下方填充子图2(ax2):使用insert()函数在数组img的倒数第一行之前插入最后一行,并将结果赋给img2。然后使用imshow()函数在子图2上展示img2。
  7. 从左边填充子图3(ax3):使用insert()函数在数组img的第一列之前插入第一列,并将结果赋给img3。然后使用imshow()函数在子图3上展示img3。
  8. 从右边填充子图4(ax4):使用insert()函数在数组img的倒数第一列之前插入最后一列,并将结果赋给img4。然后使用imshow()函数在子图4上展示img4。
  9. 使用tight_layout()函数调整子图的布局,使其适应画布。
  10. 使用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)处理。以下是代码的具体分析:

  1. 导入PIL库中的Image模块。
  2. 使用ascent()函数生成一个图像image。
  3. 使用numpy的线性代数函数la.svd()对图像进行奇异值分解,将结果分别赋给U、s和V三个变量。
  4. 使用U.shape、s.shape和V.shape分别获得U、s和V的形状(维度)信息,并输出。
  5. 创建一个全零矩阵S,其行数为U的列数,列数为V的行数。
  6. 使用numpy的fill_diagonal()函数将s数组中的元素按对角线方向填充到S矩阵之中,通过对角线填充的方式将奇异值转化为奇异值矩阵。
  7. 设置一个参数k为50,表示提取前k个奇异值和对应的奇异向量。
  8. 使用U、S和V的切片操作,分别选取前k列的奇异向量和前k行的奇异值矩阵,并通过矩阵乘法运算得到近似图像。
  9. 使用plt的imshow()函数将近似图像显示出来。

总体而言,这段代码是对图像进行奇异值分解,并根据提取到的奇异值和奇异向量重构了一个近似图像,并将其显示出来。

相关推荐
AI原吾3 小时前
掌握Python-uinput:打造你的输入设备控制大师
开发语言·python·apython-uinput
毕设木哥3 小时前
25届计算机专业毕设选题推荐-基于python的二手电子设备交易平台【源码+文档+讲解】
开发语言·python·计算机·django·毕业设计·课程设计·毕设
weixin_455446173 小时前
Python学习的主要知识框架
开发语言·python·学习
D11_4 小时前
Pandas缺失值处理
python·机器学习·数据分析·numpy·pandas
花生了什么树~.4 小时前
python基础知识(四)--if语句,for\while循环
python
IT毕设梦工厂5 小时前
计算机毕业设计选题推荐-在线拍卖系统-Java/Python项目实战
java·spring boot·python·django·毕业设计·源码·课程设计
luthane6 小时前
python 实现average mean平均数算法
开发语言·python·算法
静心问道6 小时前
WGAN算法
深度学习·算法·机器学习
码农研究僧6 小时前
Flask 实现用户登录功能的完整示例:前端与后端整合(附Demo)
python·flask·用户登录
Ylucius6 小时前
动态语言? 静态语言? ------区别何在?java,js,c,c++,python分给是静态or动态语言?
java·c语言·javascript·c++·python·学习