呉恩達機械学習作業python実現--多変数線形回帰


たへんすうせんけいかいき
対価関数J(θ ) = 1 2 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) 2 J\left(\theta\right)=\frac{1}{2m}\sum\limits_{i=1}^{m}{ { {\left( { {h}_{\theta }}\left( { {x}^{(i)}}\right)-{ {y}^{(i)}}\right)}^{2}}} J(θ)=2m1​i=1∑m​(hθ(x(i)−y(i))2仮定関数hθ ( x ) = θ T X = θ 0 x 0 + θ 1 x 1 + θ 2 x 2 + . . . + θ n x n { {h}_{\theta }}\left( x\right)={ {\theta }^{T}}X={ {\theta }_{0}}{ {x}_{0}}+{ {\theta }_{1}}{ {x}_{1}}+{ {\theta }_{2}}{ {x}_{2}}+...+{ {\theta }_{n}}{ {x}_{n}} hθ​(x)=θTX=θ0​x0​+θ1​x1​+θ2​x2​+...+θn​xn​
#           ,    1,   theta0  ,        
data2 = data2_0
data2.insert(0, 'Ones', 1)# insert             ,        ,      value

#               
cols = data2.shape[1] #    ,       ,           
X2_0 = data2.iloc[:,0:cols-1] #      
y2_0 = data2.iloc[:,cols-1:cols] #     , 1        0    

# theta   ,       
X2 = np.matrix(X2_0.values)   #   np.mat(x2_0)     
y2 = np.matrix(y2_0.values)   
theta2 = np.matrix(np.array([0,0,0]))   #   np.mat([2,2,2])     
#       ,            ,theta          
#  -1               ,          ,            ,  astype      
theta2 = np.mat(np.zeros(cols-1)).reshape(1,-1).astype(int)

'''
    theta   3  ,        ,  、             ,
      
h(theta) = theta0*x1 + theta1*x2 + theta2*x3    x1=1
            ,            cost
         ,                        theta  ,
     cost     
'''
#cost  
def computeCost(X, y, theta):
    #       
    tmp = np.power((X*theta.T -y),2)
    return np.sum(tmp)/(2*len(X))
'''
      ,                       theta               ,               
              
'''
#    
def gradientDescent(X,y,theta,alpha,iters):
    
    theta_cn = theta.shape[1]  #  len()   theta2   ,    1
    N = len(X)  #    
    cost = np.zeros(iters)  #               
    temp = np.mat(np.zeros(theta.shape))  #     theta2,      
    
    for i in range(iters):
        error = (X*theta.T-y)
        for j in range(theta_cn):
            term = np.multiply(error,X[:,j])
            temp[0,j] = theta[0,j] -(alpha/len(X)*np.sum(term))
        #  temp         theta0、theta1  theta,    i                
        theta = temp
        cost[i] = computeCost(X, y, theta)
    return theta, cost

alpha = 0.01
iters = 1000
#   theta   cost  
g2, cost2 = gradientDescent(X2, y2, theta2, alpha, iters)

#                 
computeCost(X2, y2, g2)

#  cost      
fig, ax = plt.subplots(figsize=(12,8))
ax.plot(np.arange(iters), cost2, 'r')
ax.set_xlabel('Iterations')
ax.set_ylabel('Cost')
ax.set_title('Error vs. Training Epoch')
plt.show()