当前位置:   article > 正文

监督学习Regression Model训练方法_compute_model_output

compute_model_output

Coursera吴恩达机器学习专项课,C1W1笔记

无监督回归模型整体步骤:

步骤一:将模型以公式表达 Model representation

步骤二:计算代价函数 Cost function

步骤三:使用梯度下降算法 Gradient descent for linear regression

步骤一:将模型以公式表达 Model representation

1. 导入所需工具

  1. import numpy as np
  2. import matplotlib.pyplot as plt

2. 描述问题

此处以房价为例,1000sqft售价300千美元,2000sqft售价500千美元,则训练数据集为:

  1. x_train = np.array([1.0, 2.0])
  2. y_train = np.array([300.0, 500.0])
  3. print(f"x_train = {x_train}")
  4. print(f"y_train = {y_train}")

此处输出为:x_train = [1. 2.] y_train = [300. 500.]

3. 描述数据形状

用m表述训练数据的数量。Numpy有.shape的参数,因此:

  1. print(f"x_train.shape: {x_train.shape}")
  2. m = x_train.shape[0]
  3. print(f"Number of training examples is: {m}")

此处输出:x_train.shape: (2,)   Number of training examples is: 2

4. 描述单个的训练数据

在列表的基础上,使用切片:

  1. i = 0 # Change this to 1 to see (x^1, y^1)
  2. x_i = x_train[i]
  3. y_i = y_train[i]
  4. print(f"(x^({i}), y^({i})) = ({x_i}, {y_i})")

此处输出:(x^(0), y^(0)) = (1.0, 300.0)

5. 绘制图表

可以使用matplotlib中的scatter(),可以设置maker形状,c颜色等参数:

  1. # 绘制散点图
  2. plt.scatter(x_train, y_train, marker='x', c='r')
  3. # 设置标题
  4. plt.title("Housing Prices")
  5. # 设置y轴标签
  6. plt.ylabel('Price (in 1000s of dollars)')
  7. # 设置x轴标签
  8. plt.xlabel('Size (1000 sqft)')
  9. plt.show()

6. 描述模型

此处以最简单的一元线性回归为例,

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/44446
推荐阅读