吴恩达ex1——单变量线性回归

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#加载数据
df=pd.read_csv('ex1data1-Copy1.txt',header=None,names=['population','profit'])
df.head()
populationprofit
06.110117.5920
15.52779.1302
28.518613.6620
37.003211.8540
45.85986.8233
df_np=df.values
df_np=np.insert(df_np,0,1.0,axis=1)
df_np.shape
(97, 3)
X=df_np[:,0:2]
X=np.matrix(X)
X.shape
(97, 2)
#绘图
x1=df.iloc[:,0].values
y=df.iloc[:,1].values
plt.scatter(x1,y,color='r')
plt.xlabel('population')
plt.ylabel('profit')
plt.show()

在这里插入图片描述

#将y变成列向量
y=y.reshape(97,1)
y=np.matrix(y)
y.shape
(97, 1)

梯度下降实现

'X:97x2'
'theta:列向量,2x1 '

#定义损失函数
def cost(X,y,theta):
    inn=np.sum(np.power((X*theta-y),2))
    return inn/(2*len(y))
    
theta=[0.1,0.1]
theta=np.matrix(theta).reshape(2,1)
theta.shape
(2, 1)
loss_0=cost(X,y,theta)
loss_0
25.449553111855668
#更新theta
def GredientDec(X,y,theta,iters,alpha):
    parameters=X.shape[1]
    loss=np.zeros((iters,1))
    theta_fig=theta
    
    for a in range(iters):
        error=(X*theta-y)
        
        for j in range(parameters):
            term=np.sum(np.multiply(error,X[:,j]))
            theta[j]=theta[j]-(alpha*term)/len(y)
            loss[a]=cost(X,y,theta)
        
      
            
    return theta,loss
np.seterr(invalid='ignore')
theta,loss=GredientDec(X,y,theta=theta,iters=1000,alpha=0.01)
theta
matrix([[-3.78565572],
        [ 1.18197038]])
loss_new=cost(X,y,theta)
loss_new
4.478075461131649
x=np.linspace(X[:,1].min(),X[:,1].max(),100)
y_fig=theta[0]+theta[1]*x
y_fig=y_fig.reshape(100,1)
x1=df.iloc[:,0].values
y=df.iloc[:,1].values
plt.scatter(x1,y,color='r')
plt.plot(x,y_fig,color='k')
plt.xlabel('population')
plt.ylabel('profit')
plt.show()

在这里插入图片描述

绘制损失函数

iters=1000
plt.plot(np.arange(iters),loss,color='r')
plt.xlabel('numbers of iter')
plt.ylabel('loss of J(θ)')
plt.show()

绘制等高线图

J=[]
for i in np.arange(-10,10,0.1):
    for j in np.arange(-10,10,0.1):
        theta=np.matrix([i,j]).reshape(2,1)
        J.append(cost(X,y,theta=theta))
J=np.array(J).reshape(200,200)
plt.contour(np.arange(-10,10,0.1),np.arange(-10,10,0.1),J,levels=20)

<matplotlib.contour.QuadContourSet at 0x173d9b6a8b0>

在这里插入图片描述


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值