np.sum() 用法 & np.log() 用法 & np.expand_dims() 用法

import numpy as np


# axis = 0表示对最外层[]里的最大单位块做块与块之间的运算,同时移除最外层[]:
a = np.array([1, 2, 3])
print(a.sum(axis=0))
"""
6
"""

a = np.array([[1, 2], [3, 4]])
print(a.sum(axis=0))
"""
[4 6]
"""

a = np.array([[[1, 2], [3, 4]], [[11, 12], [13, 14]]])
print(a)
print(a.sum(axis=0))
"""
[[[ 1  2]
  [ 3  4]]

 [[11 12]
  [13 14]]]
  
[[12 14]
 [16 18]]
"""


# axis= 1表示对第二外层[]里的最大单位块做块与块之间的运算,同时移除第二外层[]:
a = np.array([[1, 2], [3, 4]])
print(a.sum(axis=1))
"""
有两层[],第二外层[]里的最大单位块有两组(因为有两个第二外层[]),第一组是1,2,第二组是3,4,分别对这两个单位块做块与块之间的运算,第一组结果为1+2=3,第二组结果为3+4=7;
做完加法后本应是[[3],[7]],但是**移除第二外层[]**后,原来的两层[]变成一层[],所以返回结果为[3, 7]。
"""


# ------------ np.log -----------------
print(np.log(np.e))  #log下什么都不写默认是自然对数
"""
1.0
"""

print(np.log10(100))  #log10是以10为底的
"""
2.0
"""


# ------------ np.expand_dims -----------------
"""
np.array([1, 2, 3]) 
当你看以上数组时,从1到2,到3。这就是所谓的axis=0轴 
np.array([ [1, 2], [3, 4], [4, 5] ]) 

再用相同的方法,看上面数组,首先是从[1, 2]到 [3, 4]到[4, 5]。这就是从0轴视角看的数据,当我们选择0轴所在的第一个元素[1, 2]时,
我们看到的是从1到2。这就是从1轴看到的数据。假若有n维数据,axis=0表示最外面括号说包含的里面所有内容,axis=1表示第2个括号里面所包含所有内容,比如:[[[内容1],[内容2]]]。
"""

print("---"*15)
a = np.array([1, 2, 3, 4, 5])
b = a[np.newaxis, :]  # 插在最前面,从左到右,就按照从高维到低维位置排,在最面加个括号,增加了一维
print(a, "\n", b)
"""
[1 2 3 4 5] 
 [[1 2 3 4 5]]
"""

print("---"*15)
a = np.array([1, 2, 3, 4, 5])
b = a[:, np.newaxis]  # 插在最前面,从左到右,就按照从高维到低维位置排,在最面加个括号,增加了一维
print(a, "\n", b)
"""
[1 2 3 4 5] 
 [[1]
 [2]
 [3]
 [4]
 [5]]
"""


print("---"*15)
x = np.array([1, 2])
print("x ->", x)
print("x.shape->",x.shape)
y = np.expand_dims(x, axis=0)
print("y->", y)
print("y.shape->",y.shape)
"""
x -> [1 2]
x.shape-> (2,)
y-> [[1 2]]
y.shape-> (1, 2)
"""


print("---"*15)
x = np.array([1, 2])
print("x ->", x)
print("x.shape->",x.shape)
y = np.expand_dims(x, axis=1)
print("y->", y)
print("y.shape->",y.shape)
"""
x -> [1 2]
x.shape-> (2,)
y-> [[1]
 [2]]
y.shape-> (2, 1)
"""

print("---"*15)
x = np.array([[1, 2, 3], [4, 5, 6]])
print("x -> \n", x)
print("x.shape -> \n", x.shape)
y = np.expand_dims(x, axis=0)
print("y -> \n", y)
print("y.shape -> \n", y.shape)
"""
x -> 
 [[1 2 3]
 [4 5 6]]
x.shape -> 
 (2, 3)
y -> 
 [[[1 2 3]
  [4 5 6]]]
y.shape -> 
 (1, 2, 3)
"""
print("---"*15)
x = np.array([[1, 2, 3], [4, 5, 6]])
print("x -> \n", x)
print("x.shape -> \n", x.shape)
y = np.expand_dims(x, axis=1)
print("y -> \n", y)
print("y.shape -> \n", y.shape)
"""
x -> 
 [[1 2 3]
 [4 5 6]]
x.shape -> 
 (2, 3)
y -> 
 [[[1 2 3]]

 [[4 5 6]]]
y.shape -> 
 (2, 1, 3)
"""

print("---"*15)
x = np.array([[1, 2, 3], [4, 5, 6]])
print("x -> \n", x)
print("x.shape -> \n", x.shape)
# y = np.expand_dims(x, axis=-1)
y = np.expand_dims(x, axis=2)
print("y -> \n", y)
print("y.shape -> \n", y.shape)
"""
x -> 
 [[1 2 3]
 [4 5 6]]
x.shape -> 
 (2, 3)
y -> 
 [[[1]
  [2]
  [3]]

 [[4]
  [5]
  [6]]]
y.shape -> 
 (2, 3, 1)
"""

 

 

参考链接:

https://blog.csdn.net/qq_35860352/article/details/80463111

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值