美文网首页计算机杂谈代码改变世界
吴恩达《机器学习》线性回归python版本实现(增图例动态更新

吴恩达《机器学习》线性回归python版本实现(增图例动态更新

作者: 张照博 | 来源:发表于2020-02-19 23:02 被阅读0次

正文之前

也不知道是循着什么路径,我又看了一次机器学习的入门必备--线性回归,索性看看这号称最好的入门教程《机器学习》---吴恩达。

不得不说大佬还是大佬。。讲的比我以前看的那些强多了。。然后找到了一份笔记,对照着,然后自己写了份实时显示拟合曲线的版本,感觉比那份广为流传的python版本更加适合人看(当然,人家的本来就很优秀了,我也是学着写的)

正文

废话不多说,我把吴恩达课程的笔记,python实现代码的Github链接放这儿(不是我的:

https://github.com/HustWolfzzb/Coursera-ML-AndrewNg-Notes

然后摆上我的代码和运行结果:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
A = np.eye(5)
data = pd.read_csv('ex1data1.txt', header=None, names=['Population', 'Profit'])
data.describe()

data.plot(kind='scatter', x=0, y=1, figsize=(12,8))
data.insert(0, 'Ones', 1)
cols = data.shape[1]
X = data.iloc[:,0:cols-1]
y = data.iloc[:,cols-1:cols]
m = len(y)
def computeCost(X, y, theta):
    inner = np.power(((X * theta) - y), 2)
    return np.sum(inner) / (2 * len(X))

X.head()

        Ones Population
    0   1   6.1101
    1   1   5.5277
    2   1   8.5186
    3   1   7.0032
    4   1   5.8598
y.head()

    Profit
    0   17.5920
    1   9.1302
    2   13.6620
    3   11.8540
    4   6.8233

X = np.matrix(X.values)
y = np.matrix(y.values)
theta = np.zeros((2, 1))
X.shape, theta.shape, y.shape
>>((97, 2), (2, 1), (97, 1))


computeCost(X,y,theta)
theta.size
np.zeros(theta.shape)


下面是改动最大的一部分,数据读取和处理部分我基本是照抄的:

from IPython import display

def gradientDescent(X, y, theta, alpha, iters):
    x = np.linspace(data.Population.min(), data.Population.max(), 100)
    temp = np.zeros(theta.shape)
    pars = theta.size
    cost = np.zeros(iters)
    for i in range(iters):
        if len(theta) == 3:
            f = theta[0, 0] + (theta[1, 0] * x) + theta[2,0] * x
        else:
            f = theta[0, 0] + (theta[1, 0] * x) 
            
        plt.plot(x, f, 'r', label='Prediction')
        plt.scatter(data.Population, data.Profit, label='Traning Data')
        plt.xlabel('Population')
        plt.ylabel('Profit')
        plt.title('Predicted Profit vs. Population Size --> %s'%i)
        plt.show()
        display.clear_output(wait=True)
        for j in range(pars):
            temp[j,0] = theta[j,0] - (alpha/len(X) * np.sum(np.multiply((X * theta - y), X[:,j])))
        theta = temp
        cost[i] = computeCost(X,y,theta)   

    return theta, cost


iterations = 1500; 
alpha = 0.01;
g, cost = gradientDescent(X, y, theta, alpha, iterations)
g
红方框里面是显示迭代次数
computeCost(X, y, g)
fig, ax = plt.subplots(figsize=(12,8))
ax.plot(np.arange(iterations), cost, 'r')
ax.set_xlabel('Iterations')
ax.set_ylabel('Cost')
ax.set_title('Error vs. Training Epoch')
plt.show()

正文之后

还有一个多变量的版本我没写了。洗澡去~ 🛀

算了,复制粘贴改几个参数多大事~

path =  'ex1data2.txt'
data2 = pd.read_csv(path, header=None, names=['Size', 'Bedrooms', 'Price'])
data2.head()
data2 = (data2 - data2.mean()) / data2.std()
data2.head()
# add ones column
data2.insert(0, 'Ones', 1)


# set X (training data) and y (target variable)
cols = data2.shape[1]
iters = 1000
X2 = data2.iloc[:,0:cols-1]
y2 = data2.iloc[:,cols-1:cols]

# convert to matrices and initialize theta
X2 = np.matrix(X2.values)
y2 = np.matrix(y2.values)
theta2 = np.matrix(np.array([0,0,0]).reshape(3,1))

# perform linear regression on the data set
g2, cost2 = gradientDescent(X2, y2, theta2, alpha, iters)

# get the cost (error) of the model
computeCost(X2, y2, g2)

相关文章

  • 吴恩达《机器学习》线性回归python版本实现(增图例动态更新

    正文之前 也不知道是循着什么路径,我又看了一次机器学习的入门必备--线性回归,索性看看这号称最好的入门教程《机器学...

  • 记录一下jupyter notebook上跑python

    前言 刚学完吴恩达老师的机器学习课程线性回归部分,想跑一下找到的python版本的小作业,就开始纠结选什么软件来编...

  • 线性回归--原理

    线性回归--原理 线性回归--python实现(不使用框架) 线性回归--sklearn框架实现 通常我们学习机器...

  • 2 逻辑回归

    逻辑回归是线性回归的变形,看了很多机器学习书籍,吴恩达的课程对线性回归和逻辑回归的讲解非常清晰,原理性和推导都很好...

  • 吴恩达deep_learning_week2_logistic回

    吴恩达deep_learning_week2_logistic回归 标签: 机器学习深度学习 这是吴恩达深度学习里...

  • 吴恩达机器学习:线性回归

    首先说一些关于课程的题外话。对于 Ng 的这个课程,笔者没有选择在 Coursera 上学习课程,一来是因为 Co...

  • 线性回归(吴恩达机器学习)

    回归是机器学习中最经典的算法,它的意思就是根据之前的数据找出某种规律(可以是线性,也可以是非线性),构建模型实现预...

  • 机器学习笔记

    学习记录,从小白做起。 传统给机器学习 先来镇楼的,吴恩达机器学习:吴恩达机器学习 OCTAVE版本下载:http...

  • Python实现梯度下降算法求多元线性回归(一)

    预备知识及相关文档博客 学习吴恩达机器学习课程笔记,并用python实现算法 python numpy基本教程: ...

  • 单变量线性回归

      本文档基于吴恩达机器学习课程单变量线性回归部分内容,记录自己对该部分内容的理解,并给出了MATLAB代码实现,...

网友评论

    本文标题:吴恩达《机器学习》线性回归python版本实现(增图例动态更新

    本文链接:https://www.haomeiwen.com/subject/keozfhtx.html