Linked Questions
10 questions linked to/from Binary random array with a specific proportion of ones?
4 votes
4 answers
9k views
Python Random Array of 0s and 1s [duplicate]
I want to randomly produce an array of n ones and m zeros. I thought of this solution: produce the ones array (np.ones) produce the zeros array (np.zeros) combine them to one array (np.hstack) ...
3 votes
0 answers
60 views
Numpy generate X random binary numbers with a define threshold in 1 line [duplicate]
I have come with the following code to generate an array of size: arr_size, containing only 1's and 0's so that, on average, it'll have "threshold" 1's in it. Here's the code: import numpy as np ...
30 votes
5 answers
52k views
Generate random array of 0 and 1 with a specific ratio
I want to generate a random array of size N which only contains 0 and 1, I want my array to have some ratio between 0 and 1. For example, 90% of the array be 1 and the remaining 10% be 0 (I want this ...
5 votes
3 answers
1k views
Create a 3D tensor of zeros with exactly one '1' randomly placed on every slice in numpy/jax
I need to create a 3D tensor like this (5,3,2) for example array([[[0, 0], [0, 1], [0, 0]], [[1, 0], [0, 0], [0, 0]], [[0, 0], [1, 0], [...
2 votes
1 answer
1k views
Creating an Array with an equal number of 0 and 1 in a random order?
Before I explain, here is my code for reference: import numpy as np arrayteam = [[3,3,3,3,3,3],[2,2,2,2,2,2],[1,1,1,1,1,1]] #nteams = 3 #nsubteam = 2 #newnums = np.zeros((len(arrayteam),len(...
-4 votes
1 answer
3k views
create and initialize grid(matrix) 100x100 wih numpy
I am doing a task to simulate Schelling's Model of Segregation. Now I would like to know the following: 1)how do I generate a 2d matrix(grid) with 100x100cells using numpy 2) how to fill the matrix ...
0 votes
1 answer
2k views
Clustering in binary numpy ndarray with cluster size/density variable and exact no. of zeros
I am new to programming and hoping somebody can help me with a specific problem I have. I want to form clusters in a 100x100 binary numpy ndarray under two conditions: I want to specify the number ...
1 vote
1 answer
519 views
Shuffle in one dimension of a matrix(effeciently)?
I was trying to write a function that gets a matrix of 2D points and a probability p and change or swap each points coordinates with probability p So I asked a question and I was trying to use a ...
-1 votes
2 answers
272 views
random number with specific condition in python
I'd like to generate an int random number between 0 and 1 n number of times. I know the code to generate this: num_samp = np.random.randint(0, 2, size= 20) However, I need to specify a condition that ...
0 votes
3 answers
106 views
Function to create matrix of zeros and ones, with a certain density of ones
I would like to create a function that takes three arguments (n,m,d) and it should output a matrix with n rows and m columns. The matrix should be populated with values 0 and 1 at random, in order to ...