Chapter01
codes&results
分别设置多项式的阶数为:
order=[1,2,3,10,100]
, 结果如下图:
import scipy as sp
import matplotlib.pyplot as plt
data = sp.genfromtxt("./data/web_traffic.tsv", delimiter="\t")
# print(data[:10])
# print(data.shape)
x = data[:, 0]
y = data[:, 1]
print(sp.sum(sp.isnan(y)))
x = x[~sp.isnan(y)]
y = y[~sp.isnan(y)]
plt.scatter(x, y)
plt.title("Web traffic over the last month")
plt.xlabel("time")
plt.ylabel("Hits/hour")
plt.xticks([w * 7 * 24 for w in range(10)],
['week %i' % w for w in range(10)])
def error(f, x, y):
return sp.sum((f(x) - y) ** 2)
orders = [1, 2, 3, 10, 100] # order
legds = []
# polyfit: 1, 2, 3, 4,
for k in orders:
fp = sp.polyfit(x, y, k) # 1 for one oder
print("Model parameters: %s" % fp)
f = sp.poly1d(fp)
print("test error: ", error(f, x, y))
fx = sp.linspace(0, x[-1], 1000)
plt.plot(fx, f(fx), linewidth=4)
legds.append("order=%i" % f.order)
plt.legend(legds, loc="upper left")
plt.autoscale(tight=True)
plt.grid()
plt.show()
problems
- 在
import matplotlib.pyplot as plt
时,报出如下错误:
import matplotlib.pyplot as plt;
File "/usr/local/lib/python2.7/dist-packages/matplotlib/__init__.py", line 116, in <module>
raise ImportError("matplotlib requires dateutil")
ImportError: matplotlib requires dateutil
根据提示,安装dateutil
库,命令:sudo easy_install python-dateutil
,然后又提示如下错误:
import matplotlib.pyplot as plt;
File "/usr/local/lib/python2.7/dist-packages/matplotlib/__init__.py", line 140, in <module>
raise ImportError("matplotlib requires pyparsing")
ImportError: matplotlib requires pyparsing
根据提示,安装pyparsing
,命令:easy_install pyparsing
。这样就好了。