机器学习追求的是合理的假设空间的选取和模型的泛化能力。
人生苦短,我用python。
单纯的lambda表达式
f = lambda x:pow(x,2)
f(2)
如上两行代码,定义一个lambda表达式f,输入参数为x,返回为x的平方
机器学习的过程:
获取与处理数据
选择与处理数据
评估与可视化结果
import numpy as np import matplotlib.pyplot as plt # Read dataset x, y = [], [] for sample in open("../_Data/prices.txt", "r"): xx, yy = sample.split(",") x.append(float(xx)) y.append(float(yy)) x, y = np.array(x), np.array(y) # Perform normalization x = (x - x.mean()) / x.std() # Scatter dataset plt.figure() plt.scatter(x, y, c="g", s=20) plt.show() x0 = np.linspace(-2, 4, 100) # Get regression model under LSE criterion with degree 'deg' def get_model(deg): return lambda input_x=x0: np.polyval(np.polyfit(x, y, deg), input_x) # Get the cost of regression model above under given x, y def get_cost(deg, input_x, input_y): return 0.5 * ((get_model(deg)(input_x) - input_y) ** 2).sum() # Set degrees test_set = (1, 4, 10) for d in test_set: print(get_cost(d, x, y)) # Visualize results plt.scatter(x, y, c="g", s=20) for d in test_set: plt.plot(x0, get_model(d)(), label="degree = {}".format(d)) plt.xlim(-2, 4) plt.ylim(1e5, 8e5) plt.legend() plt.show()
96732238800.35292
94112406641.67743
75874846680.09283