1

I want to create random variables in python and used the following below code weights = np.random.random(10) but I want to create random variables such that one third of the weights should be zero. Is there any way possible? I have also tried below code but this is not what I want

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

5 Answers 5

2

With the clarification that you want the 0's to appear randomly, you can just use shuffle:

weights = np.random.random(7)
weights = np.append(weights,[0, 0, 0])
np.random.shuffle(weights)
Sign up to request clarification or add additional context in comments.

1 Comment

Note that your code will be faster and more memory efficient if you use replace instead of append, as suggested by other answers. Generate 10 random values, then: weights[-3:] = [0,0,0]
2

One simple way:

>>> 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

It draws uniformly from [-0.5, 1] and then sets everything below zero to zero.

Comments

2

Set Approximately 1/3 of weights

This will guarantee that approximately one third of your weights are 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.        ])

What it does is divides about 1/3 of your values by 0, giving you inf, then just replace the inf by 0

Set Exactly 1/3 of weights

Alternatively, if you need it to be exactly 1/3 (or in your case, 3 out of 10), you can replace 1/3 of your weights with 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])

That selects 3 random indices from weights and replaces them with 0

Comments

1
size = 10
v = np.random.random(size)
v[np.random.randint(0, size, size // 3)] = 0

A little bit more optimized (because random number generation is not "cheap"):

v = np.zeros(size)
nnonzero = size - size // 3
idx = np.random.choice(size, nnonzero, replace=False)
v[idx] = np.random.random(nnonzero)

Comments

0

What about replacing the first third of items with 0 then shuffle it as following

weights = np.random.random(10)
weights[: weights.size / 3] = 0
np.random.shuffle(weights)

2 Comments

This adds 0 to first three numbers but what I want is to randomly assign the zeros as well
Then you can use np.random.shuffle, as Ahmed Yousif suggests

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.