# -*- coding: utf-8 -*-
"""
Created on Tue Jun 30 12:48:02 2020
@author: cheetah023
"""
import numpy as np
import matplotlib.pyplot as plt
#函数定义
def featureNormalize(X):
m = X.shape[0]
mu = X.mean(axis=0)
sigma = X.std(axis=0)
mu_mat = np.tile(mu,(m,1))
sigma_mat = np.tile(sigma,(m,1))
X_norm = (X - mu_mat) / sigma_mat
return X_norm, mu, sigma
def computeCostMulti(X, y, theta):
J = np.sum((np.dot(X,theta) - y) ** 2) / (2 * X.shape[0])
return J
def gradientDescentMulti(X, y, theta, alpha, num_iters):
m = X.shape[0]
J_history = np.zeros((num_iters,1))
for i in range(0,num_iters):
theta = theta - (alpha / m) * np.dot(X.T, (np.dot(X,theta) - y))
J_history[i] = computeCostMulti(X, y, theta)
return theta,J_history
def normalEqn(X, y):
R = np.linalg.pinv(np.dot(X.T,X))
theta = np.dot(R,np.dot(X.T,y))
return theta
#Part 1: Feature Normalization
data = np.loadtxt("ex1data2.txt",delimiter=',')
X = data[:,0:2]
y = data[:,2:3]
print('X:',X.shape)
print('y:',y.shape)
(X,mu,sigma) = featureNormalize(X)
print('X_norm:\n',X[0:4,:])
print('mu:',mu,"\nsigma",sigma)
m = X.shape[0]
#print('m:',m)
ones = np.ones((m,1))
X = np.column_stack((ones,X))
#print('X:',X.shape)
#Part 2: Gradient Descent
alpha = 0.01;
num_iters = 400;
theta = np.zeros((3,1))
(theta, J_history) = gradientDescentMulti(X, y, theta, alpha, num_iters)
print('Theta computed from gradient descent:\n',theta)
#plot J_history
plt.plot(range(len(J_history)),J_history)
plt.xlabel('Number of iterations')
plt.ylabel('Cost J')
price = np.dot([1,1650,3],theta)
print('Predicted price of a 1650 sq-ft, 3 br house')
print('(using gradient descent):${}'.format(price))
#Part 3: Normal Equations
X = data[:,0:2]
y = data[:,2:3]
X = np.column_stack((ones,X))
theta = normalEqn(X, y)
print('Theta computed from the normal equations:\n',theta)
price = np.dot([1,1650,3],theta)
print('Predicted price of a 1650 sq-ft, 3 br house')
print('(using normal equations):${}'.format(price))
运行结果:
X: (47, 2) y: (47, 1) X_norm: [[ 0.13141542 -0.22609337] [-0.5096407 -0.22609337] [ 0.5079087 -0.22609337] [-0.74367706 -1.5543919 ]] mu: [2000.68085106 3.17021277] sigma [7.86202619e+02 7.52842809e-01] Theta computed from gradient descent: [[334302.06399328] [ 99411.44947359] [ 3267.01285407]] Predicted price of a 1650 sq-ft, 3 br house (using gradient descent):$[1.64372995e+08] Theta computed from the normal equations: [[89597.90954361] [ 139.21067402] [-8738.01911255]] Predicted price of a 1650 sq-ft, 3 br house (using normal equations):$[293081.46433499]
参考资料:
https://blog.csdn.net/lccflccf/category_8379707.html
https://blog.csdn.net/Cowry5/article/details/83302646
https://blog.csdn.net/weixin_44027820/category_9754493.html