吴恩达机器学习-ex1-单变量

本文介绍了如何使用Python和numpy/pandas库实现线性回归模型,包括数据预处理、代价函数的计算、梯度下降算法的应用以及成本走势的可视化。通过实例演示了如何预测foodtruck收益与城市人口的关系。
摘要由CSDN通过智能技术生成

损失函数

 j(\theta )=\frac{1}{2}*SUM((X*\theta -y)^{2})

梯度下降函数

\theta = \theta -\alpha \frac{1}{m}X^T(X\theta -y)

维度

X(m,n)y(m,1)  theta(n,1)

 

代码

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# 线性回归(单变量)
# 预测food truck的收益值。ex1data1.txt:数据集,第一列表示城市人数,第二列表示该城市的food truck收益

path = 'ex1data1.txt'
data = pd.read_csv(path, header=None, names=['Population', 'Profit'])
data.head()

data.describe()

data.plot(kind='scatter', x='Population', y='Profit', figsize=(12, 8))
plt.show()

# 代价函数
# 新增一例,x0
data.insert(0, 'Ones', 1)
data.head()

cols = data.shape[1]
X = data.iloc[:, 0:cols - 1]
Y = data.iloc[:, cols - 1:cols]  # 步长

X.head()
Y.head()

X = np.matrix(X.values)
Y = np.matrix(Y.values)
theta = np.matrix(np.array([0, 0]))
X.shape, Y.shape, theta.shape


# 代价函数
def computeCost(X, Y, theta):  # theta.T表示对参数矩阵theta进行转置操作
    inner = np.power((X * theta.T) - Y, 2)
    return np.sum(inner) / (2 * len(X))


computeCost(X, Y, theta)


# difference = computeCost(X, Y, theta)
# print(difference)

# 梯度下降
def gradientDescent(X, Y, theta, alpha, iters):
    temp = np.matrix(np.zeros(theta.shape))
    parameters = int(theta.shape[1])
    cost = np.zeros(iters)

    for i in range(iters):
        error = X * theta.T - Y

        for j in range(parameters):
            term = np.multiply(error, X[:, j])
        temp[0, j] = temp[0, j] - alpha / len(X) * np.sum(term)

        theta = temp
        cost[i] = computeCost(X, Y, theta)

    return theta, cost


alpha = 0.01  # 学习率
iters = 1000  # 是指完成一个epoch所需的迭代次数
g, cost = gradientDescent(X, Y, theta, alpha, iters)
g
# print(g)

# 计算训练模型的误差
computeCost(X, Y, theta)

# 画出拟合图像
x = np.linspace(data.Population.min(), data.Population.max(), 100)
f = g[0, 0] + g[0, 1] * x

plt.figure(figsize=(12, 8))
plt.xlabel('Population')
plt.ylabel('Profit')
l1 = plt.plot(x, f, label='Prediction', color='red')
l2 = plt.scatter(data.Population, data.Profit, label='Traing Data', )
plt.legend(loc='best')
plt.title('Predicted Profit vs Population Size')
plt.show()

# 画出cost的走势
plt.figure(figsize=(12, 8))
plt.xlabel('Iterations')
plt.ylabel('Cost')
plt.title('Error vs Training Epoch')
plt.plot(np.arange(iters), cost, 'r')
plt.show()

图像展示

 

各阶段数据集可以自己打印查看

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值