numpy是python的一个数值计算库,可是有许多语法和python不兼容。
比如python的random.randint(low,high)使用方法是返回[low,high]之间的整数,官方文档:
random.randint(a, b)
Return a random integer N such that a <= N <= b.
注意是两边都是闭区间,但在numpy中,random.randint(low,high)就变成了前闭后开,即不包含high,官方文档:
Return random integers from the “discrete uniform” distribution of the specified dtype in the “half-open” interval [low, high). If high is None (the default), then results are from [0, low).
所以在python和numpy一块用时,千万不能搞混,如果导入numpy建议起个别名:
import numpy as np;
这样当使用numpy的random时,可以:
np.random.randint(low,high)
使用python时,先导入random包:
import random as ra;
ra.randint(low,high);
这样就不易混了
参考:
python2.7文档:
https://docs.python.org/2.7/library/random.html?highlight=random#module-random
numpy文档:
https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.random.randint.html#numpy.random.randint