《机器学习系统设计》学习笔记


Chapter01

codes&results

分别设置多项式的阶数为: order=[1,2,3,10,100] , 结果如下图:
多项式拟合-web traffic

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。这样就好了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值