numpy

np.random.choice

"""
numpy.random.normal(loc=0.0, scale=1.0, size=None) 生成服从正态分布的随机量

loc : 均值 float or array_like of floats
Mean (“centre”) of the distribution.

scale : 标准差 float or array_like of floats
Standard deviation (spread or “width”) of the distribution. Must be non-negative.

size : Output形状 int or tuple of ints, optional
Output shape.

"""
# np.random.choice(a, size=None, replace=True, p=None)
# m:选择数的范围, size:选取数的规模,  replace:是否重用, p:每个数被选的概率
m = 5
a = np.random.choice(m, size=3, replace=False)  
print(a)
b = np.random.choice(a, size=5, replace=True, p=[0.1, 0.3, 0.6])
print(b)

np.bincount

# np.bincount(x, weights=None, minlength=0) 
#             weights:权重,与x的shape一样 out[n]+=weight[i],  
#             minlength:输出的最小值
#             输出长度:输入矩阵最大值+1, np.amax(x)+1
hist = np.bincount(b)
print(hist)
hist = np.bincount(b, weights=[1,3,1,2,1])
print(hist)

np.ndarray.flatten

a = np.array([[1, 2, 3, 4, 3, 1], [3, 2, 3, 4, 3, 5]]) # 展为1-D
a_fla_c = a.flatten('C')  # row-major 行优先
print(a_fla_c) # [1 2 3 4 3 1 3 2 3 4 3 5]
a_fla_f = a.flatten('F')  # column-major 列优先
print(a_fla_f) # [1 3 2 2 3 3 4 4 3 3 1 5]
a_fla_a = a.flatten('A')
print(a_fla_a) # [1 2 3 4 3 1 3 2 3 4 3 5]
a_fla_k = a.flatten('K')
print(a_fla_k) # [1 2 3 4 3 1 3 2 3 4 3 5]

np.unique

a = np.array([[5, 2, 3, 4, 3, 1], [3, 2, 3, 4, 3, 5]])  # 先flatten为1-D
a = a.flatten('C') 
b = np.unique(a) # 构建unique的array
print(a)
print(b) # 自动按升序排列
"""
[5 2 3 4 3 1 3 2 3 4 3 5]
[1 2 3 4 5]
"""
b, indices = np.unique(a, return_index=True)
print(b)
print(indices)  # indices: 从a构建b
print(a[indices])
"""
[1 2 3 4 5]
[5 1 2 3 0]
[1 2 3 4 5]
"""
b, indices = np.unique(a, return_inverse=True)  
print(b)
print(indices)  # indices: 从b构建a
print(b[indices])
"""
[1 2 3 4 5]
[4 1 2 3 2 0 2 1 2 3 2 4]
[5 2 3 4 3 1 3 2 3 4 3 5]
"""

np.concatenate

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
print(np.concatenate((a, b), axis=0))   # 按行合并
print(np.concatenate((a, b.T), axis=1)) # 按列合并
print(np.concatenate((a, b), axis=None))# flatten
"""
[[1 2]
 [3 4]
 [5 6]]
[[1 2 5]
 [3 4 6]]
[1 2 3 4 5 6]
"""
np.linalg (linear algebra)

dot

a = np.array([[1, 2], [3, 4], [5, 6]]) 
b = np.array([[1], [2]])
print(np.dot(a, b))     # 矩阵点乘   2-D * 2-D
"""
[[ 5]
 [11]
 [17]]
"""
# print(a@b)
# print(np.matmul(a, b))

a = np.array([1, 2, 3])  # 向量点乘  1-D * 1-D
b = np.array([3, 4, 5])
print(np.dot(a, b))
# 26

vdot

a = np.array([1+2j,3+4j])
b = np.array([5+6j,7+8j])
print(np.vdot(a, b))  # (70-8j)

# flattens input arguments to 1-D vectors first
a = np.array([[1, 4], [5, 6]])
b = np.array([[4, 1], [2, 2]])
print(np.vdot(a, b)) # 30

inner

a = np.array([[1, 2, 3],[2, 3, 4]])
b = np.array([2, 3, 4])
print(np.inner(a, b))  # [20 29]

inv 矩阵的逆(inverse)

a = np.array([[1, 2], [3, 4]])
b = np.linalg.inv(a)
print(b)
print(np.dot(a, b))
"""
[[-2.   1. ]
 [ 1.5 -0.5]]
[[1.00000000e+00 1.11022302e-16]
 [0.00000000e+00 1.00000000e+00]]
"""
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值