文章目录
常用操作一览表
操作 | 描述 |
---|---|
np.array() | 从列表或元组创建数组。 |
np.zeros() | 创建全零数组。 |
np.ones() | 创建全一数组。 |
np.arange() | 创建一个范围数组。 |
np.linspace() | 创建等间隔的数组。 |
np.random.rand() | 创建指定形状的随机数组。 |
属性 | |
ndarray.shape | 数组的维度。 |
ndarray.size | 数组中元素的总数。 |
ndarray.dtype | 数组中元素的数据类型。 |
数组查找 | |
np.argmin() | 查找数组中最小值的索引。 |
数组操作 | |
np.vstack() | 沿着垂直方向堆叠数组。 |
np.hstack() | 沿着水平方向堆叠数组。 |
数组重塑和转置 | |
ndarray.reshape() | 重塑数组的形状。 |
ndarray.T | 数组的转置。 |
数组排序 | |
np.sort() | 对数组进行排序。 |
数组索引和迭代 | |
数组切片 | |
布尔索引和条件筛选 | |
数组拼接和分割 | |
np.concatenate() | 沿指定轴拼接数组。 |
np.split() | 将数组沿指定轴分割。 |
数组形状操作 | |
np.flatten() | 将多维数组转换为一维数组。 |
np.squeeze() | 移除数组中的单维度条目。 |
广播 | 自动处理不同形状数组之间的运算。 |
生成对角线矩阵 | Z = np.eye(100) |
将对角线以下元素设为0 | indices = np.tril_indices_from(Z, k=-1) Z[indices] = 0 |
获取对角线元素 | diagonal = np.diag(Z) |
获取对角线以上的元素 | above_diagonal = np.triu(Z, k=1) |
获取对角线以下的元素 | below_diagonal = np.tril(Z, k=-1) |
矩阵乘法 | result = np.dot(Z, A) |
计算行列式 | determinant = np.prod(diagonal) |
生成数组
- 列表转为np.array
import numpy as np
np1 = np.array([0,1,2,3,4,5,6,7,8,9])
np1
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np1.shape
(10,)
- arange(array range数组范围)
np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(0,10,2)
array([0, 2, 4, 6, 8])
- zero
np.zeros(10)#生成10维0"列向量"
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
np.zeros((2,10))
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
- full
np.full((10),6)
array([6, 6, 6, 6, 6, 6, 6, 6, 6, 6])
np.full((10),0)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
np.full((2,10),0)
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
数组切片
- 一维数组切片与列表类似
np1 = np.array([0,1,2,3,4,5,6,7,8,9])
np1[1:5]
array([1, 2, 3, 4])
np1[3:]
array([3, 4, 5, 6, 7, 8, 9])
np1[-3:-1]
array([7, 8])
np1[1:5:2]
array([1, 3])
np1[::2]
array([0, 2, 4, 6, 8])
- 二维切片
np1 = np.array([[1,2,3,4,5],
[6,7,8,9,10]])
np1[1,2]
8
np1[0,1:3]
array([2, 3])
np1[:,1:3]
array([[2, 3],
[7, 8]])
常用函数
np1 = np.arange(10)
np1
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
- 平方根
np.sqrt(np1)
array([0. , 1. , 1.41421356, 1.73205081, 2. ,
2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])
- 绝对值
np1 = np.arange(-5,6)
np1
array([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5])
np.abs(np1)
array([5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5])
- 指数
np1 = np.arange(10)
np1
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.exp(np1)
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])
- 最大值\最小值
np1 = np.arange(10)
np.max(np1)
9
- 符号函数
np1 = np.arange(-5,6)
np.sign(np1)
array([-1, -1, -1, -1, -1, 0, 1, 1, 1, 1, 1])
- 三角函数
np.sin(np1)
array([ 0.95892427, 0.7568025 , -0.14112001, -0.90929743, -0.84147098,
0. , 0.84147098, 0.90929743, 0.14112001, -0.7568025 ,
-0.95892427])
- 对数
np1 = np.arange(1,10)
np.log(np1)
array([0. , 0.69314718, 1.09861229, 1.38629436, 1.60943791,
1.79175947, 1.94591015, 2.07944154, 2.19722458])
copy or view
np1 = np.arange(10)
np1
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np2 = np1.view()
np2
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np1[1] = 100
np1
array([ 0, 100, 2, 3, 4, 5, 6, 7, 8, 9])
np2
array([ 0, 100, 2, 3, 4, 5, 6, 7, 8, 9])
np2[1] =1000
np1
array([ 0, 1000, 2, 3, 4, 5, 6, 7, 8, 9])
view():改变原变量,副本也会变化;改变副本原变量也会变化
copy():相反,副本不会变化,因为两个变量完全独立
shape reshape
- reshape 2D
np1 = np.arange(1,13)
np1
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
np1.reshape(3,4)
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
np2 = np.array([
[1,2,3,4,5,6],
[7,8,9,10,11,12]
])
np2.reshape(3,4)
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
np1 与np2 reshape得到结果一样
- reshape 3D
np3 = np1.reshape(2,3,2)
np3
array([[[ 1, 2],
[ 3, 4],
[ 5, 6]],
[[ 7, 8],
[ 9, 10],
[11, 12]]])
- flateen to 1D
np4 = np3.reshape(-1)
np4
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
np4.shape
(12,)
遍历数组
- 1维
np1 = np.arange(1,11)
for i in np1:
print(i)
1
2
3
4
5
6
7
8
9
10
- 2维
np2 = np.array(np.arange(1,13).reshape(2,3,2))
np2
array([[[ 1, 2],
[ 3, 4],
[ 5, 6]],
[[ 7, 8],
[ 9, 10],
[11, 12]]])
for x in np2:
for y in x:
for z in y:
print(z)
1
2
3
4
5
6
7
8
9
10
11
12
- 3维
np3 = np.arange(1,13).reshape(4,3)
np3
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
for x in np3:
for j in x:
print(j)
1
2
3
4
5
6
7
8
9
10
11
12
这种方法太原始,下面使用np.nditer
nditer是"n-dimensional iterator"
for x in np.nditer(np3):
print(x)
1
2
3
4
5
6
7
8
9
10
11
12
sort
- 1维数
np1 = np.array([2,6,7,3,2,5,6])
np.sort(np1)
array([2, 2, 3, 5, 6, 6, 7])
np2 = np.array(['Join','Alo','Bob','Cresi'])
print(np.sort(np2))
['Alo' 'Bob' 'Cresi' 'Join']
print(np.sort(np.array([True,False,False,True])))
[False False True True]
因为False 是0 True 是1
- 2维
np.sort(np.array([[6,2,3,1],
[7,4,5,6]]))
array([[1, 2, 3, 6],
[4, 5, 6, 7]])
查找元素位置
- where
np1 = np.array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
print(np1)
print('----------------------------')
np.where(np1==10)
[ 1 2 3 4 5 6 7 8 9 10 11 12]
----------------------------
(array([9], dtype=int64),)
np.where(np1%2==1)
(array([ 0, 2, 4, 6, 8, 10], dtype=int64),)
Filter
np1 = np.array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
filt = [True,True,False,False,False,False,False,False,False,False]
np1[x]
array([1, 2])
- 查找偶数
filt = []
for thing in np1:
if thing % 2 == 0:
filt.append(True)
else:
filt.append(False)
filt
[False, True, False, True, False, True, False, True, False, True]
np1[filt]
array([ 2, 4, 6, 8, 10])
更快捷方式
np1 % 2 == 0
array([False, True, False, True, False, True, False, True, False,
True])
filt = (np1 % 2 == 0)
np1[filt]
array([ 2, 4, 6, 8, 10])
- 小节
数组可直接进行线性运算
判断语句时是数组中的每个元素一一去比对
数组运算
np1 ** 2
array([ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100], dtype=int32)
np1 + 1
array([ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
- 条件判断
np.array(['a','b']) == 'a'
array([ True, False])
- 加法运算
x = np.array([1,2,4,8])
y = np.array([2,2,2,2])
x + y
array([ 3, 4, 6, 10])
- 乘法运算(对应位置相乘)
x * y
array([ 2, 4, 8, 16])
- 幂运算
x ** y
array([ 1, 4, 16, 64], dtype=int32)
- 广播机制
a = np.arange(3).reshape(3,1)
b = np.arange(2).reshape(1,2)
a,b
(array([[0],
[1],
[2]]),
array([[0, 1]]))
a + b
array([[0, 1],
[1, 2],
[2, 3]])
np.argmax
import numpy as np
# 创建一个二维数组
arr = np.array([[1, 2, 3],
[4, 100, 6],
[7, 50, 9]])
# 在整个数组中找到最大值的索引
max_index = np.argmax(arr)
print("最大值的索引:", max_index)
# 沿着每一列找到最大值的索引
max_indices_col = np.argmax(arr, axis=0)
print("每一列最大值的索引:", max_indices_col)
# 沿着每一行找到最大值的索引
max_indices_row = np.argmax(arr, axis=1)
print("每一行最大值的索引:", max_indices_row)
```python
最大值的索引: 4
每一列最大值的索引: [2 1 2]
每一行最大值的索引: [2 1 1]
np.empty与np.zeros
在NumPy中,np.empty() 和 np.zeros() 函数都用于创建指定形状的数组,但它们有一些区别:
np.empty()
函数创建一个指定形状的数组,但不初始化数组元素的值。它只分配内存空间,因此返回的数组中的元素值可能是任意的,取决于数组原先在内存中的状态。
因此,使用 np.empty() 创建的数组的元素值是未定义的,可能包含任意的数值(通常情况下是垃圾值
)。
由于不进行初始化,np.empty() 的执行速度比 np.zeros()快
,因为后者需要将数组的所有元素初始化为零。
np.zeros()
函数创建一个指定形状的数组,并将所有元素的值初始化为零。
返回的数组中的所有元素都是零。
import numpy as np
# 使用 np.empty() 创建一个形状为 (2, 3) 的数组
arr_empty = np.empty((2, 3))
print("使用 np.empty() 创建的数组:")
print(arr_empty)
# 使用 np.zeros() 创建一个形状为 (2, 3) 的数组
arr_zeros = np.zeros((2, 3))
print("\n使用 np.zeros() 创建的数组:")
print(arr_zeros)
使用 np.empty() 创建的数组:
[[6.90827581e-310 4.65913659e-310 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000]]
使用 np.zeros() 创建的数组:
[[0. 0. 0.]
[0. 0. 0.]]
(df[‘label1’] == df[‘label2’]用法(astype、sum)
df['label_1'] == df['label_2']
0 True
1 False
2 False
3 False
4 False
dtype: bool
(df['label_1'] == df['label_2']).astype(int)
0 1
1 0
2 0
3 0
4 0
dtype: int32
sum((df['label_1'] == df['label_2']))
1
生成坐标数组
import numpy as np
x = np.array([1, 2, 3, 4]) # 一维数组
y = np.array([5, 6, 7]) # 一维数组
X, Y = np.meshgrid(x, y)
print('X:\n',X,'\n','Y:\n',Y)
grid = np.c_[X.ravel(), Y.ravel()]
print(grid)
X:
[[1 2 3 4]
[1 2 3 4]
[1 2 3 4]]
Y:
[[5 5 5 5]
[6 6 6 6]
[7 7 7 7]]
[[1 5]
[2 5]
[3 5]
[4 5]
[1 6]
[2 6]
[3 6]
[4 6]
[1 7]
[2 7]
[3 7]
[4 7]]
np.sum与np.nansum
import numpy as np
data = np.array([1, 2, np.nan, 4])
7.0
np.nansum(data)
7.0
np.sum(data)
nan
np.where
import numpy as np
# 示例数组
arr = np.array([1, 2, 3, 4, 5])
np.where(arr>3)
(array([3, 4], dtype=int64),)
arr[np.where(arr>3)]
array([4, 5])
np.where(arr>3,arr,'')
array(['', '', '', '4', '5'], dtype='<U11')
np.where有点类似df中的filter
np.where用于np.array
filter用于df