这里写目录标题
1. np.argmax()获取最大值索引
1.1 一维数组
a = [7, 2, 9, 4, 5]
arr_a = np.array(a)
b = np.array([[12, 14, 15], [10, 20, 30], [9, 7, 4]])
max_a_index = np.argmax(arr_a)
print(max_a_index)
结果输出
2
1.2 二维数组
- np.argmax()方法中存在一个
a
x
i
s
=
axis=
axis= 参数
- 当没有对该参数赋值时,是为一维数组进行比较。
- axis=0 时,表示对每一列进行比较,获取每一列最大值的索引
- axis=1 时,表示对每一行进行比较,获取每一行最大值的索引
b = np.array([[12, 14, 15], [13, 11, 4], [10, 20, 30], [9, 7, 4]])
max_b_index = np.argmax(b)
max_b2_index = np.argmax(b, axis=0)
max_b3_index = np.argmax(b, axis=1)
print(b)
print("*"*20)
print(max_b_index)
print(max_b2_index)
print(max_b3_index)
结果输出
[[12 14 15]
[13 11 4]
[10 20 30]
[ 9 7 4]]
********************
8
[1 2 2]
[2 0 2 0]
当然,我们还可以或者三维数组的最大值。因为不太常用,我们在这里我们就不再一一讲解。同理,我们还可以利用 np.argmin() 获取数组的最小值.在这里我们依旧略过。
2. max()获取最大值
b = np.array([[12, 14, 15], [13, 11, 4], [10, 20, 30], [9, 7, 4]])
print(b)
print("-------------")
print(b.max())
print(b.max(axis=0)) # 每行进行对比,获取每列的最大值
print(b.max(axis=1)) # # 每列进行对比,获取每行的最大值
输出:
[[12 14 15]
[13 11 4]
[10 20 30]
[ 9 7 4]]
-------------
30
[13 20 30]
[15 13 30 9]
3. Numpy中常用的统计函数
4. Numpy中的数组拼接
方法 | 要点 |
---|---|
concatenate() | 能够一次完成多个数组的拼接,适合大规模的数据拼接 |
append() | 最常见的方法,意为追加的意思。默认将数组ravel扁平化,返回的是一个1维数组 |
column_stack | 按行进行拼接 |
row_stack | 按列进行拼接 |
stack | 用于生成新的维度 |
hstack | 按行进行拼接 |
vstack | 按列进行拼接 |
dstack | 沿着第三个轴进行拼接 |
c_ | 按列进行拼接,适用于小型数据拼接 |
r_ | 按行进行拼接,适用于小型数据拼接 |
4.1 concatenate()
- axis=0时,按行拼接
- axis=1时,按列拼接
- axis参数默认为 0
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([[11, 21, 31], [7, 8, 9]])
c = np.array([[10, 12, 13], [14, 15, 16]])
new_arr1 = np.concatenate((a, b, c), axis=0)
new_arr2 = np.concatenate((a, b, c), axis=1)
new_arr3 = np.concatenate((a, b, c))
print(new_arr1)
print("-" * 20)
print(new_arr2)
[[ 1 2 3]
[ 4 5 6]
[11 21 31]
[ 7 8 9]
[10 12 13]
[14 15 16]]
--------------------
[[ 1 2 3 11 21 31 10 12 13]
[ 4 5 6 7 8 9 14 15 16]]
4.2 append()
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([[7, 8, 9], [11, 16, 18]])
c = np.array([[10, 12, 13], [14, 15, 16]])
new_arr1 = np.append(a, b)
new_arr2 = np.append(a, b, axis=0)
new_arr3 = np.append(a, b, axis=1)
print(new_arr1)
print("-" * 20)
print(new_arr2)
print("-" * 20)
print(new_arr3)
[ 1 2 3 4 5 6 7 8 9 11 16 18]
--------------------
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[11 16 18]]
--------------------
[[ 1 2 3 7 8 9]
[ 4 5 6 11 16 18]]
4.3 其他
对于其他方法都大同小异,下面我们就不一一进行讲解了,直接上代码
- 按列拼接
# 按列拼接
temp1 = np.concatenate((a, c), axis=1)
temp2 = np.append(a, c, axis=1)
temp3 = np.hstack((a, c))
temp4 = np.column_stack((a, c))
temp5 = np.c_[a, c]
print(temp1)
print("-" * 20)
print(temp2)
print("-" * 20)
print(temp3)
print("-" * 20)
print(temp4)
print("-" * 20)
print(temp5)
[[ 1 2 3 10 12 13]
[ 4 5 6 14 15 16]]
--------------------
[[ 1 2 3 10 12 13]
[ 4 5 6 14 15 16]]
--------------------
[[ 1 2 3 10 12 13]
[ 4 5 6 14 15 16]]
--------------------
[[ 1 2 3 10 12 13]
[ 4 5 6 14 15 16]]
--------------------
[[ 1 2 3 10 12 13]
[ 4 5 6 14 15 16]]
- 按行拼接
# 按行拼接
temp1 = np.concatenate((a, c), axis=0)
temp2 = np.append(a, c, axis=0)
temp3 = np.vstack((a, c))
temp4 = np.row_stack((a, c))
temp5 = np.r_[a, c]
print(temp1)
print("-" * 20)
print(temp2)
print("-" * 20)
print(temp3)
print("-" * 20)
print(temp4)
[[ 1 2 3]
[ 4 5 6]
[10 12 13]
[14 15 16]]
--------------------
[[ 1 2 3]
[ 4 5 6]
[10 12 13]
[14 15 16]]
--------------------
[[ 1 2 3]
[ 4 5 6]
[10 12 13]
[14 15 16]]
--------------------
[[ 1 2 3]
[ 4 5 6]
[10 12 13]
[14 15 16]]