本文整理汇总了Python中numpy.random.multinomial方法的典型用法代码示例。如果您正苦于以下问题:Python random.multinomial方法的具体用法?Python random.multinomial怎么用?Python random.multinomial使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy.random
的用法示例。
在下文中一共展示了random.multinomial方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: draw_links
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import multinomial [as 别名]
def draw_links(self,n=1,log_sampling=False):
""" Draw multiple random links. """
urls = []
domain_array = np.array([dmn for dmn in self.domain_links])
domain_count = np.array([len(self.domain_links[domain_array[k]]) for k in range(domain_array.shape[0])])
p = np.array([np.float(c) for c in domain_count])
count_total = p.sum()
if log_sampling: # log-sampling [log(x+1)] to bias lower count domains
p = np.fromiter((np.log1p(x) for x in p), dtype=p.dtype)
if count_total > 0:
p = p/p.sum()
cnts = npr.multinomial(n, pvals=p)
if n > 1:
for k in range(cnts.shape[0]):
domain = domain_array[k]
cnt = min(cnts[k],domain_count[k])
for url in random.sample(self.domain_links[domain],cnt):
urls.append(url)
else:
k = int(np.nonzero(cnts)[0])
domain = domain_array[k]
url = random.sample(self.domain_links[domain],1)[0]
urls.append(url)
return urls
示例2: test_basic
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import multinomial [as 别名]
def test_basic(self):
random.multinomial(100, [0.2, 0.8])
示例3: test_zero_probability
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import multinomial [as 别名]
def test_zero_probability(self):
random.multinomial(100, [0.2, 0.8, 0.0, 0.0, 0.0])
示例4: test_size
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import multinomial [as 别名]
def test_size(self):
# gh-3173
p = [0.5, 0.5]
assert_equal(np.random.multinomial(1, p, np.uint32(1)).shape, (1, 2))
assert_equal(np.random.multinomial(1, p, np.uint32(1)).shape, (1, 2))
assert_equal(np.random.multinomial(1, p, np.uint32(1)).shape, (1, 2))
assert_equal(np.random.multinomial(1, p, [2, 2]).shape, (2, 2, 2))
assert_equal(np.random.multinomial(1, p, (2, 2)).shape, (2, 2, 2))
assert_equal(np.random.multinomial(1, p, np.array((2, 2))).shape,
(2, 2, 2))
assert_raises(TypeError, np.random.multinomial, 1, p,
float(1))
示例5: test_multinomial
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import multinomial [as 别名]
def test_multinomial(self):
np.random.seed(self.seed)
actual = np.random.multinomial(20, [1/6.]*6, size=(3, 2))
desired = np.array([[[4, 3, 5, 4, 2, 2],
[5, 2, 8, 2, 2, 1]],
[[3, 4, 3, 6, 0, 4],
[2, 1, 4, 3, 6, 4]],
[[4, 4, 2, 5, 2, 3],
[4, 3, 4, 2, 3, 4]]])
assert_array_equal(actual, desired)
示例6: test_size
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import multinomial [as 别名]
def test_size(self):
# gh-3173
p = [0.5, 0.5]
assert_equal(np.random.multinomial(1, p, np.uint32(1)).shape, (1, 2))
assert_equal(np.random.multinomial(1, p, np.uint32(1)).shape, (1, 2))
assert_equal(np.random.multinomial(1, p, np.uint32(1)).shape, (1, 2))
assert_equal(np.random.multinomial(1, p, [2, 2]).shape, (2, 2, 2))
assert_equal(np.random.multinomial(1, p, (2, 2)).shape, (2, 2, 2))
assert_equal(np.random.multinomial(1, p, np.array((2, 2))).shape,
(2, 2, 2))
assert_raises(TypeError, np.random.multinomial, 1, p,
np.float(1))
示例7: test_multinomial
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import multinomial [as 别名]
def test_multinomial(self):
np.random.seed(self.seed)
actual = np.random.multinomial(20, [1/6.]*6, size=(3, 2))
desired = np.array([[[4, 3, 5, 4, 2, 2],
[5, 2, 8, 2, 2, 1]],
[[3, 4, 3, 6, 0, 4],
[2, 1, 4, 3, 6, 4]],
[[4, 4, 2, 5, 2, 3],
[4, 3, 4, 2, 3, 4]]])
np.testing.assert_array_equal(actual, desired)
示例8: test_multinomial
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import multinomial [as 别名]
def test_multinomial(self):
np.random.seed(self.seed)
actual = np.random.multinomial(20, [1/6.]*6, size=(3, 2))
desired = np.array([[[4, 3, 5, 4, 2, 2],
[5, 2, 8, 2, 2, 1]],
[[3, 4, 3, 6, 0, 4],
[2, 1, 4, 3, 6, 4]],
[[4, 4, 2, 5, 2, 3],
[4, 3, 4, 2, 3, 4]]])
np.testing.assert_array_equal(actual, desired)