吴恩达机器学习练习题python实现ex1(Linear regression with multiple variables)

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('ex1data2.txt',names = ['size','bedroom','price']) #打开文件
def normalize_feature(data):           #均值归一化
    return (data - data.mean()) / data.std()
data = normalize_feature(data)
data.insert(0,'ones',1)#插入一列
X = data.iloc[:,0:-1]       #构造数据集
Y = data.iloc[:,-1]
X = np.array(X)      #将dataframe转成数组,方便运算
Y = np.array(Y)
Y.shape = (47,1)
def costFunction(X,Y,theta):#损失函数
    inner = np.power(X @ theta - Y, 2)
    return np.sum(inner) /(2 * len(X))
theta = np.zeros((3,1))
cost_init = costFunction(X,Y,theta)
def gradientDescent(X,Y,theta,alpha,iters,isprint = False):    #梯度下降函数
    costs = []
    for i in range(iters):
        theta = theta - (X.T @ (X @ theta - Y)) * alpha / len(X)
        cost = costFunction(X,Y,theta)
        costs.append(cost)

        if i % 100 == 0:
            if isprint:
                print(cost)
    return theta,costs
candidate_alpha= [0.0003,0.003,0.03,0.0001,0.001,0.01]#不同alpha迭代速度不同
iters = 2000
fig,ax = plt.subplots()
for alpha in candidate_alpha:
    _,costs = gradientDescent(X,Y,theta,alpha,iters)
    ax.plot(np.arange(iters),costs,label = alpha)
    ax.legend()
ax.set(xlabel = 'iters',ylabel = 'cost',title = 'cost vs iters')
plt.show()
def normaEquation(X,Y):  #正规方程求解theta
    theta = np.linalg.inv(X.T @ X) @ X.T @ Y
    return theta   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值