sourcecode

범위 간에 임의의 부동 소수점 배열 생성

copyscript 2023. 7. 18. 21:54
반응형

범위 간에 임의의 부동 소수점 배열 생성

저는 특정 범위 사이에서 주어진 길이의 무작위 플로트 배열을 생성하는 함수를 찾지 못했습니다.

랜덤 샘플링을 살펴보았지만 필요한 기능이 없는 것 같습니다.

random.uniform은 근접하지만 특정 숫자가 아닌 단일 요소만 반환합니다.

이게 바로 내가 추구하는 것입니다.

ran_floats = some_function(low=0.5, high=13.3, size=50)

이는 범위에 균일하게 분포된 50개의 무작위 비반복 플로트 배열(즉, 반복이 허용됨)을 반환합니다.[0.5, 13.3].

그런 기능이 있습니까?

np.random.uniform 사용 사례에 적합:

sampl = np.random.uniform(low=0.5, high=13.3, size=(50,))

2019년 10월 업데이트:

구문은 여전히 지원되지만, API는 난수 생성기에 대한 더 나은 제어를 지원하기 위해 NumPy 1.17로 변경된 것으로 보입니다.으로 API가 변경되었으므로 https://docs.scipy.org/doc/numpy/reference/random/generated/numpy.random.Generator.uniform.html 을 참조해야 합니다.

개선 제안은 다음과 같습니다. https://numpy.org/neps/nep-0019-rng-policy.html

왜 목록 이해력을 사용하지 않습니까?

파이썬 2에서

ran_floats = [random.uniform(low,high) for _ in xrange(size)]

파이썬 3에서,range와 같이 작동합니다.xrange(ref)

ran_floats = [random.uniform(low,high) for _ in range(size)]

이것이 가장 간단한 방법입니다.

np.random.uniform(start,stop,(rows,columns))

당신이 찾고 있는 것을 할 수 있는 기능이 이미 있을 수 있지만, 저는 그것에 대해 잘 모릅니다(아직?).그 동안 다음을 사용할 것을 제안합니다.

ran_floats = numpy.random.rand(50) * (13.3-0.5) + 0.5

이렇게 하면 0.5와 13.3 사이의 균일한 분포를 갖는 모양 배열(50,)이 생성됩니다.

함수를 정의할 수도 있습니다.

def random_uniform_range(shape=[1,],low=0,high=1):
    """
    Random uniform range

    Produces a random uniform distribution of specified shape, with arbitrary max and
    min values. Default shape is [1], and default range is [0,1].
    """
    return numpy.random.rand(shape) * (high - min) + min

편집: 흠, 네, 그래서 놓쳤어요, 당신이 원하는 정확한 통화가 있는 numpy.random.uniform()이 있어요!해라import numpy; help(numpy.random.uniform)자세한 정보는.

대신 SciPy를 사용할 수 있습니다.

from scipy import stats
stats.uniform(0.5, 13.3).rvs(50)

그리고 레코드가 정수를 샘플링하는 것은.

stats.randint(10, 20).rvs(50)

random.uniform과 목록 이해를 결합하는 것은 어떻습니까?

>>> def random_floats(low, high, size):
...    return [random.uniform(low, high) for _ in xrange(size)]
... 
>>> random_floats(0.5, 2.8, 5)
[2.366910411506704, 1.878800401620107, 1.0145196974227986, 2.332600336488709, 1.945869474662082]

목록 이해의 for 루프는 시간이 걸리고 속도가 느려집니다.numpy 매개변수(낮음, 높음, 크기 등)를 사용하는 것이 좋습니다.

import numpy as np
import time
rang = 10000
tic = time.time()
for i in range(rang):
    sampl = np.random.uniform(low=0, high=2, size=(182))
print("it took: ", time.time() - tic)

tic = time.time()
for i in range(rang):
    ran_floats = [np.random.uniform(0,2) for _ in range(182)]
print("it took: ", time.time() - tic)

샘플 출력:

('필요 시간: ', 0.06406784057617188)

('필요 시간: ', 1.7253198623657227)

대신 실수 목록이 괜찮다면 표준을 사용할 수 있습니다.random.randrange:

def some_function(low, high, size):
    low_int = int(low * 1000)
    high_int = int(high *1000)
    return [random.randrange(low_int, high_int, size)/1000 for _ in range(size)]

np.random.random_sample(size)반오픈 간격[0.0, 1.0]에서 랜덤 플로트를 생성합니다.

이것은 당신의 예에 효과가 있을 것입니다.

sample = (np.random.random([50, ]) * 13.3) - 0.5

언급URL : https://stackoverflow.com/questions/22071987/generate-random-array-of-floats-between-a-range

반응형