赞
踩
Coursera吴恩达机器学习专项课,C1W1笔记
步骤一:将模型以公式表达 Model representation
步骤二:计算代价函数 Cost function
步骤三:使用梯度下降算法 Gradient descent for linear regression
- import numpy as np
- import matplotlib.pyplot as plt
此处以房价为例,1000sqft售价300千美元,2000sqft售价500千美元,则训练数据集为:
- x_train = np.array([1.0, 2.0])
- y_train = np.array([300.0, 500.0])
- print(f"x_train = {x_train}")
- print(f"y_train = {y_train}")
此处输出为:x_train = [1. 2.] y_train = [300. 500.]
用m表述训练数据的数量。Numpy有.shape的参数,因此:
- print(f"x_train.shape: {x_train.shape}")
- m = x_train.shape[0]
- print(f"Number of training examples is: {m}")
此处输出:x_train.shape: (2,) Number of training examples is: 2
在列表的基础上,使用切片:
- i = 0 # Change this to 1 to see (x^1, y^1)
-
- x_i = x_train[i]
- y_i = y_train[i]
- print(f"(x^({i}), y^({i})) = ({x_i}, {y_i})")
此处输出:(x^(0), y^(0)) = (1.0, 300.0)
可以使用matplotlib中的scatter(),可以设置maker形状,c颜色等参数:
- # 绘制散点图
- plt.scatter(x_train, y_train, marker='x', c='r')
- # 设置标题
- plt.title("Housing Prices")
- # 设置y轴标签
- plt.ylabel('Price (in 1000s of dollars)')
- # 设置x轴标签
- plt.xlabel('Size (1000 sqft)')
- plt.show()
此处以最简单的一元线性回归为例,
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。