Numpy数组操作

数组转置

import numpy as np

a = np.arange(6).reshape(2, 3)
a1 = np.transpose(a)             # 转置,返回数组视图
a2 = a.T                         # 转置,返回数组视图
a[0, 0] = 100
print(a)
print(a1)
print(a2)

数组拼接

a = np.array([1, 1, 1])
b = np.hstack((a, a))  # 水平拼接,参数为元组
c = np.vstack((a, a))  # 垂直拼接
print(b, "\n\n", c)
print()

a = np.array([[1, 1, 1], [5, 5, 5]])
b = np.hstack((a, a))  # 水平拼接
c = np.vstack((a, a))  # 垂直拼接
print(b, "\n\n", c)
import numpy as np

a = np.array([1, 2, 3])
b = np.c_[a, a, a]       # 不管对象是否具有列维(axis=1),沿列(axis=1)拼接,中括号
c = np.r_[a, a, a]       # 沿行(axis=0)拼接
print(b, "\n\n", c)
print()

a = np.array([[1, 2, 3]])
b = np.c_[a, a, a]
c = np.r_[a, a, a]
print(b, "\n\n", c)

检查真值

# np.any用于检查数组是否具有真值,只要有一个真值则返回True

import numpy as np  

a = np.array([[1, 1, 1], [0, 0, 0]])  
result = np.any(a)               # 数组有一个真值返回True,返回单一布尔值
result1 = np.any(a > 0)          # 检查是否有大于0的数组元素
print(result)  
print(result1)

result2 = np.any(a, axis=0)      # 返回布尔数组
result3 = np.any(a > 0, axis=1)
print(result2)
print(result3)
# np.all用于检查数组是否全为真值,只有全为真值才返回True

import numpy as np  

a = np.array([[1, 1, 1], [0, 0, 0]])
a1 = np.array([1, 5])  
result = np.all(a)         
result1 = np.all(a1)
result2 = np.all(a > 0)         # 检查数组元素是否全大于0
print(result)  
print(result1)
print(result2)

result4 = np.all(a, axis=0)
result5 = np.all(a, axis=1)
print(result4)
print(result5)

查找元素

where按条件查找

# 按条件查找,条件为布尔数组

import numpy as np

a = np.arange(6).reshape((2, 3))
print(a)
index = np.where(a > 3)  # 返回符合条件的元素的索引,返回元组类型(行索引,列索引)
print(index)
index1 = np.where(a == 3)
print(index1)
# 按条件修改数组元素

import numpy as np

a = np.arange(6).reshape((2, 3))
b = np.where(a > 3)           # 返回tuple
c = np.where(a > 2, a**2, 1)  # 返回ndarray,np.where(condition, x, y),参数x和y用于指定返回值
                              # 满足条件返回x,不满足返回y。
print(a,"\n")
print(b, type(b))
print(c, type(c))

# np.where修改元素值等同于以下操作
a = np.arange(6).reshape((2, 3))
a[a > 2] = a[a > 2] ** 2
a[a <= 2] = 1
print(a)

nonzero查找非零元素

import numpy as np

a = np.array([[1, 0], [0, 1]])
indices = np.nonzero(a)                      # 返回非零元素的索引,返回元组类型(行索引,列索引)
print(indices)
indices = list(zip(indices[0], indices[1]))  # 合并行列索引
print(indices)

查找最值

import numpy as np

a = np.array([[5, 3], [1, 2], [9, 10]])
a_min = np.min(a)                                          # 返回最小值
a_max = np.max(a, axis=1)                                  # 返回每行的最大值
min_index = np.argmin(a, axis = None)                      # 如果不指定轴,按一维数组下标返回索引
max_index = np.argmax(a, axis = 1)                         # 返回每行中最大值的行索引
min_index_nd = np.unravel_index(min_index, shape=a.shape)  # 把一维数组的索引按指定数组形状恢复为行列索引
indices = list(zip(range(3), max_index))                   # 恢复行列索引
print('a_min:', a_min, 'a_max:', a_max)
print('min_index:', min_index, type(min_index))
print('max_index:', max_index, type(max_index))
print('min_index_nd:', min_index_nd)
print('indices:', indices)
  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值