数据分析之numpy常用方法【获取最大、小值、数组拼接】

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]]
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值