吳恩達機器學習第一次作業 Linear Regression(基於Python實現)

1.數據處理部分

1.1 加載數據

data_file_path = "ex1/ex1data1.txt"  # 此處需根據你的數據文件的位置進行相應的修改
data = np.loadtxt(data_file_path, dtype=np.float, delimiter=',')

1.2 分割X和y

X = data[:, 0:1]
y = data[:, 1:]

1.3 繪圖展示數據

fig = plt.figure(num=1, figsize=(15,8), dpi=80)
plt.scatter(X, y)
plt.xlabel('Population of City in 10,000s')
plt.ylabel("Profit in $10,000s")
plt.show()

1.4 給X加一列全爲1的向量

one_col = np.ones((X.shape[0], 1))
X = np.column_stack((one_col, X))

2.實現線性迴歸算法

2.1 初始化參數

n = X.shape[1]
m = y.shape[0]
theta = np.zeros(2)
alpha = 0.01
iterations = 1500

2.2 實現損失函數

def computeCost(X, y, theta):
    return np.sum((np.dot(X, theta).reshape(m,1) - y)**2) / (2*m)

此時,可以測試一下損失函數的正確性

print("%.2f" % computeCost(X, y, theta)) 
print("此處的結果應該是32.07")

2.3 實現梯度下降

def gradientDescent(X, y, theta, alpha, iterations):
    for i in range(iterations):
        g = np.dot((np.dot(X, theta).reshape(m,1) - y).T, X) / m
        theta -= alpha * g.reshape(n,)
        # 觀察損失函數的變化
        if i % 100 == 0:
            loss = computeCost(X, y, theta)
            print("第i次的損失函數值爲%.2f" % loss)
    return theta

# 直接得到最終模型
theta = gradientDescent(X, y, theta, alpha, iterations)

測試一下

print(theta)
print("此處的結果應該是-3.6303 1.1664")

2.4 將得到的直線繪製出來,觀察擬合程度

plt.xlabel('Population of City in 10,000s')
plt.ylabel("Profit in $10,000s")
plt.scatter(X[:, 1:], y )
plt.plot(X[:, 1:],  theta[0] + theta[1] * X[:, 1:], color='g')
plt.legend(['Linear regression', 'Training data'])
plt.show()

原始數據與模型的預測對比

3.完整代碼

# coding=utf-8
import numpy as np
import matplotlib.pyplot as plt

# 1.數據處理部分
# 1.1加載數據
data_file_path = "ex1/ex1data1.txt"  # 此處需根據你的數據文件的位置進行相應的修改
data = np.loadtxt(data_file_path, dtype=np.float, delimiter=',')

# 1.2分割X和y
X = data[:, 0:1]
y = data[:, 1:]

# 1.3繪圖展示數據
# fig = plt.figure(num=1, figsize=(15,8), dpi=80)
# plt.scatter(X, y)
# plt.xlabel('Population of City in 10,000s')
# plt.ylabel("Profit in $10,000s")
# plt.show()

# 1.4給X加一列全爲1的向量
one_col = np.ones((X.shape[0], 1))
X = np.column_stack((one_col, X))
# print(X[0:5])

# 2.實現線性迴歸算法
# 2.1 初始化參數
n = X.shape[1]
m = y.shape[0]
theta = np.zeros(2)
alpha = 0.01
iterations = 1500


# 2.2 實現損失函數
def computeCost(X, y, theta):
    return np.sum((np.dot(X, theta).reshape(m,1) - y)**2) / (2*m)

# print("%.2f" % computeCost(X, y, theta)) 
# print("此處的結果應該是32.07")

# 2.3 實現梯度下降
def gradientDescent(X, y, theta, alpha, iterations):
    for i in range(iterations):
        g = np.dot((np.dot(X, theta).reshape(m,1) - y).T, X) / m
        theta -= alpha * g.reshape(n,)
        # 觀察損失函數的變化
        if i % 100 == 0:
            loss = computeCost(X, y, theta)
            print("第i次的損失函數值爲%.2f" % loss)
    return theta

theta = gradientDescent(X, y, theta, alpha, iterations)
# print(theta)
# print("此處的結果應該是-3.6303 1.1664")

# 2.4 將得到的直線繪製出來,觀察擬合程度
# fig = plt.figure(num=1, figsize=(15,8), dpi=80)
plt.xlabel('Population of City in 10,000s')
plt.ylabel("Profit in $10,000s")
plt.scatter(X[:, 1:], y )
plt.plot(X[:, 1:],  theta[0] + theta[1] * X[:, 1:], color='g')
plt.legend(['Linear regression', 'Training data'])
plt.show()

如有不當之處,歡迎指出!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章