本文整理汇总了Python中numpy.random.random_sample方法的典型用法代码示例。如果您正苦于以下问题:Python random.random_sample方法的具体用法?Python random.random_sample怎么用?Python random.random_sample使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy.random
的用法示例。
在下文中一共展示了random.random_sample方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_numpy_inverse
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import random_sample [as 别名]
def test_numpy_inverse(self, dim):
self.query(dedent('''\
CREATE EXTERNAL SCALAR SCRIPT
numpy(dim INTEGER)
RETURNS boolean AS
# redirector @@redirector_url@@
from numpy import *
from numpy.linalg import inv
from numpy.random import seed, random_sample
def run(ctx):
dim = ctx.dim
seed(12345678 * dim)
A = random_sample((dim, dim))
Ai = inv(A)
R = dot(A, Ai) - identity(dim)
return bool(-1e-12 <= R.min() <= R.max() <= 1e-12)
'''))
rows = self.query('SELECT numpy(?) FROM dual', dim)
self.assertRowsEqual([(True,)], rows)
示例2: test_numpy_inverse
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import random_sample [as 别名]
def test_numpy_inverse(self, dim):
self.query(dedent('''\
CREATE python SCALAR SCRIPT
numpy(dim INTEGER)
RETURNS boolean AS
from numpy import *
from numpy.linalg import inv
from numpy.random import seed, random_sample
def run(ctx):
dim = ctx.dim
seed(12345678 * dim)
A = random_sample((dim, dim))
Ai = inv(A)
R = dot(A, Ai) - identity(dim)
return bool(-1e-12 <= R.min() <= R.max() <= 1e-12)
'''))
rows = self.query('SELECT numpy(?) FROM dual', dim)
self.assertRowsEqual([(True,)], rows)
示例3: test_numpy_inverse
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import random_sample [as 别名]
def test_numpy_inverse(self, dim):
self.query(dedent('''\
CREATE python3 SCALAR SCRIPT
numpy(dim INTEGER)
RETURNS boolean AS
from numpy import *
from numpy.linalg import inv
from numpy.random import seed, random_sample
def run(ctx):
dim = ctx.dim
seed(12345678 * dim)
A = random_sample((dim, dim))
Ai = inv(A)
R = dot(A, Ai) - identity(dim)
return bool(-1e-12 <= R.min() <= R.max() <= 1e-12)
'''))
rows = self.query('SELECT numpy(?) FROM dual', dim)
self.assertRowsEqual([(True,)], rows)
示例4: test_fixed_seed_outcomes
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import random_sample [as 别名]
def test_fixed_seed_outcomes():
"""
Tests if fixing a seed results in deterministic outcomes even when using a
'random' acceptance criterion (here SA).
"""
outcomes = [0.01171, 0.00011, 0.01025]
for seed, desired in enumerate(outcomes): # idx is seed
alns = get_alns_instance(
[lambda state, rnd: ValueState(rnd.random_sample())],
[lambda state, rnd: None],
seed)
simulated_annealing = SimulatedAnnealing(1, .25, 1 / 100)
result = alns.iterate(One(), [1, 1, 1, 1], .5, simulated_annealing, 100)
assert_almost_equal(result.best_state.objective(), desired, decimal=5)
# TODO test more complicated examples?
示例5: setup
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import random_sample [as 别名]
def setup():
global source
global target
source = []
for i in range(13):
if random() < 0.5:
source = concatenate((source, random(CHUNK_SIZE_RANDOM)))
else:
source = concatenate((source, zeros(CHUNK_SIZE_ZEROS, 'f')))
source = source.astype('f')
# Set on target
target = BinnedArray(128, NaN, len(source))
for i in range(len(source)):
# if not isNaN( source[i] ):
target[i] = source[i]
return source, target
示例6: test_file_lzo
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import random_sample [as 别名]
def test_file_lzo():
# With a file (lzo)
target.to_file(open("/tmp/foo3", "wb"), comp_type="lzo")
target3 = FileBinnedArray(open("/tmp/foo3", 'rb'))
# Verify
for i in range(len(source)):
assert source[i] == target3[i], "No match, index: %d, source: %d, target: %d" % (i, source[i], target3[i])
# Verify with slices
target3 = FileBinnedArray(open("/tmp/foo3", 'rb'))
for i in range(10):
a = int(random() * len(source))
b = int(random() * len(source))
if b < a:
a, b = b, a
assert allclose(source[a:b], target3[a:b]), "No match, index: %d:%d, source: %s, target: %s" % \
(a, b, ",".join(map(str, source[a:a+10])), ",".join(map(str, target3[a:a+10])))
示例7: pytest_generate_tests
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import random_sample [as 别名]
def pytest_generate_tests(metafunc):
if "diagonal_property" in metafunc.fixturenames:
metafunc.parametrize("diagonal_property", DIAGONAL_PROPERTIES)
elif "edge_name" in metafunc.fixturenames:
metafunc.parametrize("edge_name", EDGE_NAMES)
elif "graph_element" in metafunc.fixturenames:
metafunc.parametrize("graph_element", GRAPH_ELEMENTS)
elif "field_dtype" in metafunc.fixturenames:
metafunc.parametrize("field_dtype", FIELD_DTYPES)
elif "random_xy" in metafunc.fixturenames:
from numpy.random import random_sample
metafunc.parametrize(
"random_xy",
(
tuple(-1e3 * random_sample(2)),
tuple(1e3 * random_sample(2)),
tuple(1e3 * (random_sample(2) - 0.5)),
),
)
示例8: _compute_vectorized
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import random_sample [as 别名]
def _compute_vectorized(self, args, y):
random_values = random_sample(args.index.shape[0])
if self.a != 0.0 or self.b != 1.0:
random_values = (self.b - self.a) * random_values + self.a
return random_values
示例9: randwppf
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import random_sample [as 别名]
def randwppf(ppf, args=(), size=None):
"""
returns an array of randomly distributed integers of a distribution
whose percent point function (inverse of the CDF or quantile function)
is given.
args is a tuple of extra arguments to the ppf function (i.e. shape,
location, scale), and size is the size of the output. Note the ppf
function must accept an array of q values to compute over.
"""
U = random_sample(size=size)
return ppf(*(U,)+args)
示例10: corrupt_image
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import random_sample [as 别名]
def corrupt_image(img, MAR_prob=0, min_rects=0, max_rects=0, min_width=0, max_width=0, apply_to_all_channels=False):
def generate_channel_mask():
mask = np.zeros(img.shape[0:2], dtype=np.bool)
if MAR_prob > 0:
mask[(random_sample(mask.shape) < MAR_prob)] = True
if max_rects > 0 and max_width > 0:
h, w = mask.shape
num_rects = random_integers(min_rects, max_rects)
for i in range(num_rects):
px1 = random_integers(0, w - min(max(min_width, 1), w))
py1 = random_integers(0, h - min(max(min_width, 1), h))
px2 = px1 + min_width + random_integers(0, max(min(w - px1 - min_width, max_width - min_width), 0));
py2 = py1 + min_width + random_integers(0, max(min(h - py1 - min_width, max_width - min_width), 0));
if px1 <= px2 and py1 <= py2:
mask[py1:py2, px1:px2] = True
else:
# One of the sides has length 0, so we should remove any pixels4
pass
return mask
new_img = img.copy()
channels = 1 if len(new_img.shape) == 2 else new_img.shape[-1]
global_mask = np.zeros(img.shape, dtype=np.bool)
if channels == 1 or apply_to_all_channels:
mask = generate_channel_mask()
if channels == 1:
global_mask[:, :] = mask
else:
for i in xrange(channels):
global_mask[:, :, i] = mask
else:
global_mask = np.zeros(img.shape, dtype=np.bool)
for i in xrange(channels):
global_mask[:,:,i] = generate_channel_mask()
new_img[global_mask] = 0
return (new_img, 1.0 * global_mask)
# Process command line inputs
示例11: sample
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import random_sample [as 别名]
def sample(values, probabilities, size):
assert np.allclose(np.sum(probabilities, axis=-1), 1.0)
bins = np.add.accumulate(probabilities)
return values[np.digitize(random_sample(size), bins)]
示例12: _random_float
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import random_sample [as 别名]
def _random_float(self, a, b):
return (b - a) * random_sample() + a
示例13: test_simple
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import random_sample [as 别名]
def test_simple():
# Verify
for i in range(len(source)):
assert source[i] == target[i], "No match, index: %d, source: %f, target: %f, len( source ): %d" % (i, source[i], target[i], len(source))
# Verify with slices
for i in range(10):
a = int(random() * len(source))
b = int(random() * len(source))
if b < a:
a, b = b, a
assert allclose(source[a:b], target[a:b]), "No match, index: %d:%d, source: %s, target: %s" % \
(a, b, ",".join(map(str, source[a:a+10])), ",".join(map(str, target[a:a+10])))
示例14: test_file
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import random_sample [as 别名]
def test_file():
# With a file (zlib)
target.to_file(open("/tmp/foo", "wb"))
target2 = FileBinnedArray(open("/tmp/foo", 'rb'))
for i in range(len(source)):
assert source[i] == target2[i], "No match, index: %d, source: %d, target: %d" % (i, source[i], target2[i])
# Verify with slices
target2 = FileBinnedArray(open("/tmp/foo", 'rb'))
for i in range(10):
a = int(random() * len(source))
b = int(random() * len(source))
if b < a:
a, b = b, a
assert allclose(source[a:b], target[a:b]), "No match, index: %d:%d, source: %s, target: %s" % \
(a, b, ",".join(map(str, source[a:a+10])), ",".join(map(str, target2[a:a+10])))
示例15: wolfe_line_search
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import random_sample [as 别名]
def wolfe_line_search(self, fun, search_dir, curr_value, exp_decrease) :
"""
see Numerical Optimization,
Nocedal and Wright, Algorithm 3.5, p. 60
"""
f = lambda t : fun(self.after_step(t * search_dir))[0]
fp = lambda t : self.scal_L2(fun(self.after_step(t * search_dir))[1], search_dir).Q0
exit_code = 0 # Default : everything is all right
# Code to uncomment to check that fp is the true derivative of f========
h = 1e-8
for i in range(5) :
t = random_sample()
update_th = fp(t)
update_emp = (f(t+h) - f(t-h)) / (2*h)
print('')
print('search dir : ', search_dir.to_array())
print('Checking the function passed to the Wolfe line search, t = ', t)
print('Empirical derivative : ', update_emp)
print('Theoretical derivative : ', update_th)
#=======================================================================
print("Exp decrease : ", exp_decrease)
(a, _, _, _, _, _) = line_search(f, fp, 0, 1, exp_decrease, curr_value, c2 = 0.95)
if a == None :
print('Error during the wolfe line search')
a = 0
exit_code = 1 # Exit_code = 1 : break !
step = a * search_dir
new_state = self.after_step(step)
self.set_state(new_state)
#self.current_cost_grad = (C,grad)
#self.is_current_cost_computed = True
self.is_current_point_computed = False
return (step, exit_code)