吴恩达机器学习作业一(Linear regression with one variable and multiple variables)

(吴恩达作业pdf免费资源:https://download.csdn.net/download/iv__vi/12675412

吴恩达机器学习作业一(Linear regression with one variable and multiple variables)

一、损失函数

J(θ)为损失函数,代码中的computeCost函数就是根据这个公式写的。

二、梯度下降 

 

 单变量:

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


# 计算损失函数
def computeCost(x, y, w):
    return np.sum(np.power(np.matmul(x, w) - y, 2)) / (2 * len(x))


# 梯度下降
def gradient(x, y, w, lr, iters):
    cost = np.zeros(iters)
    plt.ion()
    plt.figure(1)
    for i in range(iters):
        error = np.matmul(x, w) - y  # (97, 1)
        # 下面的方法1是循环更新权重
        # for j in range(w.shape[0]):
        #     w[j, 0] = w[j, 0] - (lr / len(x)) * np.sum(np.multiply(error, x[:, j].reshape(-1, 1)))
        # 下面的方法2是利用矩阵,一次性更新权重
        w = w - (lr / len(x)) * np.sum(np.multiply(error, x), axis=0).reshape((2, 1))
        cost[i] = computeCost(x, y, w)
        print(i, cost[i])
        plt.plot(i, cost[i], 'b.-')
        plt.pause(0.01)
    return w, cost


path = 'ex1data1.txt'
data = pd.read_csv(path, header=None, names=['Population', 'Profit'])  # 读入数据
data.insert(0, 'Ones', 1)  # 在data第0列添加一列1,用于与偏置b相乘
cols = data.shape[1]  # 获得data的行数
x = data.iloc[:, 0:cols - 1]  # x是所有行,去掉最后一列, type=<class 'pandas.core.frame.DataFrame'>
y = data.iloc[:, cols - 1:cols]  # y是所有行,最后一列, type=<class 'pandas.core.frame.DataFrame'>
x = x.values  # shape=(97, 2), type=<class 'numpy.ndarray'>
y = y.values  # shape=(97, 1), type=<class 'numpy.ndarray'>
w = np.array([0, 0], dtype=np.float).reshape(-1, 1)  # shape=(2, 1), type=<class 'numpy.ndarray'>
lr = 0.01  # 学习率
iters = 10000  # 迭代次数
w, cost = gradient(x, y, w, lr, iters)  # 进行迭代,获得训练后的权重和损失值
np.savetxt('hw01_01_w.csv', w)  # 保存权重
# # 绘制图像,查看拟合效果
# x_test = np.linspace(data.Population.min(), data.Population.max(), 100)
# y_test = w[0, 0] + (w[1, 0] * x_test)
# fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(12, 8))
# ax[0][0].plot(x_test, y_test, 'r', label='Prediction')
# ax[0][0].scatter(data.Population, data.Profit, label='Traning Data')
# ax[0][0].legend(loc=2)  # 图例放在左上角
# ax[0][0].set_xlabel('Population')
# ax[0][0].set_ylabel('Profit')
# ax[0][0].set_title('Predicted Profit vs. Population Size')
# # ax[0][1].scatter(data.Population * 2, data.Profit * 2, label='Traning Data 2')
# # ax[0][1].legend(loc=2)  # 图例放在左上角
# plt.show()

 

多变量:

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


def computeCost(x, y, w):
    return np.sum(np.power(np.matmul(x, w) - y, 2)) / (2 * len(x))



# 梯度下降
def gradient(x, y, w, lr, iters):
    cost = np.zeros(iters)
    plt.ion()
    plt.figure(1)
    for i in range(iters):
        error = np.matmul(x, w) - y  # (97, 1)
        # 下面的方法1是循环更新权重
        # for j in range(w.shape[0]):
        #     w[j, 0] = w[j, 0] - (lr / len(x)) * np.sum(np.multiply(error, x[:, j].reshape(-1, 1)))
        # 下面的方法2是利用矩阵,一次性更新权重
        w = w - (lr / len(x)) * np.sum(np.multiply(error, x), axis=0).reshape((2, 1))
        cost[i] = computeCost(x, y, w)
        print(i, cost[i])
        plt.plot(i, cost[i], 'b.-')
        plt.pause(0.01)
    return w, cost


path2 = 'ex1data2.txt'
data2 = pd.read_csv(path2, header=None, names=['size', 'room_number', 'price'])
data2 = (data2 - data2.mean()) / data2.std()
data2.insert(0, 'Ones', 1)
x = data2.iloc[:, 0:-1].values  # (47, 3)
y = data2.iloc[:, -1].values.reshape(-1, 1)  # (47, 1)
w = np.zeros(shape=(3, 1))
print(x.shape, y.shape, w.shape)
w, cost = gradient(x, y, w, lr=0.001, iters=1000)
np.savetxt('hw01_w2.csv', w)
np.savetxt('hw01_cost2.csv', cost)
fig, ax = plt.subplots(figsize=(12, 8))
ax.set_xlabel('iters')
ax.set_ylabel('cost')
ax.plot(np.arange(1000), cost)
plt.show()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值