首页 > 其他分享 >numpy.random.choice(a, size=None, replace=True, p=None)

numpy.random.choice(a, size=None, replace=True, p=None)

时间:2023-04-11 18:13:39浏览次数:42  
标签:None random choice pooh replace True size

import numpy as np
import random
list_a = ["a", "b", "c", "d", "e", "f", "g"]

get_ = np.random.choice(list_a, 4, replace=False)
print(get_)

输出:

['a' 'f' 'c' 'e']

numpy.random.choice(a, size=None, replace=True, p=None)
从a(只要是ndarray都可以,但必须是一维的)中随机抽取数字,并组成指定大小(size)的数组
replace:True表示可以取相同数字,False表示不可以取相同数字
数组p:与数组a相对应,表示取数组a中每个元素的概率,默认为选取每个元素的概率相同。

>>> aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher']
>>> np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])
	array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'], dtype='|S11')
#可以看到,‘pooh’被选取的概率明显比其他几个高很多

标签:None,random,choice,pooh,replace,True,size
From: https://www.cnblogs.com/yanghailin/p/17307149.html

相关文章

  • 【Python】pip intall 遇到了WARNING: Retrying (Retry(total=4, connect=None, read=
    具体可能会遇到的是以下这些情况:WARNING:Retrying(Retry(total=4,connect=None,read=None,redirect=None,status=None))afterconnectionbrokenby'ProxyError('Cannotconnecttoproxy.',OSError(0,'Error'))'requests.exceptions.ProxyErro......
  • cpp: random
     //RandomlySampled.h:此文件包含"RandomlySampled"类。十个常用排序算法C++11//2023年4月9日涂聚文GeovinDuedit.#pragmaonce#ifndefRANDOMLYSAMPLED_H#defineRANDOMLYSAMPLED_H#include<iostream>#include<string>#include<vector&g......
  • (C#)Random实现随机点名
    namespaceWindowsFormsApp3{publicpartialclassForm1:Form{//实例化字符串,设置字符串长度与内容string[]student=newstring[7]{"张三","李四","王五","赵六","hello","world","helloworl......
  • 两个对象值相同(x.equals(y) == true),但却可有不同的hash code
    当我们使用Java的hashCode()方法时,该方法将返回与该对象相关的一个整数值。hashcode用于在哈希表等数据结构中确定对象的位置,以提高查找效率。当我们使用equals()方法比较两个对象时,如果equals()返回true,则可以认为这两个对象的值相等。然而,有时候两个对象值相同,但它们......
  • cpp generate random array then sort by quick sort
    #include<chrono>#include<ctime>#include<iomainp>#include<iostream>#include<random>#include<sstream>std::stringget_time_now(){std::chrono::time_point<std::chrono::high_resolution_clock>now=st......
  • ERROR: Could not find a version that satisfies the requirement pymysql (from ver
    踩过的坑不管是idea中直接引入还是 pip3installpymysql都会报错:ERROR:Couldnotfindaversionthatsatisfiestherequirementpymysql(fromversions:none)  原因是网络问题,需要需要使用国内镜像源来加速,比如豆瓣源pipinstallpymysql-ihttp://pypi.douba......
  • java lambda List 查找 anyMatch() allMatch() noneMatch()
    packagelambda.list;importcn.hutool.core.util.ObjectUtil;importlombok.extern.slf4j.Slf4j;importorg.junit.Test;importpojo.Dome;importjava.util.ArrayList;importjava.util.List;/***@Author:xxx*@date2021/5/14**/@Slf4jpublicclassSe......
  • 随机生成工具 RandomValueUtil
    packageentity;/*****@Author:www.itheima.com*@Description:itheima*****/publicclassRandomValueUtil{publicstaticStringbase="abcdefghijklmnopqrstuvwxyz0123456789";privatestaticStringfirstName="赵钱孙李周吴郑王冯陈褚卫蒋沈......
  • es(Elasticsearch)查询报错: Set fielddata=true on [level] in order to load fieldd
     Invocationofinitmethodfailed;nestedexceptionisElasticsearchStatusException[Elasticsearchexception[type=search_phase_execution_exception,reason=allshardsfailed]];nested:ElasticsearchException[Elasticsearchexception[type=illegal_argument_......
  • excel文件公式数据为None怎么办?
    #原因,包问题我用openpyxl操作完以后,带公式的值就读取不出来了;#编写操作,打开一个目录下的所有xlsx文件功能#所需要的依赖importwin32apiimportwin32conimportwin32processfromwin32comimportclientimportpsutildefkill(p_name="excel.exe"):pids=......