Opps_0
Opps_0

Reputation: 438

How to create random float number (between a range) for param_grid

I want to create float numbers between (0.2, 0.3) inside param_grid. I have the code

    test_size = (0.2, 0.3)
    param_iters = 2
    param_grid = {
        "test_size": uniform(test_size[0], test_size[1]),
    }
    sampler = list(ParameterSampler(param_grid, n_iter=param_iters))
    args = [Namespace(**{**args, **dict(params=params)}) for params in sampler]

But I have found that the train_size are 0.3897422919229748 and 0.4852142919229748. That means I am getting over the range (0.2, 0.3)

I also tried to use the below code

    param_grid = {
        "test_size": round(random.uniform(test_size[0], test_size[1]), 2),
    }

But getting an error at the next line

Parameter value is not iterable or distribution (key='test_size', value=0.24)

How can I limit the train_size in between (0.2, 0.3) inside param_grid?

Upvotes: 1

Views: 146

Answers (2)

0Knowledge
0Knowledge

Reputation: 755

Based on @mb4329, I would like to change a little. Here instead of 1 you have to use param_iters. Otherwise, you will get only 1 test_size.

    param_grid = {
        "test_size": np.random.uniform(test_size[0], test_size[1], param_iters)
    }

Output

{'test_size': array([0.23745401, 0.29507143])}
[{'test_size': 0.23745401188473625}, {'test_size': 0.2950714306409916}]

Upvotes: 1

mb4329
mb4329

Reputation: 21

I think you just need to return a list instead of float in that second example

test_size = (0.2, 0.3)
x = np.random.uniform(test_size[0], test_size[1], 1)
print(x)
>> [0.2100939]

Upvotes: 1

Related Questions