import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
def compute_loss(X,y,theta):
loss = (X.dot(theta.T)-y)**2
return loss.sum()/2/m
def gradient_descent(X_X,y,theta,alpha,epochs):
for i in range(epochs):
a = theta[0][0]-alpha*np.sum((1/m) * (X_X.dot(theta.T) - y))
theta =np.array([[a,theta[0][1],theta[0][2]]])
a = theta[0][1]-alpha*np.sum((1/m) * X_X[:,1].reshape(47,1) * (X_X.dot(theta.T) - y))
theta =np.array([[theta[0][0],a,theta[0][2]]])
a = theta[0][2]-alpha*np.sum((1/m) * X_X[:,2].reshape(47,1) * (X_X.dot(theta.T) - y))
theta =np.array([[theta[0][0],theta[0][1],a]])
return theta
data = pd.read_csv("ex1data2.txt",header = None)
#特征归一化,因为数据太大,会超出运算
data = (data - data.mean()) / data.std()
data = data.values
x = data[:,0:2]
y = data[:,2].reshape(47,1)
m = len(x)
x_0 = np.ones([1,47])
X = np.insert(x,0,values = x_0,axis = 1)
theta = np.array([0,0,0]).reshape(1,3)
alpha = 0.01
epochs = 1500
print("Starting theta_0 = {0}, theta_1 = {1},theta_2={2} error = {3}".format(theta[0][0], theta[0][1], theta[0][2], compute_loss(X,y,theta)))
print("Running...")
theta = gradient_descent(X, y, theta, alpha, epochs)
print("After {0} iterations theta_0 = {1}, theta_1 = {2},theta_2={3} error = {4}".format(epochs, theta[0][0], theta[0][1], theta[0][2] ,compute_loss(X,y,theta)))
有不明白的地方可以加群讨论(没错只有我一个人)484266833