import numpy as np
a = np.array([2,23,4],dtype=np.int)
print(a.dtype)
a = np.array([[2,3,4],
[2,3,4]])
# 形状; 数据类型
a = np.zeros((3,4), dtype=float)
# 起始; 结束; 步长
a = np.arange(10,20,2)
# 起始; 结束; 份数
a = np.linspace(1,10,5)
# 改变数组形状
a = np.array([[2,3,4],
[2,3,4]]).reshape(2,3)
import numpy as np
a = np.array([10,20,30,40])
b = np.arange(4)
# print(a,b)
# 加减乘除,乘方,函数
# 均为逐个计算
c = a + b
# a是[10 20 30 40], b是[0 1 2 3], c是[10+0 20+2 30+3 40+3]
c = a - b
# out: [10 19 28 37]
c = a * b
# out: [0 20 60 120]
c = a / b
# out: cannot be devided by zero
c = b ** 2
# out: [0 1 4 9]
c = np.sin(a)
# out: [-0.54402111 0.91294525 -0.98803162 0.74511316]
# 比较
print(b < 3)
# out: [ True True True False]
# reshape
a = np.array([[1,1],
[0,1]])
b = np.arange(4).reshape((2,2))
# out: [[0 1]
# [2 3]]
# 矩阵乘法
c = np.dot(a,b)
c = a.dot(b)
# out: [[2 4]
# [2 3]]
# 随机的array
a = np.random.random((2,4))
print(a)
# out: [[0.05339648 0.84098481 0.34542441 0.30252523]
# [0.80463402 0.84972925 0.73973259 0.95150856]]
# 最大值最小值求和, axis=1时对第二维度操作,axis=0对第一维度操作,axis=-1时对最后一个维度操作
print(np.max(a))
print(np.min(a))
print(np.sum(a, axis=1))
# 最大值最小值对应的
print(np.argmin(A))
print(np.argmax(A))
# out: 0
# out: 11
# 平均值
print(np.mean(A))
print(A.mean())
print(np.average(A))
# out: 7.5
# out: 7.5
# out: 7.5
# 累加,每一位都是前面几项的加和
print(np.cumsum(A))
# out: [ 2 5 9 14 20 27 35 44 54 65 77 90]
# 累差每一项减去前一项
print(np.diff(A))
# out: [[1 1 1]
# [1 1 1]
# [1 1 1]]
# 求非零元素
print(np.nonzero(A))
# out: (array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2]), array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]))
# 第一个是不是零的元素的行数,第二个是不是零的元素的列数
# 逐行排序
print(np.sort(A))
# out: A排好序了,就是A
# 矩阵的转置
print(np.transpose(A))
print(A.T)
# out: [[ 2 6 10]
# [ 3 7 11]
# [ 4 8 12]
# [ 5 9 13]]
# 限定范围
print(np.clip(A, 5, 9))
# out: [[5 5 5 5]
# [6 7 8 9]
# [9 9 9 9]]
原文:https://www.cnblogs.com/zyl-hub/p/12811200.html