梯度下降的线性回归用python_机器学习入门-线性拟合和梯度下降Python实现

import math

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

data = pd.read_csv('ex1data1.txt',header=None,names = ['Population','Profit'])

data.plot(kind = 'scatter',x = 'Population',y = 'Profit',figsize = (12,8))

'''

绘制散点图

种类:scatter,定义x与y轴所代表数值.

plot.show()

'''

def computeCost(X,y,theta):

inner = np.power(((X*theta.T)-y),2)

return np.sum(inner)/(2*len(X))

data.insert(0,'all_1',1)

cols = data.shape[1]

X = data.iloc[:,0:cols-1]

y = data.iloc[:,cols-1:cols]

'''

由于代价函数为numpy类型下的矩阵也就是matrix

我们需要转换xy将他变成matrix

'''

X = np.matrix(X.values)

y = np.matrix(y.values)

print(X)

print(type(y))

'''

theta是一个shape为(1,2)的矩阵

也就是一维二阶矩阵,对应的就是两个变化参数值

'''

theta = np.matrix(np.array([0,0]))

print(computeCost(X,y,theta))

'''

batch gradient decent

'''

print(int(theta.ravel().shape[0]))

'''

def ravel(order='C')

Return a flattened matrix.

order : {'C', 'F', 'A', 'K'}, optional

The elements of `m` are read using this index order. 'C' means to

index the elements in C-like order, with the last axis index

changing fastest, back to the first axis index changing slowest.

'F' means to index the elements in Fortran-like index order, with

the first index changing fastest, and the last index changing

slowest. Note that the 'C' and 'F' options take no account of the

memory layout of the underlying array, and only refer to the order

of axis indexing.  'A' means to read the elements in Fortran-like

index order if `m` is Fortran *contiguous* in memory, C-like order

otherwise.  'K' means to read the elements in the order they occur

in memory, except for reversing the data when strides are negative.

By default, 'C' index order is used.

Returns

ravel()

降维函数,对应参数'F',按列降维.

shape(1)代表获取第一维长度

shape(0)代表获取第二维长度

'''

def gradient_decent(X,y,theta,alpha,iters):

temp = np.matrix(np.zeros(theta.shape))

#创建theta容器,以存放theta

parameters = int(theta.ravel().shape[1])

#创建迭代器,以迭代theta

cost = np.zeros(iters)

#创建代价矩阵,储存errors

for i in range(iters):#1000次迭代

error = (X * theta.T) - y#计算

print('error:',error)

for j in range(parameters):

term = np.multiply(error,X[:,j])#矩阵相乘

temp[0,j] = theta[0,j] - ((alpha/len(X))*np.sum(term))#求cost并且求和,后半部分就是针对各自的theta求偏导

theta = temp#通过矩阵更新,来同时更新对应值.

cost[i] = computeCost(X,y,theta)#对应计算不同的cost值,并返回给对象

return theta,cost

alpha = 0.01

iters = 1000

g,cost = gradient_decent(X,y,theta,alpha,iters)

print(g)

x = np.linspace(data.Population.min(), data.Population.max(), 100)

f = g[0, 0] + (g[0, 1] * x)

fig, ax = plt.subplots(figsize=(12,8))

ax.plot(x, f, 'r', label='Prediction')

ax.scatter(data.Population, data.Profit, label='Traning Data')

ax.legend(loc=2)

ax.set_xlabel('Population')

ax.set_ylabel('Profit')

ax.set_title('Predicted Profit vs. Population Size')

plt.show()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值