1. getattr() 函数
getattr() 函数用于返回一个对象属性值。
描述
getattr() 函数用于返回一个对象属性值。
语法
getattr 语法:
getattr(object, name[, default])
参数
- object -- 对象。
- name -- 字符串,对象属性。
- default -- 默认返回值,如果不提供该参数,在没有对应属性时,将触发 AttributeError。
返回值
返回对象属性值。
2. np.random.choice方法
def choice(a, size=None, replace=True, p=None)
表示从a中随机选取size个数replacement 代表的意思是抽样之后还放不放回去,如果是False的话,那么通一次挑选出来的数都不一样,如果是True的话, 有可能会出现重复的,因为前面的抽的放回去了。p表示每个元素被抽取的概率,如果没有指定,a中所有元素被选取的概率是相等的。-
默认为有放回的抽样 (可以重复)
例子
- np.random.choice(5, 3)
- 和np.random.randint(0,5,3)意思相同,表示从[0,5)之间随机以等概率选取3个数
- np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])
- 表示分别以p=[0.1, 0, 0.3, 0.6, 0]的概率从[0,1,2,3,4]这四个数中选取3个数
3. numpy.setdiff1d(ar1, ar2, assume_unique=False)[source]
Find the set difference of two arrays.
Return the unique values in ar1 that are not in ar2.
Parameters:
ar1: array_like
Input array.
ar2:array_like
Input comparison array.
assume_unique: bool
If True, the input arrays are both assumed to be unique, which can speed up the calculation. Default is False.
Returns:
setdiff1d: ndarray
1D array of values in ar1 that are not in ar2. The result is sorted when assume_unique=False, but otherwise only sorted if the input is sorted.
本文深入探讨Python中的getattr函数用法,包括如何获取对象属性值及默认值设定,同时详解NumPy库中np.random.choice与numpy.setdiff1d函数的功能与参数,帮助读者掌握Python高级特性和NumPy核心操作。
1022

被折叠的 条评论
为什么被折叠?



