Python 常用函数(自用)

目录

np.where用法

np.ones


np.where用法

np.where有两种用法


1.np.where(condition,x,y) 当where内有三个参数时,第一个参数表示条件,当条件成立时where方法返回x,当条件不成立时where返回y


2.np.where(condition) 当where内只有一个参数时,那个参数表示条件,当条件成立时,where返回的是每个符合condition条件元素的坐标,返回的是以元组的形式

#用法一
#当self.net_input(X)返回的值大于等于0.0时,where返回1,否则返回0
np.where(self.net_input(X) >= 0.0, 1, 0)
#用法二
a = np.array([2,4,6,8,10])
#只有一个参数表示条件的时候
np.where(a > 5)

输出:
array([ 6,  8, 10])

原文链接:https://blog.youkuaiyun.com/island1995/article/details/90200151


np.ones

生成参数为一的数组

>>> np.ones(5)
array([ 1.,  1.,  1.,  1.,  1.])
>>> np.ones((5,), dtype=np.int)
array([1, 1, 1, 1, 1])
>>> np.ones((2, 1))
array([[ 1.],
       [ 1.]])
>>> s = (2,2)
>>> np.ones(s)
array([[ 1.,  1.],
       [ 1.,  1.]])

len()与len(a[0])

len()返回字符串、列表、字典、元组等长度

len(a[0])返回第*维数组的长度

直接放代码理解

str = "avdsc"
print(len(str))  # 字符串长度

l = [[1, 2, 3, 4, 4, 5],
     [2, 2, 2, 2, 2]]
print(len(l))
print(len(l[0]))
print(len(l[1]))

结果:

5
2
6

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值