Python common functions (for personal use)

Table of contents

np.where Usage

np.ones


np.where Usage

There are two usages of np.where


1.np.where(condition,x,y) When there are three parameters in where, the first parameter indicates the condition, where method returns x when the condition is true, and where returns y when the condition is not true


2.np.where(condition) When there is only one parameter in where, that parameter represents the condition. When the condition is true, where returns the coordinates of each element that meets the condition, and the return is in the form of a tuple

#用法一
#当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])

Original link: https://blog.csdn.net/island1995/article/details/90200151


np.ones

Generates an array with one argument

>>> 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() returns the length of strings , lists, dictionaries, tuples, etc.

len(a[0]) returns the length of the *th dimension array

Direct code understanding

str = "avdsc"
print(len(str)) # string length

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

result:

5
2
6

Guess you like

Origin blog.csdn.net/weixin_52127098/article/details/124551419