线性回归

引言:我们真正的需要的是一种有效的算法,能够自动地找出这些使代价函数J取最小值的参数theta0和theta1来。

一:__init__.py(主函数)

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import Cost_function as cf
import batch_gradient_decent as bgd

path =  r'******'
data = pd.read_csv(path, header = None, names = ['Population', 'Profit']) #读取数据
# data.plot(kind = 'scatter', x = 'Population', y = 'Profit', figsize = (12,8))
# plt.show() #绘制散点图

data.insert(0, 'Ones', 1) #设置x0=1方便数据运算

cols = data.shape[1] #简单理解为数行数和列数,0:行,1:列
X = data.iloc[:, 0: cols - 1] #iloc 取值操作
y = data.iloc[:, cols - 1: cols]

X = np.matrix(X.values) #变成矩阵
y = np.matrix(y.values)
theta = np.matrix(np.array([0,0]))

#单变量线性回归---代价函数计算
cf_num = cf.computeCost(X, y, theta)
print(cf_num)

#批量梯度下降
alpha = 0.01
iters = 1000

g, cost = bgd.gradientDecent(X, y, theta, alpha, iters)
#使用我们最后拟合的参数theta来计算训练模型的代价函数(误差)
bgd_num = cf.computeCost(X, y, g)
print(bgd_num)

#多变量线性回归
path2 =  r'******'
data2 = pd.read_csv(path2, header=None, names=['Size', 'Bedrooms', 'Price'])

#对于多变量进行归一化预处理
data2 = (data2 - data2.mean()) / data2.std()

data2.insert(0, 'Ones', 1)
cols2 = data2.shape[1]
X2 = data2.iloc[:, 0: cols2 - 1]
y2 = data2.iloc[:, cols2 - 1: cols2]

X2 = np.matrix(X2.values)
y2 = np.matrix(y2.values)
theta2 = np.matrix(np.array([0,0,0]))

g2, cost2 = bgd.gradientDecent(X2, y2, theta2, alpha, iters)
bgd_num2 = cf.computeCost(X2, y2, g2)
print(bgd_num2)

二:Cost_function.py(代价函数)

#代价函数
import numpy as np

def computeCost(X, y, theta):
    inner = np.power(X * theta.T - y, 2)
    return (np.sum(inner)) / (2*len(X))

 三:batch_gardient_decent.py(梯度下降)

#梯度下降函数
import numpy as np
import Cost_function as cf

#函数需要参数变量X和y,θ:theta, α:alpha, 数据数量:iters
def gradientDecent(X, y, theta, alpha, iters):
    temp = np.matrix(np.zeros(theta.shape)) #用temp存储每次迭代更新的theta
    parameters = int(theta.ravel().shape[1]) #相当于计算出一共有几个参数,包括了自定义的x0=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)) * np.sum(term)

        theta = temp
        cost[i] = cf.computeCost(X, y, theta)

    return theta, cost

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值