2020吴恩达 machine learning 编程作业 python实现 ex1_multi

​
# -*- 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
 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值