涉及到 Numpy、Matplotlib以及Pandas的使用,旨在掌握数据表示、清洗、统计和展示的能力。当今无论是科研人员还是职场人士,数据处理能力都是必备的一项技能,玩好了必定会为自己加分。
内容比较多,准备分为三篇文章连载,合集发布在我的 Github 里,文档是用 jupyter 写的,所有更适合在 Github 里看,欢迎Star关注。
一、 NumPy 篇
数据维度表示
- 一维数据:列表或集合类型
- 二维数据:列表
- 多维数据:列表
数据格式
- JSON
- XML
- YAML
# 导入NumPy库
import numpy as np
# 计算 a + b
#原始方式
a = [1, 2, 3, 4]
b = [9, 8, 7, 6]
c = []
for i in range(len(a)):
c.append(a[i]**2 + b[i]**2)
print(c)
[82, 68, 58, 52]
# 计算 a + b
# numpy实现
a = np.array([1, 2, 3, 4])
b = np.array([9, 8, 7, 6])
c = a**2 + b**2
print(c)
[82 68 58 52]
综上,
- 数组对象可以去掉元素间运算所需的循环,使一维向量更像单个数据(向量化)
- 设置专门的数组对象,经过优化,可以提升这类应用的运算速度(提升运算速度)
ndarray对象属性
- .ndim:秩,即轴的数量或维度的数量
- .shape:ndarray对象的尺度,对于矩阵,n行m列
- .size:ndarray对象元素的个数,相当于.shape中n*m的值
- .dtype:ndarray对象的元素类型
- .itemsize:ndarray对象中每个元素的大小,以字节为单位
a = np.array([[0, 1, 2, 3, 4],
[9, 8, 7, 6, 5]])
a.shap e
(2, 5)
a.size
10
创建 ndarray
- 从Python中的列表、元组等类型创建ndarray数组
- 使用NumPy中函数创建ndarray数组,如:arange, ones, zeros等
- np.arange(n):元素从0到n‐1
- np.ones(shape):生成一个全1数组,shape是元组类型
- np.zeros(shape)
- np.full(shape,val):根据shape生成一个数组,每个元素值都是val
- np.eye(n):创建一个正方的n*n单位矩阵,对角线为1,其余为0
- 从字节流(raw bytes)中创建ndarray数组
- 从文件中读取特定格式,创建ndarray数组
np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.ones((3, 4))
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
np.zeros((2, 3))
array([[0., 0., 0.],
[0., 0., 0.]])
np.full((3, 4), 6)
array([[6, 6, 6, 6],
[6, 6, 6, 6],
[6, 6, 6, 6]])
np.eye(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
np.ones((2, 3, 4))
array([[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]],
[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]])
- np.ones_like(a) 根据数组a的形状生成一个全1数组
- np.zeros_like(a) 根据数组a的形状生成一个全0数组
- np.full_like(a,val) 根据数组a的形状生成一个数组,每个元素值都是val
a = np.eye(4)
print(a)
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
np.ones_like(a)
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
- np.linspace() 根据起止数据等间距地填充数据,形成数组(等分)
- np.concatenate() 将两个或多个数组合并成一个新的数组
np.linspace(1, 10, 3)
array([ 1. , 5.5, 10. ])
ndarray 数组的变换
- .reshape(shape) 不改变数组元素,返回一个shape形状的数组,原数组不变
- .resize(shape) 与.reshape()功能一致,但修改原数组
- .swapaxes(ax1,ax2) 将数组n个维度中两个维度进行调换
- .flatten() 对数组进行降维,返回折叠后的一维数组,原数组不变
a = np.ones((2, 3, 4), dtype=np.int32)
print(a)
[[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]]
a.reshape((3, 8))
array([[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1]])
a
array([[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]])
a.resize((3, 8))
a
array([[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1]])
a.flatten()
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1])
- new_a = a.astype(new_type):类型变换
- ls = a.tolist():转换为列表
b = a.astype(np.float)
b
array([[1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1.]])
a
array([[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1]])
a.tolist()
[[1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1]]
ndarray 数组的操作
- 索引:获取数组中特定位置元素的过程
- 切片:获取数组元素子集的过程
a = np.arange(24).reshape((2, 3, 4))
a
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
a[1, 2, 3]
23
a[:, 1, -3]
array([ 5, 17])
a[:, 1:3, :]
array([[[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[16, 17, 18, 19],
[20, 21, 22, 23]]])
a[:, :, ::2]
array([[[ 0, 2],
[ 4, 6],
[ 8, 10]],
[[12, 14],
[16, 18],
[20, 22]]])
ndarray数组的运算
- 数组与标量之间的运算作用于数组的每一个元素
a = np.arange(24).reshape((2, 3, 4))
a
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
a.mean()
11.5
a = a / a.mean()
a
array([[[0. , 0.08695652, 0.17391304, 0.26086957],
[0.34782609, 0.43478261, 0.52173913, 0.60869565],
[0.69565217, 0.7826087 , 0.86956522, 0.95652174]],
[[1.04347826, 1.13043478, 1.2173913 , 1.30434783],
[1.39130435, 1.47826087, 1.56521739, 1.65217391],
[1.73913043, 1.82608696, 1.91304348, 2. ]]])
一元函数
- np.abs(x) np.fabs(x) 计算数组各元素的绝对值
- np.sqrt(x) 计算数组各元素的平方根
- np.square(x) 计算数组各元素的平方
- np.log(x) np.log10(x) np.log2(x) 计算数组各元素的自然对数、10底对数和2底对数
- np.ceil(x) np.floor(x) 计算数组各元素的ceiling值或floor值
- np.rint(x) 计算数组各元素的四舍五入值
- np.modf(x) 将数组各元素的小数和整数部分以两个独立数组形式返回
- np.cos(x) np.cosh(x) np.sin(x) np.sinh(x) np.tan(x) np.tanh(x) 计算数组各元素的普通型和双曲型三角函数
- np.exp(x) 计算数组各元素的指数值
- np.sign(x) 计算数组各元素的符号值,1(+), 0, ‐1(‐)
a = np.arange(24).reshape((2, 3, 4))
a
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
np.square(a)
array([[[ 0, 1, 4, 9],
[ 16, 25, 36, 49],
[ 64, 81, 100, 121]],
[[144, 169, 196, 225],
[256, 289, 324, 361],
[400, 441, 484, 529]]], dtype=int32)
np.sqrt(a)
array([[[0. , 1. , 1.41421356, 1.73205081],
[2. , 2.23606798, 2.44948974, 2.64575131],
[2.82842712, 3. , 3.16227766, 3.31662479]],
[[3.46410162, 3.60555128, 3.74165739, 3.87298335],
[4. , 4.12310563, 4.24264069, 4.35889894],
[4.47213595, 4.58257569, 4.69041576, 4.79583152]]])
np.exp(a)
array([[[1.00000000e+00, 2.71828183e+00, 7.38905610e+00, 2.00855369e+01],
[5.45981500e+01, 1.48413159e+02, 4.03428793e+02, 1.09663316e+03],
[2.98095799e+03, 8.10308393e+03, 2.20264658e+04, 5.98741417e+04]],
[[1.62754791e+05, 4.42413392e+05, 1.20260428e+06, 3.26901737e+06],
[8.88611052e+06, 2.41549528e+07, 6.56599691e+07, 1.78482301e+08],
[4.85165195e+08, 1.31881573e+09, 3.58491285e+09, 9.74480345e+09]]])
二元函数
- + ‐ * / ** 两个数组各元素进行对应运算
- np.maximum(x,y) np.fmax()
- np.minimum(x,y) np.fmin() 元素级的最大值/最小值计算
- np.mod(x,y) 元素级的模运算
- np.copysign(x,y) 将数组y中各元素值的符号赋值给数组x对应元素
- > < >= <= != 算术比较,产生布尔型数组
小结 1
数据存取与函数
CSV(Comma‐Separated Value, 逗号分隔值)文件存储
- 局限:只能存储一维和二维数组
np.savetxt(fram, array, fmt='%.18e', delimiter=None)
- frame : 文件、字符串或产生器,可以是.gz或.bz2的压缩文件
- array : 存入文件的数组
- fmt : 写入文件的格式,例如:%d %.2f %.18e
- delimiter : 分割字符串,默认是任何空格
import numpy as np
a = np.arange(100).reshape(5, 20)
a
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
76, 77, 78, 79],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99]])
np.savetxt('a.csv', a, fmt='%d', delimiter=',')
np.loadtxt(frame, dtype=np.float, delimiter=None, unpack=False)
- frame : 文件、字符串或产生器,可以是.gz或.bz2的压缩文件
- dtype : 数据类型,可选
- delimiter : 分割字符串,默认是任何空格
- unpack : 如果True,读入属性将分别写入不同变量
b = np.loadtxt('a.csv', delimiter=',')
b
array([[ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.,
13., 14., 15., 16., 17., 18., 19.],
[20., 21., 22., 23., 24., 25., 26., 27., 28., 29., 30., 31., 32.,
33., 34., 35., 36., 37., 38., 39.],
[40., 41., 42., 43., 44., 45., 46., 47., 48., 49., 50., 51., 52.,
53., 54., 55., 56., 57., 58., 59.],
[60., 61., 62., 63., 64., 65., 66., 67., 68., 69., 70., 71., 72.,
73., 74., 75., 76., 77., 78., 79.],
[80., 81., 82., 83., 84., 85., 86., 87., 88., 89., 90., 91., 92.,
93., 94., 95., 96., 97., 98., 99.]])
b = np.loadtxt('a.csv', dtype=np.int, delimiter=',')
b
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
76, 77, 78, 79],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99]])
多维数据存储
a.tofile(frame, sep='', format='%s')
- frame : 文件、字符串
- sep : 数据分割字符串,如果是空串,写入文件为二进制
- format : 写入数据的格式
a = np.arange(100).reshape(5, 10, 2)
a
array([[[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11],
[12, 13],
[14, 15],
[16, 17],
[18, 19]],
[[20, 21],
[22, 23],
[24, 25],
[26, 27],
[28, 29],
[30, 31],
[32, 33],
[34, 35],
[36, 37],
[38, 39]],
[[40, 41],
[42, 43],
[44, 45],
[46, 47],
[48, 49],
[50, 51],
[52, 53],
[54, 55],
[56, 57],
[58, 59]],
[[60, 61],
[62, 63],
[64, 65],
[66, 67],
[68, 69],
[70, 71],
[72, 73],
[74, 75],
[76, 77],
[78, 79]],
[[80, 81],
[82, 83],
[84, 85],
[86, 87],
[88, 89],
[90, 91],
[92, 93],
[94, 95],
[96, 97],
[98, 99]]])
a.tofile('b.dat', sep=',', format='%d')
np.fromfile(frame, dtype=float, count=‐1, sep='')
- frame : 文件、字符串
- dtype : 读取的数据类型
- count : 读入元素个数,‐1表示读入整个文件
- sep : 数据分割字符串,如果是空串,写入文件为二进制
c = np.fromfile('b.dat', dtype=np.int, sep=',')
c
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])
c = np.fromfile('b.dat', dtype=np.int, sep=',').reshape(5, 10, 2)
c
array([[[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11],
[12, 13],
[14, 15],
[16, 17],
[18, 19]],
[[20, 21],
[22, 23],
[24, 25],
[26, 27],
[28, 29],
[30, 31],
[32, 33],
[34, 35],
[36, 37],
[38, 39]],
[[40, 41],
[42, 43],
[44, 45],
[46, 47],
[48, 49],
[50, 51],
[52, 53],
[54, 55],
[56, 57],
[58, 59]],
[[60, 61],
[62, 63],
[64, 65],
[66, 67],
[68, 69],
[70, 71],
[72, 73],
[74, 75],
[76, 77],
[78, 79]],
[[80, 81],
[82, 83],
[84, 85],
[86, 87],
[88, 89],
[90, 91],
[92, 93],
[94, 95],
[96, 97],
[98, 99]]])
NumPy 的便捷文件存取
- np.save(fname, array) 或np.savez(fname, array)
- fname : 文件名,以.npy为扩展名,压缩扩展名为.npz
- array : 数组变量
- np.load(fname)
- fname : 文件名,以.npy为扩展名,压缩扩展名为.npz
a = np.arange(100).reshape(5, 10, 2)
np.save('a.npy', a) # 存取
b = np.load('a.npy') # 读取
b
array([[[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11],
[12, 13],
[14, 15],
[16, 17],
[18, 19]],
[[20, 21],
[22, 23],
[24, 25],
[26, 27],
[28, 29],
[30, 31],
[32, 33],
[34, 35],
[36, 37],
[38, 39]],
[[40, 41],
[42, 43],
[44, 45],
[46, 47],
[48, 49],
[50, 51],
[52, 53],
[54, 55],
[56, 57],
[58, 59]],
[[60, 61],
[62, 63],
[64, 65],
[66, 67],
[68, 69],
[70, 71],
[72, 73],
[74, 75],
[76, 77],
[78, 79]],
[[80, 81],
[82, 83],
[84, 85],
[86, 87],
[88, 89],
[90, 91],
[92, 93],
[94, 95],
[96, 97],
[98, 99]]])
NumPy 随机数函数
**np.random.***
- np.random.rand()
- np.random.randn()
- np.random.randint()
rand(d0,d1,..,dn):根据d0‐dn创建随机数数组,浮点数,[0,1),均匀分布
randn(d0,d1,..,dn):根据d0‐dn创建随机数数组,标准正态分布
randint(low[,high,shape]):根据shape创建随机整数或整数数组,范围是[low, high)
a = np.random.rand(3, 4, 5)
a
array([[[0.00970598, 0.91426723, 0.94091358, 0.35995173, 0.73527167],
[0.67247714, 0.84621682, 0.06575003, 0.40443652, 0.55132703],
[0.55764129, 0.33425244, 0.36622444, 0.91013015, 0.01951838],
[0.9191911 , 0.11547889, 0.10495101, 0.85942447, 0.63077568]],
[[0.98473279, 0.97766314, 0.5897988 , 0.52280097, 0.00828881],
[0.25084206, 0.98663782, 0.06988935, 0.09980435, 0.50884526],
[0.26710583, 0.01625678, 0.10963428, 0.18005632, 0.68440569],
[0.34564166, 0.03708294, 0.53053936, 0.23928454, 0.23232696]],
[[0.42324316, 0.81030347, 0.95111069, 0.74277046, 0.74635378],
[0.19895444, 0.38080835, 0.22913251, 0.85612153, 0.21900946],
[0.32004786, 0.91132185, 0.16489621, 0.70907992, 0.58382586],
[0.85160099, 0.64809148, 0.85002856, 0.81320381, 0.33886516]]])
b = np.random.randn(3 ,4, 5)
b
array([[[-0.21348197, -0.44570241, -0.71621609, 1.85129229,
-0.44588806],
[ 1.47838512, -0.70666759, 0.87700215, 0.48592201,
0.79956022],
[ 2.13625401, -0.07079582, -0.17725274, 0.19694769,
2.05754363],
[-0.64257978, 0.87595102, 2.07479717, -0.74537305,
0.97901296]],
[[ 0.92565121, -0.38530396, 0.53915771, 1.19615551,
-0.48224399],
[-0.4333694 , 0.26592849, -0.24992116, 0.5544876 ,
1.55532195],
[-0.73849873, 0.5891183 , 1.99784687, -0.23486072,
-0.02866399],
[-1.38406207, 1.50134676, -0.60666506, -0.43052217,
-0.28625313]],
[[-2.21592009, 0.10519323, 0.78980958, -0.98435227,
0.36112141],
[ 0.6756284 , 1.50836044, 0.44943334, -0.8722511 ,
0.51363153],
[ 0.04066975, 0.15197328, 0.18679335, -0.16095451,
0.81433414],
[-1.2608254 , 0.40367403, 0.07805773, -1.08035367,
0.5968214 ]]])
c = np.random.randint(100, 200, (3, 4))
c
array([[197, 148, 131, 155],
[136, 113, 145, 127],
[146, 120, 142, 183]])
- shuffle(a) 根据数组a的第1轴进行随排列,改变数组x
- permutation(a) 根据数组a的第1轴产生一个新的乱序数组,不改变数组x
- choice(a[,size,replace,p]) 从一维数组a中以概率p抽取元素,形成size形状新数组
- replace表示是否可以重用元素,默认为False
- uniform(low,high,size) 产生具有均匀分布的数组,low起始值,high结束值,size形状
- normal(loc,scale,size) 产生具有正态分布的数组,loc均值,scale标准差,size形状
- poisson(lam,size) 产生具有泊松分布的数组,lam随机事件发生率,size形状
u = np.random.uniform(0, 10, (3, 4))
u
array([[4.94197895, 6.23998106, 7.84428825, 6.5761329 ],
[6.05642477, 4.84202956, 0.09009197, 7.00308733],
[9.67549623, 4.81499028, 5.95440908, 6.71349473]])
n = np.random.normal(10, 5, (3, 4))
n
array([[-1.25062946, 15.79000683, 7.86733604, 11.49315651],
[15.06321319, 10.87464604, 11.936292 , 8.8190334 ],
[ 1.70087674, 4.18490983, 20.2382347 , 9.15884821]])
NumPy 统计函数
**np.***
- sum(a, axis=None) 根据给定轴axis计算数组a相关元素之和,axis整数或元组
- mean(a, axis=None) 根据给定轴axis计算数组a相关元素的期望,axis整数或元组
- average(a,axis=None,weights=None) 根据给定轴axis计算数组a相关元素的加权平均值
- std(a, axis=None) 根据给定轴axis计算数组a相关元素的标准差
- var(a, axis=None) 根据给定轴axis计算数组a相关元素的方差
a = np.arange(15).reshape(3, 5)
a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
np.sum(a)
105
np.mean(a, axis=1)
array([ 2., 7., 12.])
np.mean(a, axis=0)
array([5., 6., 7., 8., 9.])
np.average(a, axis=0, weights=[1, 2, 3])
array([ 6.66666667, 7.66666667, 8.66666667, 9.66666667, 10.66666667])
np.std(a)
4.320493798938574
np.var(a)
18.666666666666668
- min(a) max(a) 计算数组a中元素的最小值、最大值
- argmin(a) argmax(a) 计算数组a中元素最小值、最大值的降一维后下标
- unravel_index(index, shape) 根据shape将一维下标index转换成多维下标
- ptp(a) 计算数组a中元素最大值与最小值的差
- median(a) 计算数组a中元素的中位数(中值)
b = np.arange(15, 0, -1).reshape(3, 5)
b
array([[15, 14, 13, 12, 11],
[10, 9, 8, 7, 6],
[ 5, 4, 3, 2, 1]])
np.min(b)
1
np.ptp(b)
14
np.median(b)
8.0
NumPy 梯度函数
- np.gradient(f) 计算数组f中元素的梯度,当f为多维时,返回每个维度梯度
a = np.random.randint(0, 20, (5))
a
array([ 4, 8, 18, 0, 6])
np.gradient(a)
array([ 4., 7., -4., -6., 6.])
c = np.random.randint(0, 50, (3, 5))
c
array([[ 7, 34, 41, 1, 17],
[47, 10, 48, 19, 17],
[ 3, 14, 28, 20, 16]])
np.gradient(c)
[array([[ 40. , -24. , 7. , 18. , 0. ],
[ -2. , -10. , -6.5, 9.5, -0.5],
[-44. , 4. , -20. , 1. , -1. ]]),
array([[ 27. , 17. , -16.5, -12. , 16. ],
[-37. , 0.5, 4.5, -15.5, -2. ],
[ 11. , 12.5, 3. , -6. , -4. ]])]
小结 2
实例1:图像的手绘效果
图像的数组表示
图像是一个三维数组,维度分别是高度、宽度和像素RGB值
PIL库(Python Image Library)
- 安装:pip install pillow
- from PIL import Image,Image是PIL库中代表一个图像的类(对象)
from PIL import Image
import numpy as np
im = np.array(Image.open("DV03-实例1-图像的手绘效果.jpg"))
print(im.shape, im.dtype)
(245, 488, 3) uint8
图像变换
a = np.array(Image.open("DV03-实例1-图像的手绘效果.jpg"))
# print(a.shape, a.dtype)
b = [255, 255 ,255] - a
im = Image.fromarray(b.astype('uint8'))
im.save("DV03-实例1-图像的手绘效果1.jpg")
a = np.array(Image.open("DV03-实例1-图像的手绘效果.jpg").convert('L'))
print(a.shape, a.dtype)
b = 255 - a
im = Image.fromarray(b.astype('uint8'))
im.save("DV03-实例1-图像的手绘效果2.jpg")
(245, 488) uint8
a = np.array(Image.open("DV03-实例1-图像的手绘效果.jpg").convert('L'))
c = (100/255) * a + 150 # 区间变换
im = Image.fromarray(c.astype('uint8'))
im.save("DV03-实例1-图像的手绘效果3.jpg")
a = np.array(Image.open("DV03-实例1-图像的手绘效果.jpg"))
d = 255 * (a / 255)**2 # 像素平方
im = Image.fromarray(d.astype('uint8'))
im.save("DV03-实例1-图像的手绘效果4.jpg")
文章评论