task 1 数据基础
1.随机数的应用
pi的估计
P ( ( X , Y ) 落 在 圆 内 ) = P ( X 2 + Y 2 ≤ 1 ) = π / 4 P((X,Y)落在圆内) = P(X^2+Y^2\le1)=\pi/4 P((X,Y)落在圆内)=P(X2+Y2≤1)=π/4
def pi_estimate(n):
'''
n为投点的数量
'''
n_rand_X = np.random.uniform(-1.0,1.0,n)
n_rand_Y = np.random.uniform(-1.0,1.0,n)
## 判断是否在圆内
distance = np.sqrt(n_rand_X**2 + n_rand_Y**2)
dis_n = float(len(distance[distance<=1.0]))
return 4 * (dis_n / n)
for i in [10,50,100,500,1000,5000,10000,50000,100000,500000,10000000]:
print("pi的估计值为",pi_estimate(i))
np.random.uniform 生成服从均匀分布的随机数
np.random.exponential 生成服从指数分布的随机数
2.作业
x1 = np.arange(-100, 100, 0.05)
x2 = np.arange(-100, 100, 0.05)
x1, x2 = np.meshgrid(x1, x2)
def f(a,b):
z = np.square(a - x1) + b * np.square(x2 - np.square(x1))
fig1 = plt.figure()
ax1 = plt.axes(projection='3d')
ax1.plot_surface(x1, x2, z, alpha=0.3, cmap='rainbow')
ax1.set_xlabel('X')
ax1.set_ylabel('Y')
ax1.set_zlabel('Z')
plt.title('Rosenbrock')
plt.show()
interactive_plot = interactive(f, a=(-200, 200), b=(-300, 300))
output = interactive_plot.children[-1]
output.layout.height = '350px'
interactive_plot
b小于0时
a控制图片两条边的走势,b控制图形的凹凸。
更为细致的a,b区间变化还可以进一步探索。