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

我想在 python 中创建随机变量并使用以下代码, weights = np.random.random(10)但我想创建随机变量,使三分之一的权重应为零。有什么办法吗?我也试过下面的代码,但这不是我想要的

weights = np.random.random(7)
weights.append(0, 0, 0)


湖上湖
浏览 127回答 3
3回答

慕妹3146593

设置大约 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>>> weightsarray([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>>> weightsarray([0.        , 0.36839012, 0.        , 0.51468295, 0.45694205,       0.23881473, 0.1223229 , 0.68440171, 0.        , 0.15542469])从权重中选择 3 个随机索引并将它们替换为 0

倚天杖

一种简单的方法:>>> import numpy as np>>>                                                                                                                 >>> a = np.clip(np.random.uniform(-0.5, 1, (100,)), 0, np.inf)>>> aarray([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] 均匀地绘制,然后将低于零的所有内容设置为零。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python