import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
import pandas as pd
#獲取數據
data = pd.read_csv('ex1data1.txt',names=['population', 'profit'])
X = data['population']
y = data['profit'] #使用列名作爲索引
m = len(y)
#爲矩陣X增加列
ones = pd.DataFrame({'ones': np.ones(m)}) #學習一下
X = pd.concat([ones, X], axis=1)
#參數個數即爲X特徵值
theta = np.zeros(X.shape[1])
#獲取代價函數,單獨定義是:theta不同對應的代價函數不同
def J_function(X,y,theta):
h_theta = X.dot(theta) #矩陣乘積
cost = ( (h_theta - y).dot((h_theta - y).T) ) / (2*m)
return cost
#梯度下降算法
def gradient(X,y,theta):
gradient = (X.T).dot( X.dot(theta) - y )
return gradient/m
def batch_gradient_descent(X,y,theta,alpha,iterations):
cost_data = [J_function(X,y,theta)]
_theta = theta.copy()
for _ in range(iterations):
_theta = _theta - alpha * gradient(X,y, _theta)
cost_data.append( J_function(X,y,theta) )
return _theta, cost_data
alpha = 0.01
iterations = 500
final_theta, cost_data = batch_gradient_descent(X, y, theta ,alpha, iterations)
final_costdata = J_function( X,y,final_theta)
b = final_theta[0]
k = final_theta[1]
plt.scatter(data['population'],data['profit'], label='Training data')
plt.plot(data['population'],data['population']*k + b , label='Prediction')
plt.xlabel(('Population of City in 10,000s')),plt.ylabel(('Profit in $10,000s'))
plt.show()
機器學習Python版 ex1:單變量線性迴歸
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.