为了账号安全,请及时绑定邮箱和手机立即绑定

在 Python 中创建随机变量,其中三分之一的数组为零

在 Python 中创建随机变量,其中三分之一的数组为零

湖上湖 2021-06-18 12:19:05
我想在 python 中创建随机变量并使用以下代码, weights = np.random.random(10)但我想创建随机变量,使三分之一的权重应为零。有什么办法吗?我也试过下面的代码,但这不是我想要的weights = np.random.random(7) weights.append(0, 0, 0)
查看完整描述

3 回答

?
慕妹3146593

TA贡献1820条经验 获得超9个赞

设置大约 1/3 的重量

这将保证大约三分之一的权重为 0:


weights = np.random.random(10)/np.random.choice([0,1],10,p=[0.3,0.7])


weights[np.isinf(weights)] = 0

# or 

# weights[weights == np.inf] = 0


>>> weights

array([0.        , 0.25715864, 0.        , 0.80958258, 0.12880619,

       0.48781856, 0.52278911, 0.76541417, 0.87736431, 0.        ])

它的作用是将您的值的大约 1/3 除以 0,给您inf,然后只需将其替换为inf0


设置正好 1/3 的权重

或者,如果您需要它正好是1/3(或者在您的情况下,10 分之 3),您可以将 1/3 的权重替换为0:


weights = np.random.random(10)

# Replace 3 with however many indices you want changed...

weights[np.random.choice(range(len(weights)),3,replace=False)] = 0


>>> weights

array([0.        , 0.36839012, 0.        , 0.51468295, 0.45694205,

       0.23881473, 0.1223229 , 0.68440171, 0.        , 0.15542469])

从权重中选择 3 个随机索引并将它们替换为 0


查看完整回答
反对 回复 2021-06-22
?
倚天杖

TA贡献1828条经验 获得超3个赞

一种简单的方法:


>>> import numpy as np

>>>                                                                                                                 

>>> a = np.clip(np.random.uniform(-0.5, 1, (100,)), 0, np.inf)

>>> a

array([0.39497669, 0.65003362, 0.        , 0.        , 0.        ,                                                  

       0.75545815, 0.30772786, 0.1805628 , 0.        , 0.        ,                                                  

       0.        , 0.82527704, 0.        , 0.63983682, 0.89283051,                                                  

       0.25173721, 0.18409163, 0.63631959, 0.59095185, 0.        ,                                                  

       0.85817311, 0.        , 0.06769175, 0.        , 0.67807471,                                                  

       0.29805637, 0.03429861, 0.53077809, 0.32317273, 0.52346321,                                                  

       0.22966515, 0.98175502, 0.54615167, 0.        , 0.88853359,                                                  

       0.        , 0.70622272, 0.08106305, 0.        , 0.8767082 ,                                                  

       0.52920044, 0.        , 0.        , 0.29394736, 0.4097331 ,                                                  

       0.77977164, 0.62860222, 0.        , 0.        , 0.14899124,                                                  

       0.81880283, 0.        , 0.1398242 , 0.        , 0.50113732,                                                  

       0.        , 0.68872893, 0.15582668, 0.        , 0.34789122,                                                  

       0.18510949, 0.60281713, 0.21097922, 0.77419626, 0.29588479,                                                  

       0.18890799, 0.9781896 , 0.96220508, 0.52201816, 0.71087763,                                                  

       0.        , 0.43540516, 0.99297503, 0.        , 0.69248893,                                                  

       0.05157044, 0.        , 0.75131066, 0.        , 0.        ,                                                  

       0.25627591, 0.53367521, 0.58151298, 0.85662171, 0.455367  ,

       0.        , 0.        , 0.21293519, 0.52337335, 0.        ,

       0.68644488, 0.        , 0.        , 0.39695189, 0.        ,

       0.40860821, 0.84549468, 0.        , 0.21247807, 0.59054669])

>>> np.count_nonzero(a)

67

它从 [-0.5, 1] 均匀地绘制,然后将低于零的所有内容设置为零。


查看完整回答
反对 回复 2021-06-22
  • 3 回答
  • 0 关注
  • 126 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号