2021-11-12

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_csv(‘ex1data1.txt’,header = None,names=[‘Population’,‘Profits’])
data.head()
data.describe()
fig,axes = plt.subplots()
data.plot(kind=‘scatter’,x=‘Population’,y=‘Profits’,ax=axes,marker=‘o’,color=‘r’)
axes.set(xlabel=‘Population’,ylabel=‘Profits’)
fig.savefig(‘p1.png’)
data.insert(0,‘Ones’,1)
data.head()
def get_fea_lab(data):
cols = data.shape[1]
X = data.iloc[:, 0:cols - 1]
y = data.iloc[:, cols - 1:cols]

X = np.matrix(X.values)
y = np.matrix(y.values)
return X, y

def computeCost(data, theta, i):
X, y = get_fea_lab(data)
inner = np.power(((X * theta.T) - y), 2)
return (float(inner[i] / 2))
def stochastic_gradient_descent(data, theta, alpha, epoch):
X0, y0 = get_fea_lab(data) # 提取X和y矩阵
temp = np.matrix(np.zeros(theta.shape))
parameters = int(theta.shape[1])
cost = np.zeros(len(X0))
avg_cost = np.zeros(epoch)

for k in range(epoch):
    new_data = data.sample(frac=1)  # 打乱数据
    X, y = get_fea_lab(new_data)  # 提取新的X和y矩阵

    for i in range(len(X)):
        error = X[i:i + 1] * theta.T - y[i]
        cost[i] = computeCost(new_data, theta, i)

        for j in range(parameters):
            temp[0, j] = theta[0, j] - alpha * error * X[i:i + 1, j]

        theta = temp
    avg_cost[k] = np.average(cost)

return theta, avg_cost

alpha = 0.001
epoch = 200
theta = np.matrix(np.array([0, 0]))
g, avg_cost = stochastic_gradient_descent(data, theta, alpha, epoch)
fig, axes = plt.subplots()
axes.plot(np.arange(epoch), avg_cost, ‘r’)
axes.set_xlabel(‘Epoch’)
axes.set_ylabel(‘avg_cost’)
axes.set_title(‘avg_cost vs. Epoch’)
fig.savefig(‘p2.png’)
x = np.linspace(data.Population.min(), data.Population.max(), 100)
f = g[0, 0] + g[0, 1] * x

fig, axes = plt.subplots()
axes.plot(x, f, ‘r’, label=‘Fitted’)
axes.scatter(x=data.Population, y=data.Profits, label=‘Trainning data’)
axes.legend(loc=‘best’)
axes.set(xlabel=‘Population’, ylabel=‘Profits’, title=‘Population vs. Profits’)
fig.savefig(‘p3.png’)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值