小白学习吴恩达机器学习课后作业一

本文记录了作者在学习吴恩达机器学习课程后,对课后作业一的完成过程和思考,旨在分享学习心得。
摘要由CSDN通过智能技术生成

最近在学习吴恩达老师的机器学习,做的课后作业1供分享参考

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


alpha = 0.01#学习率
iters = 1000#迭代次数设置为1000
path = 'ex1data1.txt'
data = pd.read_csv(path, header=None, names=['population', 'profit'])
print(data.head())
print(data.describe())
data.plot(kind='scatter', x='population', y='profit', figsize=(10,8))


#求损失函数
def computeCost(X, y, thera):
    inner = np.power(((X*thera.T)-y), 2)
    return np.sum(inner) / (2 * len(X))


#定义梯度下降函数
def gradientDescent(X, y, theta, alpha, iters):#公式推导
    temp = np.matrix(np.zeros(theta.shape))
    parameters = int(theta.ravel().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] = theta[0, j] - ((alpha / len(X)) *
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值