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.csdn.net/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

猜你喜欢

转载自blog.csdn.net/weixin_52127098/article/details/124551419