Python常用函数: 基本函数

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(ar1ar2assume_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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值