Machine Learning(Andrew) ex1-Linear Regression
椰汁学习笔记
最近刚学习完吴恩达机器学习的课程,现在开始复习和整理一下课程笔记和作业,我将陆续更新。
Linear regression with one variable
- 2.1 Plotting the Data
首先我们读入数据,先观察数据的内容

数据是一个txt文件,每一行存储一组数据,第一列数据为城市的人口,第二列数据城市饭店的利润。
读入数据时按行读入,每行通过分解,获得两个数据。数据的存储使用列表。初次接触没有使用一些第三方库pandas、numpy,后续会逐渐用到。
# Part1:从txt文件中读取数据,绘制成散点图
f = open("ex1data1.txt", 'r')
population = []
profit = []
for line in f.readlines():
col1 = line.split(',')[0]
col2 = line.split(',')[1].split('\n')[0]
population.append(float(col1))
profit.append(float(col2))
接下来将数据可视化,需要用到matplotlib这个画图库,这个教程还可以
import matplotlib.pyplot as plt
#上面这样引入
plt.title("Scatter plot of training data")
plt.xlabel("population of city")
plt.ylabel("profit")
plt.scatter(population, profit, marker='x')
plt.show()
xlabel和ylabel设置当前图片的x,y轴的标签;scatter绘制散点图,前两个参数为两个坐标轴的对应数值列表(元组等),marker指定绘制点的图形;show函数显示。

- 2.2 Gradient Descent
J ( θ ) = 1 2 m ∑ i = 1 m h θ ( x ( i ) − y ( i ) ) 2 \mathit{J}(\theta) = \frac{1}{2m} \sum_{i=1}^{m}h_\theta(x^{(i)}-y^{(i)})^{2} J(θ)=2m1i=1∑mhθ(x(i)−y(i))2
h θ ( x ) = θ T x = θ 0 + θ 1 x 1 h_{\theta}(x)=\theta^{T}x=\theta_{0}+\theta_{1}x_{1} hθ(x)=θ

本文是作者对吴恩达机器学习课程中线性回归部分的笔记和作业实践,涵盖了一维和多维线性回归。作者通过Python实现梯度下降法,详细介绍了数据读入、可视化、损失函数计算、梯度下降优化以及特征归一化。最终,通过正规方程对比了梯度下降法求解线性回归参数的效果。
&spm=1001.2101.3001.5002&articleId=104535408&d=1&t=3&u=48fbd07012124d2b91d9b130f273fd60)
797

被折叠的 条评论
为什么被折叠?



