介绍
NumPy是Python中用于数值计算的核心包之一,它提供了大量的高效数组操作函数和数学函数。它支持多维数组和矩阵运算,并且可以集成C/C++和Fortran的代码,因此可以非常高效地处理大量数据。以下是NumPy的一些主要功能和用途:
1.多维数组:NumPy的核心是ndarray(N维数组)对象,可以用来存储同类型的元素。这些数组可以是一维、二维或更高维度。它们提供了方便的数组索引和切片,以及许多基本操作和计算(如加、减、乘、除、取幂等)。
2.数组操作:NumPy提供了大量的数组操作函数,包括数学函数(如三角函数、指数函数、对数函数等)、逻辑函数(如布尔运算、比较运算、逻辑运算等)、排序函数、统计函数等。
3.矩阵操作:NumPy提供了矩阵操作函数,如矩阵加、减、乘、转置、求逆等,可以方便地进行线性代数计算。
4.随机数生成:NumPy可以生成各种随机数,如正态分布、均匀分布、泊松分布、贝努利分布等,以及随机排列和随机选择等。
5.文件IO:NumPy可以读写各种文件格式,包括文本文件、二进制文件和matlab文件等,以便于数据的存储和传输。
6.与其他Python库的整合:NumPy可以方便地与其他Python库(如Pandas、SciPy、matplotlib等)整合使用,以进行数据分析、科学计算和可视化等。
1
|
import numpy as np |
Ndarray
1.ndarray的操作
将列表数据生成array()
1
|
a = np.array([ 1 , 2 , 3 , 4 , 5 ]) |
确认数据类型
1
|
print (a.dtype) # int32 |
如果将浮点数代入整数型数组里,数据自动变为整数型(小数点以下自动舍掉)
1
2
|
a[ 1 ] = - 3.6 print (a) # [1 -3 3 4 5] |
变换数据类型
1
2
|
a2 = a.astype(np.float32) print (a2, a2.dtype) # [1. -3. 3. 4. 5.] float32 |
二维数组
1
2
3
4
|
b = np.array([[ 1 , 2 , 3 ], [ 3.2 , 5.3 , 6.6 ]]) print ( 'b=' , b) # b= [[1. 2. 3. ][3.2 5.3 6.6]] print ( 'b[1,2]=' , b[ 1 , 2 ]) # b[1,2] = 6.6 |
2.ndarray的参数
- ndarry.ndim 数组的维数
- ndarry.shape 数组的行列数
- ndarry.size 元素的个数
- ndarry.dtype 数据的类别
1
2
3
4
5
6
7
8
9
|
print ( 'ndim =' , a.ndim, b.ndim) print ( 'shape =' , a.shape, b.shape) print ( 'size =' , a.size, b.size) print ( 'dtype =' , a.dtype, b.dtype) # ndim = 1 2 # shape = (5,) (2, 3) # size = 5 6 # dtype = float32 float64 |
reshape进行数组重组(元素数不变)
1
2
3
|
print (b.reshape( 6 )) # 转为1维数组 [ 1. 2. -1.1 3.2 5.3 6.6] print (b.reshape( 3 , 2 )) # 转为3行2列数组 [[ 1. 2. ][-1.1 3.2][ 5.3 6.6]] print (b.T) # 矩阵的转置 [[ 1. 3.2][ 2. 5.3][-1.1 6.6]] |
矩阵的计算
矩阵与数值的四则运算时,各个值都进行运算
1
2
3
4
5
6
|
print (b + 2 ) #[[3. 4. 0.9][5.2 7.3 8.6]] print (b - 2 ) #[[-1. 0. -3.1][ 1.2 3.3 4.6]] print (b * 2 ) #[[ 2. 4. -2.2][ 6.4 10.6 13.2]] print (b / 2 ) #[[ 0.5 1. -0.55][ 1.6 2.65 3.3 ]] print (b * * 3 ) #3次幂 [[ 1. 8. -1.331][ 32.768 148.877 287.496]] print (b / / 1 ) #用这种方法舍掉小数 [[ 1. 2. -2.][ 3. 5. 6.]] |
同维度矩阵计算时,相同位置的值进行运算(矩阵维度不同时报错)
1
2
3
4
5
|
c = b / 2 print (b + c) # [[ 1.5 3. -1.65][ 4.8 7.95 9.9 ]] print (b - c) # [[ 0.5 1. -0.55][ 1.6 2.65 3.3 ]] print (b * c) # [[ 0.5 2. 0.605][ 5.12 14.045 21.78 ]] print (b / c) # [[2. 2. 2.][2. 2. 2.]] |
行列の積は「@」
a行b列×b行c列=a行c列の行列
1
2
3
4
5
|
A = np.arange( 6 ).reshape( 3 , 2 ) B = np.arange( 8 ).reshape( 2 , 4 ) print (A) #[[0 1][2 3][4 5]] print (B) #[[0 1 2 3][4 5 6 7]] print (A@B) #[[ 4 5 6 7][12 17 22 27][20 29 38 47]] |
矩阵的生成
1维矩阵的生成(初始值,终止值,条件)
- arange 条件为指定步长,总数自动决定,不包括终止值
- linspace 条件为总数,步长自动决定,包括终止值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
np.arange( 0 , 10 , 0.1 ) # array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 1.1, 1.2, # 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4, 2.5, # 2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, # 3.9, 4. , 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. , 5.1, # 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 6. , 6.1, 6.2, 6.3, 6.4, # 6.5, 6.6, 6.7, 6.8, 6.9, 7. , 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7, # 7.8, 7.9, 8. , 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, 8.8, 8.9, 9. , # 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9]) np.linspace( 0 , 10 , 100 ) # array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , # 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, # 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2, # 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4. , 4.1, 4.2, 4.3, # 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. , 5.1, 5.2, 5.3, 5.4, # 5.5, 5.6, 5.7, 5.8, 5.9, 6. , 6.1, 6.2, 6.3, 6.4, 6.5, # 6.6, 6.7, 6.8, 6.9, 7. , 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, # 7.7, 7.8, 7.9, 8. , 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, # 8.8, 8.9, 9. , 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, # 9.9, 10. ]) |
多维矩阵
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
|
np.zeros(( 3 , 2 )) #array([[0., 0.], # [0., 0.], # [0., 0.]]) np.ones(( 5 , 2 , 3 ), dtype = np.int16) # 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]], # # [[1, 1, 1], # [1, 1, 1]]], dtype=int16) print (np.ones(( 5 , 2 , 2 )) * 128 ) [[[ 128. 128. ] # [128. 128.]] # # [[128. 128.] # [128. 128.]] # # [[128. 128.] # [128. 128.]] # # [[128. 128.] # [128. 128.]] # # [[128. 128.] # [128. 128.]]] |
3.例子
生成随机二维数组
1
2
3
4
5
6
7
8
|
rnd = np.random.random(( 5 , 5 )) print (rnd) # [[0.61467866 0.38383428 0.4604147 0.41355961 0.22680966] # [0.83895625 0.49135984 0.21811832 0.91433166 0.18616649] # [0.80176894 0.23622139 0.87041535 0.59623534 0.93986178] # [0.48324671 0.62398314 0.82435621 0.92421743 0.84660406] # [0.63578052 0.99794079 0.46970418 0.85743179 0.11774799]] |
生成图像
1
2
|
plt.imshow(rnd, cmap = 'gray' ) plt.colorbar() #0为黑色,1为白色 |
1
2
|
plt.imshow(rnd> 0.5 , cmap = 'gray' ) plt.colorbar() |
1
2
3
4
5
6
7
8
9
10
11
12
|
color_img = np.array([ [[ 255 , 0 , 0 ], [ 0 , 255 , 0 ], [ 0 , 0 , 255 ]], [[ 255 , 255 , 0 ], [ 0 , 255 , 255 ], [ 255 , 0 , 255 ]], [[ 255 , 255 , 255 ], [ 128 , 128 , 128 ], [ 0 , 0 , 0 ]], ]) plt.imshow(color_img) |
到此这篇关于使用NumPy进行数组数据处理的示例详解的文章就介绍到这了,更多相关NumPy数组数据处理内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_49828565/article/details/129706932