Coursera ML(3)-Multivariate Linear Regression python实现

Multivariate Linear Regression and Programming Exercise 1 更多见:李飞阳


Gradient Descent for Multiple Variables

  • Suppose we have n variables, set hypothesis to be:

  • Cost Function

  • Gradient Descent Algorithm

    Get every feature into approximately [-1, 1]. Just normalize all the parameters :)

  • Learning Rate:Not too big(fail to converge), not too small(too slow)

  • Polynormal Regression:Use feature scalling. (Somewhat like normalizing dimension)

Programming Exercise 1

下载程序及相关数据

Stanford coursera Andrew Ng 机器学习课程编程作业(Exercise 1),作业下载链接貌似被墙了,下载链接放这。
http://home.ustc.edu.cn/~mmmwhy/machine-learning-ex1.zip

重新推导一下:

其实这里一共就两个式子:

  • computeCost

    hθ(x)=θ0+θ1x1+θ2x2+θ3x3++θnxn

    J(θ0,θ1)=12mi=1m(hθ(x(i))y(i))w

  • gradientDescent

    repeat until convergence: {θ0:=θ1:=}θ0α1mi=1m(hθ(xi)yi)θ1α1mi=1m((hθ(xi)yi)xi)

python拟合实现代码

原本用的是matlab代码,我用python实现了一下,结果是一样的:

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


def readfile(path):
    X=[]
    y=[]
    with open(path,'r') as f:
        for line in f:
            X.append([1,float(line.split(',')[0])])
            y.append(float(line.split(',')[1]))
    return X,y


def dataplot(x,theta,y):
    plt.plot(x, y, 'rx', markersize=10)
    plt.ylabel('Profit in $10,000s')
    plt.xlabel('Population of City in 10,000s')
    plt.plot(X[:,1],X*theta,'-')
    plt.show()


def computeCost(X,y,theta):
    m = len(y)
    J = 0
    for i in range(m):
        J = J + float((X[i]*theta-y[i])**2)
    return J/(2*m)

def gradientDescent(X, y, theta, alpha, num_iters):
    m = len(y)
    num_iters = 1500
    J_history = np.zeros(num_iters)
    for i in range(num_iters):
        S =X.T * (X * theta - np.mat(y).T) / m
        theta = theta - alpha * S;
        J_history[i] = computeCost(X,y,theta)
    return theta

if __name__=="__main__":
    theta = np.mat([[0],[0]])
    iterations = 1500
    alpha = 0.01
    iterations = 1500
    path = "C:\Users\wing\Documents\MATLAB\ex1\ex1data1.txt"

    x,y = readfile(path)# 小写的X不是矩阵,是list,大写的X是矩阵。
    X = np.mat(x)
    J = computeCost(X, y, theta)
    theta = gradientDescent(X, y, theta, alpha, iterations)
    dataplot(X[:,1],theta,y)


输出的图有点小,就这样吧。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值