参阅:【程序员数学:用Python学透线性代数和微积分】
下载:随书下载=> 源代码文件.zip , 程序员数学 附录.pdf
解压 源代码文件.zip 后访问目录 Math-for-Programmers-master\
点击 Anaconda3:Jupyter Notebook 打开 *.ipynb 文件,可见源代码。
编写 chapter01.1.py 如下
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
from math import sqrt, log
ys = [30]
xs = range(0,501)
np.random.seed(seed=42)
for delta in np.random.normal(0,1,500):
ys.append(ys[-1] + delta)
plt.plot(ys)
plt.ylabel('Stock Price')
plt.xlabel('Elapsed Time (min)')
plt.savefig('images/1.01.svg')
r = stats.linregress(xs,ys)
line = [r.slope * x + r.intercept for x in xs]
std = np.std([(y-y0) for y,y0 in zip(ys,line)])
top = [y + std for y in line]
bottom = [y - std for y in line]
plt.plot(xs,ys)
plt.plot(xs,line)
plt.plot(xs,top)
plt.plot(xs,bottom)
plt.ylabel('Stock Price')
plt.xlabel('Elapsed Time (min)')
plt.grid()
plt.show()
#plt.savefig('images/1.02.svg')
先 mkdir images
运行 python chapter01.1.py