【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()函数将近似图像显示出来。

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

相关推荐
presenttttt7 分钟前
用Python和OpenCV从零搭建一个完整的双目视觉系统(六 最终篇)
开发语言·python·opencv·计算机视觉
hie9889412 分钟前
采用最小二乘支持向量机(LSSVM)模型预测气象
算法·机器学习·支持向量机
测试19981 小时前
软件测试之压力测试总结
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·压力测试
李昊哲小课2 小时前
销售数据可视化分析项目
python·信息可视化·数据分析·matplotlib·数据可视化·seaborn
烛阴2 小时前
带参数的Python装饰器原来这么简单,5分钟彻底掌握!
前端·python
平和男人杨争争2 小时前
机器学习12——支持向量机中
算法·机器学习·支持向量机
平和男人杨争争2 小时前
机器学习14——线性回归
人工智能·机器学习·线性回归
全干engineer2 小时前
Flask 入门教程:用 Python 快速搭建你的第一个 Web 应用
后端·python·flask·web
nightunderblackcat2 小时前
新手向:Python网络编程,搭建简易HTTP服务器
网络·python·http
李昊哲小课3 小时前
pandas销售数据分析
人工智能·python·数据挖掘·数据分析·pandas