Home > OS >  Am tryin to generate different values for each element of a column of a numpy array using a certain
Am tryin to generate different values for each element of a column of a numpy array using a certain

Time:06-29

I need each column to generate random integers with specified range for col 1 (random.randint(1, 50) for col 2 random.randint(51, 100)...etc

import numpy
import random
import pandas
from random import randint
wsn = numpy.arange(1, 6)
taskn = 3

t1 = numpy.random.randint((random.randint(2, 50),random.randint(51, 100),
    random.randint(101, 150),random.randint(151, 200),random.randint(201, 250)),size=(5,5))
t2 = numpy.random.randint((random.randint(2, 50),random.randint(51, 100),
    random.randint(101, 150),random.randint(151, 200),random.randint(201, 250)),size=(5,5))
t3= numpy.random.randint((random.randint(2, 50),random.randint(51, 100),
    random.randint(101, 150),random.randint(151, 200),random.randint(201, 250)),size=(5,5))
print('\nGenerated Data:\t\n\nNumber   \t\t\t Task 1 \t\t\t Task 2 \t\t\t Task 3\n')
ni = len(t1)
for i in range(ni):
    print('\t {0}    \t   {1}   \t {2} \t {3}\n'.format(wsn[i], t1[i],t2[i],t3[i]))
print('\n\n')

It prints the following

   Generated Data:  

Number               Task 1              Task 2              Task 3

     1         [  1  13  16 121  18]     [  5  22  34  65 194]   [ 10  68  60 134 130]

     2         [  0   2 117 176  46]     [  1  15 111 116 180]   [22 41 70 24 85]

     3         [  0  12 121  19 193]     [  0   5  37 109 205]   [  5  53   5 106  15]

     4         [  0   5  97  99 235]     [  0  22 142  11 150]   [  6  79 129  64  87]

     5         [  2  46  71 101 186]     [  3  57 141  37  71]   [ 15  32   9 117  77]

soemtimes It even generates 0 when I didn't even specifiy 0 in the ranges

CodePudding user response:

np.random.randint(low, high, size=None) allows for low and high being arrays of length num_intervals.

In that case, when size is not specified, it will generate as many integers as there are intervals defined by the low and high bounds.

If you want to generate multiple integers per interval, you just need to specify the corresponding size argument, which must ends by num_intervals.

Here it is size=(num_tasks, num_samples, num_intervals).

import numpy as np

bounds = np.array([1, 50, 100, 150, 200, 250])
num_tasks = 3
num_samples = 7

bounds_low = bounds[:-1]
bounds_high = bounds[1:]
num_intervals = len(bounds_low)

arr = np.random.randint(
    bounds_low, bounds_high, size=(num_tasks, num_samples, num_intervals)
)

Checking the properties:

assert arr.shape == (num_tasks, num_samples, num_intervals)

for itvl_idx in range(num_intervals):
    assert np.all(arr[:, :, itvl_idx] >= bounds_low[itvl_idx])
    assert np.all(arr[:, :, itvl_idx] < bounds_high[itvl_idx])

An example of output:

array([[[ 45,  61, 100, 185, 216],
        [ 36,  78, 117, 152, 222],
        [ 18,  77, 112, 153, 221],
        [  9,  70, 123, 178, 223],
        [ 16,  84, 118, 157, 233],
        [ 42,  78, 108, 179, 240],
        [ 40,  52, 116, 152, 225]],

       [[  3,  92, 102, 151, 236],
        [ 45,  89, 138, 179, 218],
        [ 45,  73, 120, 183, 231],
        [ 35,  80, 130, 167, 212],
        [ 14,  86, 118, 195, 212],
        [ 20,  66, 117, 151, 248],
        [ 49,  94, 138, 175, 212]],

       [[ 13,  75, 116, 169, 206],
        [ 13,  75, 127, 179, 213],
        [ 29,  64, 136, 151, 213],
        [  1,  81, 140, 197, 200],
        [ 17,  77, 112, 171, 215],
        [ 18,  75, 103, 180, 209],
        [ 47,  57, 132, 194, 234]]])
  • Related