Chapter 002. 线性回归

张开发
2026/6/12 9:08:31 15 分钟阅读
Chapter 002. 线性回归
这个数据集可以帮助你估算客户可以卖多少钱房子尺寸为1250平方英尺从这个数据集中建立一个线性回归模型Data has “Right Answers”Regressionmodel - Predictsnumbers问题场景用房屋面积单位平方英尺预测房价单位千美元模型输出连续的数值结果如面积 1250 平方英尺时预测房价约为 220K 美元关键特点预测目标是数值Numbers存在无限多可能的输出值模型通过拟合趋势线线性回归建立输入特征与连续输出的映射关系监督学习的共性回归与分类都属于监督学习其核心定义为数据中自带 “正确答案”标签模型通过学习输入与标签的对应关系实现预测。分类模型聚焦于预测离散类别的任务图中给出两类典型场景二分类示例区分 “猫” 和 “狗”仅存在 2 种可能的输出结果多分类示例疾病诊断可输出 10 种不同的疾病类别关键特点预测目标是类别 / 标签Categories输出为有限个固定的离散值对比维度回归模型分类模型预测目标连续数值如房价、销量、温度离散类别如物种、疾病类型、用户等级输出空间无限多可能的结果有限个固定的结果典型场景房价预测、销量预测、股票价格预测图像识别、垃圾邮件过滤、疾病诊断数据表格和可视化散点图术语表Training set(训练集):Data used to the modelNotationx “input” variable featurey “output” variable “target” variablem number of training examples(x , y) single training example(x(i)x^{(i)}x(i),y(i)y^{(i)}y(i)) i(th)i^{(th)}i(th)training exampleProcess Of How Supervised Learning Worksfw,b(x(i))wx(i)bf_{w,b}(x^{(i)}) wx^{(i)}bfw,b​(x(i))wx(i)b这个公式就是一条直线方程它的作用是用 w 控制直线的斜率面积对房价的影响程度用 b 控制直线的上下平移调整整体预测的基准最终目标是找到合适的 w 和 b让这条直线尽可能贴近所有训练数据点从而实现对新数据的预测。单变量线性回归一元线性回归importnumpyasnpimportmatplotlib.pyplotasplt plt.style.use(./deeplearning.mplstyle)# x_train is the input variable (size in 1000 square feet)# y_train is the target (price in 1000s of dollars)x_trainnp.array([1.0,2.0])y_trainnp.array([300.0,500.0])print(fx_train {x_train})print(fy_train {y_train})# m is the number of training examplesprint(fx_train.shape:{x_train.shape})mx_train.shape[0]print(fNumber of training examples is:{m})# m is the number of training examplesmlen(x_train)print(fNumber of training examples is:{m})i0# Change this to 1 to see (x^1, y^1)x_ix_train[i]y_iy_train[i]print(f(x^({i}), y^({i})) ({x_i},{y_i}))# Plot the data pointsplt.scatter(x_train,y_train,markerx,cr)# Set the titleplt.title(Housing Prices)# Set the y-axis labelplt.ylabel(Price (in 1000s of dollars))# Set the x-axis labelplt.xlabel(Size (1000 sqft))plt.show()w100b100print(fw:{w})print(fb:{b})defcompute_model_output(x,w,b): Computes the prediction of a linear model Args: x (ndarray (m,)): Data, m examples w,b (scalar) : model parameters Returns y (ndarray (m,)): target values mx.shape[0]f_wbnp.zeros(m)foriinrange(m):f_wb[i]w*x[i]breturnf_wb tmp_f_wbcompute_model_output(x_train,w,b)# Plot our model predictionplt.plot(x_train,tmp_f_wb,cb,labelOur Prediction)# Plot the data pointsplt.scatter(x_train,y_train,markerx,cr,labelActual Values)# Set the titleplt.title(Housing Prices)# Set the y-axis labelplt.ylabel(Price (in 1000s of dollars))# Set the x-axis labelplt.xlabel(Size (1000 sqft))plt.legend()plt.show()# Prediction for a house with 1200 sqftw200b100x_i1.2cost_1200sqftw*x_ibprint(f${cost_1200sqft:.0f}thousand dollars)

更多文章