采光板及采光瓦营销型网站建设,怎样在网上做推广,陕西建设网站官方,莱芜金点子电子版最新招聘信息文章目录 一、Numpy的基础知识实验1 生成由随机数组成的三通道图片#xff0c;分别显示每个维度图片#xff0c;并将三个通道的像素四周进行填充#xff0c;分别从上下左右各填充若干数据。 二、Numpy的线性代数运算实验2 请准备一张图片#xff0c;按照上面的过程进行矩阵… 文章目录 一、Numpy的基础知识实验1 生成由随机数组成的三通道图片分别显示每个维度图片并将三个通道的像素四周进行填充分别从上下左右各填充若干数据。 二、Numpy的线性代数运算实验2 请准备一张图片按照上面的过程进行矩阵奇异分解要求保存前50个特征值进行压缩。 一、Numpy的基础知识
创建列表
import numpy as np
np.array([1,2,3])array([1, 2, 3])np.array([[1,2],[2,3]])array([[1, 2],[2, 3]])快捷方式创建列表
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]])采用随机数生成数组
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]])利用随机数生成图片
import numpy as np
import numpy.random as rd
import matplotlib.pyplot as plt
plt.figure(figsize(2,2))
imgrd.randint(0,255,(10,10))
plt.imshow(img)matplotlib.image.AxesImage at 0x243604a2250arr1rd.randn(1,3)
arr1.astype(float32)array([[ 0.47883075, -0.5455359 , -1.2719026 ]], dtypefloat32)数组常见属性
arr1.shape,arr1.T,arr1.dtype,arr1.ndim((1, 3),array([[ 0.47883076],[-0.54553593],[-1.27190261]]),dtype(float64),2)数组的访问
arrnp.arange(1,10).reshape(3,3)
arrarray([[1, 2, 3],[4, 5, 6],[7, 8, 9]])plt.figure(figsize(2,2))
plt.imshow(arr)matplotlib.image.AxesImage at 0x24360c1ed00arr[: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 0x24360c78af0arr[::-1,]
plt.figure(figsize(2,2))
plt.imshow(arr[::-1,])matplotlib.image.AxesImage at 0x24360ccdd30arr[::-1,].T
plt.figure(figsize(2,2))
plt.imshow(arr[::-1,].T)matplotlib.image.AxesImage at 0x24360d24e20arr.T[::-1,]
plt.figure(figsize(2,2))
plt.imshow(arr.T[::-1,])matplotlib.image.AxesImage at 0x24360d78a30arr.T[::,::-1]
plt.figure(figsize(2,2))
plt.imshow(arr.T[::,::-1])matplotlib.image.AxesImage at 0x24360dcc730可视化2*2像素的一张图
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
from scipy import misc
plt.figure(figsize(2,2))
img misc.face()
plt.imshow(img)matplotlib.image.AxesImage at 0x24360ef4be0img.shape(768, 1024, 3)plt.figure(figsize(2,2))
plt.imshow(img[:,:512,:])matplotlib.image.AxesImage at 0x24361197ca0plt.figure(figsize(2,2))
plt.imshow(img[:384,:,:])matplotlib.image.AxesImage at 0x2436131cd00plt.figure(figsize(2,2))
plt.imshow(img[:,:,2])matplotlib.image.AxesImage at 0x2436149bcd0img_rimg[:,:,2]
plt.figure(figsize(2,2))
plt.imshow(img_r[::-1,:])matplotlib.image.AxesImage at 0x243614f7250img_rimg[:,:,2]
plt.figure(figsize(2,2))
plt.imshow(img_r[::,::-1])matplotlib.image.AxesImage at 0x2436154b4c0img_newnp.insert(img_r,0,img_r[:50],axis0)
plt.figure(figsize(2,2))
plt.imshow(img_new)matplotlib.image.AxesImage at 0x2436159b790img_newnp.insert(img_r,0,img_r[:,:100].T,axis1)
plt.figure(figsize(2,2))
plt.imshow(img_new)matplotlib.image.AxesImage at 0x243615f0f40plt.figure(figsize(2,2))
plt.imshow(np.concatenate([img_r,img_r],axis1))matplotlib.image.AxesImage at 0x24362a74c10plt.figure(figsize(2,2))
plt.imshow(np.concatenate([img_r,img_r],axis0))matplotlib.image.AxesImage at 0x243628f9be0plt.figure(figsize(2,2))
plt.imshow(np.stack([img_r,img_r],axis0)[0])matplotlib.image.AxesImage at 0x2436294cdf0实验1 生成由随机数组成的三通道图片分别显示每个维度图片并将三个通道的像素四周进行填充分别从上下左右各填充若干数据。
程序设计
#利用随机数生成图片
import numpy as np
import numpy.random as rd
import matplotlib.pyplot as plt
figplt.figure(figsize(4,4))#四张子图
ax1fig.add_subplot(221)
ax2fig.add_subplot(222)
ax3fig.add_subplot(223)
ax4fig.add_subplot(224)#用随机数数组填充子图
imgrd.randint(0,255,(10,10))
ax1.imshow(img)
ax2.imshow(img)
ax3.imshow(img)
ax4.imshow(img) #从上方填充
img1np.insert(img,0,img[0,:],axis0)
ax1.imshow(img1)#从下面填充
img2np.insert(img,-1,img[-1,:],axis0)
ax2.imshow(img2)#从左边填充
img3np.insert(img,0,img[:,0],axis1)
ax3.imshow(img3)#从右边填充
img4np.insert(img,-1,img[:,-1],axis1)
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的线性代数运算
import numpy.linalg as la
arr1np.arange(1,5).reshape(2,2)
arr1array([[1, 2],[3, 4]])la.det(arr1)-2.0000000000000004la.inv(arr1)array([[-2. , 1. ],[ 1.5, -0.5]])arr1la.inv(arr1)array([[1.00000000e00, 1.11022302e-16],[0.00000000e00, 1.00000000e00]])np.dot(arr1,la.inv(arr1))array([[1.00000000e00, 1.11022302e-16],[0.00000000e00, 1.00000000e00]])#矩阵奇异分解
U,s,Vla.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]]))注意 s是个对角方阵这里用一维数组做了简写。 np.diag(s) 是其本该有的样子。 #重构矩阵
Unp.diag(s)Varray([[1., 2.],[3., 4.]])plt.figure(figsize(2,2))
plt.imshow(img_r,cmaphot)matplotlib.image.AxesImage at 0x24362cde2b0U,s,Vla.svd(img_r)U.shape,s.shape,V.shape((768, 768), (768,), (1024, 1024))#重构图像
Snp.zeros((U.shape[1],V.shape[0]))
np.fill_diagonal(S,s)
S.shape(768, 1024)plt.imshow(USV)matplotlib.image.AxesImage at 0x24362d45160#只用一部分来重构图像
k500
appro_imagUS[:,:20]V[:20,:]
plt.imshow(appro_imag)matplotlib.image.AxesImage at 0x2436554cdc0结论 使用奇异值分解可以获得图像的近似表示。此技术可以用于图像压缩或者图像的主成分分析。
appro_imag.shape(768, 1024)实验2 请准备一张图片按照上面的过程进行矩阵奇异分解要求保存前50个特征值进行压缩。
程序设计
from PIL import Image
image misc.ascent()
plt.imshow(image)matplotlib.image.AxesImage at 0x243661b3340U,s,Vla.svd(image)
U.shape,s.shape,V.shape
Snp.zeros((U.shape[1],V.shape[0]))
np.fill_diagonal(S,s)
k50
appro_imagUS[:,: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()函数将近似图像显示出来。
总体而言这段代码是对图像进行奇异值分解并根据提取到的奇异值和奇异向量重构了一个近似图像并将其显示出来。