1.绘图可视化显示函数
plt.show()
# 随机漫步示例
import random
import matplotlib.pyplot as plt
position = 0
walk = [position]
steps = 10000
for i in range(steps):
step = i if random.randint(0, 1) else -1
position += step
walk.append(position)
plt.plot(walk[:1000])
plt.show()
具体细节用法参照链接:
https://www.cnblogs.com/zyg123/p/10504633.html
2. np.where(condition, x, y)
满足条件(condition),输出x,不满足输出y。
3.Python之Random.randint()与numpy.random.randint()的区别
(1) random.randint(a,b):用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n:a<=n<=b,即[a,b]
m=random.randint(0,2)
它只会在0、1、2中随机返回一个整数
(2) np.random.randint(a,b):
用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n:a<=n<b,即[a,b)
numpy.random.randint(low, high=None, size=None, dtype='l')
即[low, high)。如果没有写参数high的值,则返回[0,low)的值。
numpy.random.randint(0,2)
会在0、1中随机返回一个整数,但是其中不会包括2。
示例:
np.random.randint(2, size=10)
array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])
np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
[3, 2, 2, 0]])
np.random.randint(2, high=10, size=(2,3))
array([[6, 8, 7],
[2, 5, 2]])