Generate a new numpy array of order 2 filling in each element with a random number in a certain range
Tag : python , By : Killercode
Date : March 29 2020, 07:55 AM
With these it helps I'm new to numpy. What's the best way to create a new array and fill each element with a random number within a certain range? , Try something like np.random.randint(2, size=(3, 3))
import numpy as np
print np.random.randint(2, size=(3, 3))
[[1 0 0]
[1 1 0]
[1 0 0]]

Efficiently select subsection of numpy array
Date : March 29 2020, 07:55 AM
Does that help I want to split a numpy array into three different arrays based on a logical comparison. The numpy array I want to split is called x. It's shape looks as follows, but it's entries vary: (In response to Saullo Castro's comment I included a slightly different array x.) , Only the first part of your question from numpy import *
x = array([[ 0.46006547, 0.5580928 , 0.70164242, 0.84519205, 1.4 ],
[ 0.00912908, 0.00912908, 0.05 , 0.05 , 0.05 ]])
low, high = array([ 0.633, 0.01 ]), array([ 1.325, 0.99 ])
# construct an array of two rows of bools expressing your conditions
indices1 = array((x[0,:]<low[0], x[1,:]<low[1]))
print indices1
# do an or of the values along the first axis
indices1 = any(indices1, axis=0)
# now it's a single row array
print indices1
# use the indices1 to extract what you want,
# the double transposition because the elements
# of a 2d array are the rows
tmp1 = x.T[indices1].T
print tmp1
# [[ True True False False False]
# [ True True False False False]]
# [ True True False False False]
# [[ 0.46006547 0.5580928 ]
# [ 0.00912908 0.00912908]]
from numpy import *
x = array([[ 0.46006547, 0.5580928 , 0.70164242, 0.84519205, 1.4 ],
[ 0.00912908, 0.00912908, 0.05 , 0.05 , 0.05 ]])
low, high = array([ 0.633, 0.01 ]), array([ 1.325, 0.99 ])
l0r = searchsorted(x[0,:], low[0], side='right')
l1r = searchsorted(x[1,:], low[1], side='right')
h0l = searchsorted(x[0,:], high[0], side='left')
h1l = searchsorted(x[1,:], high[1], side='left')
lr = max(l0r, l1r)
hl = min(h0l, h1l)
print lr, hl
print x[:,:lr]
print x[:,lr:hl]
print x[:,hl]
# 2 4
# [[ 0.46006547 0.5580928 ]
# [ 0.00912908 0.00912908]]
# [[ 0.70164242 0.84519205]
# [ 0.05 0.05 ]]
# [ 1.4 0.05]
from numpy import *
# I changed x to force overlap
x = array([[ 0.46006547, 1.4 , 1.4, 1.4, 1.4 ],
[ 0.00912908, 0.00912908, 0.05, 0.05, 0.05 ]])
low, high = array([ 0.633, 0.01 ]), array([ 1.325, 0.99 ])
l0r = searchsorted(x[0,:], low[0], side='right')
l1r = searchsorted(x[1,:], low[1], side='right')
lr = max(l0r, l1r)
h0l = searchsorted(x[0,lr:], high[0], side='left')
h1l = searchsorted(x[1,lr:], high[1], side='left')
hl = min(h0l, h1l) + lr
t1 = x[:,range(lr)]
xn = x[:,range(lr,hl)]
ncol = shape(x)[1]
t2 = x[:,range(hl,ncol)]
print x
del(x)
print
print t1
print
# note that xn is a void array
print xn
print
print t2
# [[ 0.46006547 1.4 1.4 1.4 1.4 ]
# [ 0.00912908 0.00912908 0.05 0.05 0.05 ]]
#
# [[ 0.46006547 1.4 ]
# [ 0.00912908 0.00912908]]
#
# []
#
# [[ 1.4 1.4 1.4 ]
# [ 0.05 0.05 0.05]]

Replace numpy array value on condition with random number
Date : March 29 2020, 07:55 AM
around this issue I need to replace some values in a numpy array based on a condition with a random number. , Here's one vectorized approach  noise_factor = 0.5 # Input param
# Get mask of zero places and the count of it. Also compute threshold
mask = X==0
c = np.count_nonzero(mask)
threshold_prob = noise_factor * 100.
# Generate noise numbers for count number of times.
# This is where vectorization comes into the play.
nums = np.random.randint(1,100, c)
# Finally piece of the vectorization comes through replacing that IFELSE
# with np,where that does the same op of choosing but in a vectorized way
vals = np.where(nums <= threshold_prob, np.random.randint(1,100, c) , 0)
# Assign back into X
X[mask] = vals
nums = np.random.randint(1,100, (2,c))
vals = np.where(nums[0] <= threshold_prob, nums[1] , 0)

Generate each column of the numpy array with random number from different range
Date : March 29 2020, 07:55 AM
Any of those help What you can do is produce all random numbers in the interval [0, 1) first and then scale and shift them accordingly: import numpy as np
num = 5
ranges = np.asarray([[0,1],[4,5]])
starts = ranges[:, 0]
widths = ranges[:, 1]ranges[:, 0]
a = starts + widths*np.random.random(size=(num, widths.shape[0]))

Generating multiple random integer number with fixed sum = 100 in numpy array
Date : October 02 2020, 02:00 AM
fixed the issue. Will look into that further You can generate n random numbers with given sum using the following function import random
def numbers_with_sum(n, k):
"""n numbers with sum k"""
if n == 1:
return [k]
num = random.randint(1, k)
return [num] + numbers_with_sum(n  1, k  num)
In [1]: numbers_with_sum(3, 100)
Out[1]: [75, 9, 16]

