print(np.abs(ar1))
print(np.fabs(ar2))
[4 3 2 1 0 1 2 3]
********************
[[1. 2. 3.]
[2. 3. 4.]]
平方根
print(np.sqrt(ar3))
[1. 1.41421356 1.73205081 2. 2.23606798]
取整
ar4=np.array([[-1.2,-4.6],[3.1,1.8]])
print(np.ceil(ar4))
out:
[[-1. -4.]
[ 4. 2.]]
********************
print(np.floor(ar4))
out:
[[-2. -5.]
[ 3. 1.]]
********************
print(np.rint(ar4))
out:
[[-1. -5.]
[ 3. 2.]]
********************
print(np.round(ar4))
out:
[[-1. -5.]
[ 3. 2.]]
********************
print(np.fix(ar4))
out:
[[-1. -4.]
[ 3. 1.]]
整数部分与小数部分分开
a,b=np.modf(ar4)
print(a)
print('*'*20)
print(b)
[[-0.2 -0.6]
[ 0.1 0.8]]
********************
[[-1. -4.]
[ 3. 1.]]
c1=np.array([[1,2,3,4],[5,6,7,8]])
c2=np.array([2,3,3,2])
print(np.power(c1,c2))
[[ 1 8 27 16]
[ 25 216 343 64]]
对比两个数组相同位置,输出最大/最小值(maxinum、mininum)(fmax/fmin : 将忽略nan)
c3=np.array([[1.2,4.6],[3,5.6]])
c4=np.array([[1.8,4.3],[3,5.8]])
print(np.maximum(c3,c4))
print('*'*20)
print(np.minimum(c3,c4))
[[1.8 4.6]
[3. 5.8]]
********************
[[1.2 4.3]
[3. 5.6]]
比较运算
print(np.greater(c3,c4))
print('*'*20)
print(np.greater_equal(c3,c4))
[[False True]
[False False]]
********************
[[False True]
[ True False]]
逻辑运算
d1=np.array([[1.2,0],[0,5.6]])
d2=np.array([[1.7,0],[3.2,5.8]])
print(np.logical_and(d1,d2))
print('*'*20)
print(np.logical_or(d1,d2))
print('*'*20)
print(np.logical_xor(d1,d2))
[[ True False]
[False True]]
********************
[[ True False]
[ True True]]
********************
[[False False]
[ True False]]
创建通用函数
def A(a):
result=np.zeros_like(a)
result.flat=42
return result
ufunc=np.frompyfunc(A,1,1)
f1=np.array([[1,2,3],[4,5,6]])
print(ufunc(f1))
[[array(42) array(42) array(42)]
[array(42) array(42) array(42)]]
def B(a,b):
return a**b
func1=np.frompyfunc(B,2,1)
print(func1(f1,2))
[[1 4 9]
[16 25 36]]
numpy 已有的通用函数
f2=np.arange(9)
print(f2)
[0 1 2 3 4 5 6 7 8]
print(np.add.reduce(f2))
print('*'*20)
print(np.add.accumulate(f2))
36
[ 0 1 3 6 10 15 21 28 36]
reduceat
print(np.add.reduceat(f2,[0,5,2,7]))
[10 5 20 15]
outer
f3=np.array([2,3,4,5])
print(np.add.outer(f2,f3))
[[ 2 3 4 5]
[ 3 4 5 6]
[ 4 5 6 7]
[ 5 6 7 8]
[ 6 7 8 9]
[ 7 8 9 10]
[ 8 9 10 11]
[ 9 10 11 12]
[10 11 12 13]]
逆矩阵
g1=np.mat('0 1 2;1 0 3;4 -3 8')
g2=np.linalg.inv(g1)
print(g1*g2)
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
解线性方程
x-2y+z=0 2y-8z=8 -4x+5y+9z=-9
h1=np.mat('1 -2 1;0 1 -8;-4 5 9')
h2=np.array([0,8,9])
h3=np.linalg.solve(h1,h2)
print(h3)
[-29. -16. -3.]
特征值、特征向量
j=np.mat('1 2 3;4 5 6;7 8 9')
j1=np.linalg.eigvals(j)
print('特征值:',j1)
out:
特征值: [ 1.61168440e+01 -1.11684397e+00 -9.75918483e-16]
j2,j3=np.linalg.eig(j)
print('特征值:',j2)
print('*'*50)
print('特征向量:',j3)
out:
特征值: [ 1.61168440e+01 -1.11684397e+00 -9.75918483e-16]
**************************************************
特征向量: [[-0.23197069 -0.78583024 0.40824829]
[-0.52532209 -0.08675134 -0.81649658]
[-0.8186735 0.61232756 0.40824829]]
https://app.yinxiang.com/shard/s49/nl/20930457/b166b16c-9c3f-4a79-826c-c28fbc5a22ad?title=Numpy%E2%80%94%E9%80%9A%E7%94%A8%E5%87%BD%E6%95%B0
https://zhuanlan.zhihu.com/p/82060776