-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex1: Linear Regression
83 lines (72 loc) · 3.07 KB
/
ex1: Linear Regression
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ex1: Linear Regression
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#輸入5*5單位矩陣
A = np.eye(5)
print(A)
#根據城市人口數量,預測小吃店的利潤,其中數據在ex1data1.txt裡,其中第一列是都市人口數量,而第二列是該城市小吃店利潤
path = 'Coursera-ML-AndrewNg-Notes-master\\code\\ex1-linear regression\\ex1data1.txt'
data = pd.read_csv(path, header=None, names=['Population', 'Profit'])
data.head()
data.plot(kind='scatter', x='Population', y='Profit', figsize=(12,8))
plt.show()
#利用梯度下降 這個部分計算J(Ѳ)cost function,X是矩陣,theta.T是指係數要轉置 h_θ (x)=θ^T X=θ_0+θ_1 x_1+θ_2 x_2+...+θ_n x_n
def computeCost(X, y, theta):
inner = np.power(((X * theta.T) - y), 2)
return np.sum(inner) / (2 * len(X))
cols = data.shape[1]
X = data.iloc[:,:-1].as_matrix()#X是data裡的除了最後列
y = data.iloc[:,cols-1:cols]#y是data最後一列
theta = np.zeros(X.shape[1])
computeCost(X, y, theta)
#加入一列x,用於更新theta,然後我們將初始化為0,學習率初始化為0.01,迭代次數為1500次
data.insert(0, 'Ones', 1) #其中1是代表axis=1 是直的array其標題是Ones
# 初始化X和y shape可顯示martix幾行幾列,shape[0]代表幾行,shape[1]代表幾列
#loc函數:通過行索引 "Index" 中的具體值來取行數據(如取"Index"為"A"的行) iloc函數:通過行號來取行數據(如取第幾行的數據)
cols = data.shape[1]
X = data.iloc[:,:-1] #X是data裡的除了最後列
y = data.iloc[:,cols-1:cols] #y是data最後一列
X.head()
y.head()
#轉換成矩陣
X = np.matrix(X.values)
y = np.matrix(y.values)
theta = np.matrix(np.array([0,0]))
#檢查維度是否正確
print(X.shape, theta.shape, y.shape)
computeCost(X, y, theta)
#梯度下降 檢查是否每一步theta都變小最後收斂到一個穩定的值,其結果用來預測小吃店在35000及70000都市規模人口的利潤
#theta.ravel() 是轉置
def gradientDescent(X, y, theta, alpha, iters):
temp = np.matrix(np.zeros(theta.shape))
parameters = int(theta.ravel().shape[1])
cost = np.zeros(iters)
for i in range(iters):
error = ((X * theta.T) - y)
for j in range(parameters):
term = np.multiply(error, X[:,j])
temp[0,j] = theta[0,j] - ((alpha / len(X)) * np.sum(term))
theta = temp
cost[i] = computeCost(X, y, theta)
return (theta, cost)
#改變學習率
alpha = 0.01
iters = 1500
g, cost = gradientDescent(X, y, theta, alpha, iters)
print(g)
# predict benefit on 35000 and 70000
predict35000 = [1,3.5]*g.T
print("predict35000:",predict35000)
predict70000 = [1,7]*g.T
print("predict70000:",predict70000)
x = np.linspace(data.Population.min(), data.Population.max(), 100)
f = g[0, 0] + (g[0, 1] * x)
fig, ax = plt.subplots(figsize=(12,8))
ax.plot(x, f, 'r', label='Prediction')
ax.scatter(data.Population, data.Profit, label='Traning Data')
ax.legend(loc=2)
ax.set_xlabel('Population')
ax.set_ylabel('Profit')
ax.set_title('Predicted Profit vs. Population Size')
plt.show()