79 questions
1
vote
1
answer
83
views
numpy.random - size and shape confusion
I was looking through some codes and saw this line, numpy.random.normal(size=x.shape). Where, x = numpy.linspace(1, 2, 100). I don't understand what this does. I've only come across, np.random.normal(...
1
vote
3
answers
250
views
How can I quickly generate a (large) list of random numbers given a list of seeds as input?
I need to make a function that takes in an array of integers and returns a list of random numbers with the same length as the array. However, there is the restriction that the output random number ...
2
votes
1
answer
108
views
Confusion regarding the inner workings of NumPy's SeedSequence
In case it matters at all, I'm using Python 3.11.5 64-bit on a Windows 11 Pro desktop computer with NumPy 1.26.4.
In order to try to better understand what NumPy is doing behind the scenes when I ask ...
0
votes
1
answer
125
views
How to find if two arrays are from uncorrelated distributions?
I have two arrays - array1 and array2. How confidently can I state that they are generated from uncorrelated distributions? Just checking the correlation values would not suffice; I would require p-...
0
votes
1
answer
66
views
How to randomly map a proportion of data value to a specific category?
I have a dataset below which shows if a customer is a return customer or not. The end goal is for all returned customers, I need to map about 25% of them to 'yes 1 purchase' and 75% of them to 'yes &...
3
votes
2
answers
777
views
Correctly seeding numpy random generator
For my scientific experiments, I usually seed using:
rng = np.random.Generator(np.random.PCG64(seed))
which for the current numpy version is equivalent to
rng = np.random.Generator(np.random....
2
votes
1
answer
735
views
Scaling of a Standard Normal Distribution
I have a question that is closely related to this one:
Normed histogram y-axis larger than 1
The solution of the above thread was to scale the dimensions of the axis properly.
However, that solution ...
0
votes
1
answer
256
views
Why does np.random.choice take so much longer than np.random.randint?
I've been reading that np.random.choice is faster than np.random.randint (here and here), however when comparing the speed difference between
import numpy as np
for i in range(100000):
np.random....
0
votes
1
answer
94
views
How to restore matlab random generator state into numpy?
I can save the state of matlab random generator with the following code
seed = 10;
rng(seed, 'twister');
%... random functions that don't need to be reproduced%
s = rng;
s.Type
s.Seed
s.State
save('...
0
votes
1
answer
66
views
Create Numpy array from list of arbitrary sized bites
How can I create a numpy array from a python list of bytes objects of an arbitrary (but known) size?
Example:
size = 10
byte_list = [np.random.default_rng().bytes(size) for i in range(100)]
...
0
votes
1
answer
70
views
Can't assign the "sum of np.random.normal" in a element of array
I am trying to produce random number by random.normal and take the summary of them. Then, I tried to assgin the value to each element in the array sum.
I made a zero array (float type) by np.zeros and ...
0
votes
0
answers
139
views
Randomly sampling similar distributions
I want to find a robust way to sample distributions that are similar to an existing distribution. This new distribution should be at least r/2 away from the original distribution, but at most r away. ...
1
vote
3
answers
1k
views
Uniformity test on a large set using scipy.stats.kstest
I'm playing around with some hardware entropy creation. I'm using electronic sensors to sample the environment, using A2D with between 12-16 bits of resolution. I'm hypothesizing that the lower-order ...
-1
votes
1
answer
803
views
Generate bootstrap sample from ndarray
Is there a way to generate a bootstrap sample on an N-dimensional array? I am limited to using numpy==1.19.4
I have already tried using a for loop on the other dimensions to no avail, but the ...
3
votes
1
answer
1k
views
`numpy.random.normal` generates different numbers on different systems
I'm comparing the numbers generated with np.random.normal on two different systems (details see below) with the following code (I'm using the legacy np.random.seed because this is used by another ...
0
votes
1
answer
308
views
np.random.choice conflict with multiprocessing? multiprocessing inside for loop?
I want to use np.random.choice inside a multiprocessing pool, but I get the IndexError: list index out of range. I don't get any error when I use the choice function inside a for loop (in series then, ...
1
vote
1
answer
200
views
Numpy's Multivariate Normal example is not clear
I am not sure if this question fits to numpy users or mathematicians. I don't understand how the numpy.random.multivariate_normal's example works.
In the bottom of the documentation, it generates a ...
1
vote
2
answers
310
views
How to inspect BitGenerator state in a numpy.random.Generator object?
If I have a numpy.random.Generator, what's the best way to inspect the BitGenerator used internally? And does the BitGenerator have any state that impacts what numbers are generated?
1
vote
2
answers
299
views
Function that generates random numbers calls other function that generates random numbers with a seed, and seed seems to be used again
I am trying to generate a function which generate random numbers from a uniform distribution Number_Rand() and another one which generates a random number given a certain seed Number_Rand_Seed() - so ...
2
votes
0
answers
962
views
How to create NumPy random 2D array with full rank, or a particular rank?
In NumPy, I can use random package to create a 2D array, but cannot make sure it has full rank or a particular rank. How to got it? In the case of full rank, I can use linalg.matrix_rank to check, but ...
0
votes
1
answer
402
views
numpy.random.choice with percentages not working in practice
I'm running python code that's similar to:
import numpy
def get_user_group(user, groups):
if not user.group_id:
user.group_id = assign(groups)
return user.group_id
def assign(groups):...
-1
votes
1
answer
456
views
Handwritten Digit Recognition on MNIST dataset using sklearn
I want to build a Handwritten Digit Recognition on MNIST dataset using sklearn and I wanted to shuffle my train set for both features(x) and label(y). But it shows a KeyError. Let me know what is the ...
0
votes
0
answers
156
views
KeyError pops up each time a function is called
Hi I am trying to implement the Siamese Neural Network for one-shot image recognition with the Omniglot dataset. The initial step for the implementation requires to generate pair samples with same/...
-1
votes
1
answer
94
views
Generating a probability distribution P(y) from another probability distribution P(x) such that highest probability in P(x) is least likely in P(y)
So the problem at hand is that I have some values in a dictionary with counters, let's say
dict = {"cats":0, "dogs":0, "lions":0}
I want to randomly select the keys ...
1
vote
1
answer
272
views
CuPy and Dirichlet gives me TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'
I simply want to create a random matrix A whose vectors are drawn from the Dirichlet distribution. The function works fine with numpy:
import numpy as np
A = np.random.dirichlet(np.ones(n), n)
When I ...
0
votes
1
answer
77
views
How a random number is generated in Numpy if I don't use Generator or RandomState?
I have read the Doc of Numpy,but I still don't know how a random number is generated if I don't use Generator or RandomState.
As far as I know,we can get a random number from 1 to 10 by
#use ...
3
votes
2
answers
4k
views
1D Wasserstein distance in Python
The formula below is a special case of the Wasserstein distance/optimal transport when the source and target distributions, x and y (also called marginal distributions) are 1D, that is, are vectors.
...
2
votes
3
answers
553
views
How could these alternate numpy `uniform` vs `random` constructions possibly differ?
I had some code that random-initialized some numpy arrays with:
rng = np.random.default_rng(seed=seed)
new_vectors = rng.uniform(-1.0, 1.0, target_shape).astype(np.float32) # [-1.0, 1.0)
new_vectors /...
0
votes
1
answer
962
views
How can I create a random walk (with certain probability) for stock price movement in python?
I want to simulate stock price movements in Python, for 3 years, with a total of 300 steps, with 5 paths.
The share price can go up or down with probability of increase = q and probability of falling =...
2
votes
1
answer
552
views
Numpy - generate random array with restrictions on individual values in array - Portfolio Optimisation
I'm developing a portfolio optimisation code with constraints using monte-carlo simulation. However, I have run into a problem. My problem is as follows:
I have a list of instruments ["Multi"...
1
vote
2
answers
279
views
error while using Numpy Random function (np.random.random_integers)
enter image description here
Error while using numpy random function,
it shows int object not callable
please suggest alternative
2
votes
2
answers
2k
views
Why does numpy.random.Generator.choice provides different results (seeded) with given uniform distribution compared to default uniform distribution?
Simple test code:
pop = numpy.arange(20)
rng = numpy.random.default_rng(1)
rng.choice(pop,p=numpy.repeat(1/len(pop),len(pop))) # yields 10
rng = numpy.random.default_rng(1)
rng.choice(pop) # yields 9
...
0
votes
2
answers
2k
views
Why does numpy.random.Generator.choice gives different results even if given fixed seed?
The code is simple:
import numpy
rng = numpy.random.default_rng(0)
control = rng.choice([0,1],p=[0.5,0.5])
for i in range(100):
print(control == rng.choice([0,1],p=[0.5,0.5]))
# Not only True gets ...
4
votes
1
answer
3k
views
Why is np.random.default_rng().permutation(n) preferred over the original np.random.permutation(n)?
Numpy documentation on np.random.permutation suggests all new code use np.random.default_rng() from the Random Generator package. I see in the documentation that the Random Generator package has ...
0
votes
1
answer
178
views
Random number generator with exclusions
I have 9x9 numpy array filled with numbers [1;9]. I need to pick random position and assign this cell as 0 until I get certain difficulty value. By the way, my table should satisfy certain criterias ...
11
votes
1
answer
3k
views
How to use the new NumPy random number generator?
The fact that NumPy now recommends that new code uses the default_rng() instance instead of numpy.random for new code has got me thinking about how it should be used to yield good results, both ...
0
votes
1
answer
80
views
Incoherence in complementary indices extracted from a np.array
The problem is very simple, I have a vector of indices from which I want to extract one set randomly chosen and its complement. So I write the following code:
import numpy as np
vec = np.arange(0,...
-1
votes
2
answers
321
views
swap locations of an element in numpy array
I have created a numpy array
a=np.array([1,2,3])
a
>>> array([1, 2, 3])
I would like to change the positions of the elements.
My expected output should only consists of these three patterns
...
3
votes
2
answers
2k
views
randomly choose N elements from each row of 2d array in python
I have a 2d array, say, a = [ [1, 2, 3, 4], [5, 6,7, 8], [9, 10, 11, 12], ...[21, 22, 23, 24] ], and I would like to pick N elements from each row randomly, based on a probability distribution p which ...
8
votes
1
answer
4k
views
What is the difference between numpy.random's Generator class and np.random methods?
I have been using numpy's random functionality for a while, by calling methods such as np.random.choice() or np.random.randint() etc.
I just now found about the ability to create a default_rng object, ...
-2
votes
2
answers
60
views
Randomly change x amount of values to y in a z size array
New to python and was wondering if the above could be done using python and numpy. I have an array of let's say size 10, I want to change 6 random values from their current value to something else I ...
1
vote
1
answer
959
views
How to efficiently perform billions of Bernoulli extractions using numpy?
I am working at a thesis about epidemiology, and I have to simulate a SI epidemic in a temporal network. At each time step there's a probability ~ Bernoulli(beta) to perform an extraction between an ...
1
vote
2
answers
177
views
How to use a function argument as npy method, if possible
I am trying to create a function that will take a numpy dstr name as an argument and plot a histogram of random data points from that distribution.
if it only works on npy distributions that ...
2
votes
0
answers
586
views
Additive poisson noise to an image
I have written a function to add poisson noise to an image using numpy with np.random.poisson(..). The image is already in numpy array form, using grayscale (0-255). I am wandering if it makes more ...
2
votes
2
answers
4k
views
Split a list into n randomly sized chunks
I am trying to split a list into n sublists where the size of each sublist is random (with at least one entry; assume P>I). I used numpy.split function which works fine but does not satisfy my ...
0
votes
1
answer
136
views
How to use numpy's random number generator in a ufunc?
Basically I have a ufunc that requires some randomness. In order to keep the ufuncs as reproducible as possible I would like to use numpy's random number generator for that; basically because setting ...
1
vote
0
answers
26
views
How can I fix the error: "ValueError: 'a' and 'p' must have the same size" for mtrand.RandomState.choice module? [duplicate]
I am implementing an algorithm to solve the 8-queens problem: https://en.wikipedia.org/wiki/Eight_queens_puzzle
I have run into a problem when trying to iterate through the population and create a ...
-7
votes
2
answers
131
views
Why does this code use only x instead of both x and y in for loop? [closed]
Why have they only used X in for loop and not both X and Y? And why we are using reshape with 1, -1?
# implement a loop which computes Euclidean distances between each element in X and Y
# store ...
2
votes
2
answers
5k
views
How to use RandomState with Sklearn RandomizedSearchCV on multiple cores
I am puzzled about the right way to use np.random.RandomState with sklearn.model_selection.RandomizedSearchCV when running on multiple cores.
I use RandomState to generate pseudo-random numbers so ...
0
votes
1
answer
149
views
What is the maximum number of pseudo-random numbers that can be generated with numpy.random before the sequence begins to repeat?
I need to generate many hundreds of millions of random numbers for a clustering analysis. I am using numpy.random and was wondering if anyone knows the maximum number of pseudo-randoms that can be ...