吴恩达机器学习第一周作业
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = np.loadtxt('ex1data1.txt', delimiter=',')
x = data[:, 0]
y = data[:, 1]
m = y.size
y = y.reshape(-1, 1)
plt.figure(0)
fg1 = plt.scatter(x, y, marker='x', c='r')
plt.xlabel('profit in $10,000s')
plt.ylabel('Population of City in 10,000s')
# 正规方程法
# x = np.column_stack((np.ones([m, 1]), x))
# theta = np.linalg.inv(x.T@x)@x.T@y
# print(theta)
# 代价函数
def computerCost(X, Y, Theta):
inner = X @ Theta.T - Y
J = np.sum(inner * inner) / (2 * Y.size)
return J
# 梯度下降法
x = np.column_stack((np.ones([m, 1]), x))
theta = np.zeros([1, 2])
alpha = 0.01
epoch = 1000
# 梯度下降函数
def gradientDescent(X, Y, Theta, Alpha=0.01, Epoch=1000):
Cost = np.zeros([Epoch, 1])
for i in range(Epoch):
inner = X @ Theta.T - Y
for j in range(Theta.size):
Xg = X[:, j].reshape(-1, 1)
inner = inner * Xg
Theta[0, j] = Theta[0, j] - (Alpha/m) * np.sum(inner)
Cost[i] = computerCost(X, Y, Theta)
return Cost, Theta
cost, theta = gradientDescent(x, y, theta, alpha, epoch)
plt.figure(0)
x2 = np.linspace(x[:, 1].min(), x[:, 1].max(), 100)
y2 = theta[0, 0] + theta[0, 1] * x2
fg2 = plt.plot(x2, y2)
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201108203620647.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3NDg5OTA2,size_16,color_FFFFFF,t_70#pic_center)