-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex2: Logistic regression
77 lines (63 loc) · 2.86 KB
/
ex2: Logistic 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
# Logistic regression and Regularization
# 在訓練的初始階段,構建一個邏輯回歸模型來預測,某位學生是否被大學錄取。假設你是大學招生官,想通過申請的學生兩次測試來評分,決定是否被錄取。
# 有申請學生的訓練樣本集將其用於訓練邏輯回歸,每一個訓練樣本,有他們兩次測試的評分和最後是錄取的結果。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
path='Coursera-ML-AndrewNg-Notes-master\\code\\ex2-logistic regression\\ex2data1.txt'
data = pd.read_csv(path, header=None, names=['Exam 1', 'Exam 2', 'Admitted'])
data.head() #可顯示前五項資料
# 將資料視覺化,分成錄取以及未錄取
# data['Admitted'].isin 是檢查data中的['Admitted']項,其中isin是看要檢查啥利用此將其標記
positive = data[data['Admitted'].isin([1])]
negative = data[data['Admitted'].isin([0])]
fig, ax = plt.subplots(figsize=(12,8))
ax.scatter(positive['Exam 1'], positive['Exam 2'], s=50, c='b', marker='o', label='Admitted')
ax.scatter(negative['Exam 1'], negative['Exam 2'], s=50, c='r', marker='x', label='Not Admitted')
ax.legend()
ax.set_xlabel('Exam 1 Score')
ax.set_ylabel('Exam 2 Score')
plt.show()
# 邏輯回歸常用函數sigmoid 函數
def sigmoid(z):
return 1 / (1 + np.exp(-z))
# 邏輯回歸常用函數的代價函數(與線性回歸的代價函數略微不同)
# np.dot()是矩陣內積
# 實現對應元素相乘,有2種方式,一個是np.multiply(),另外一個是*
def cost(theta, X, y):
theta = np.matrix(theta)
X = np.matrix(X)
y = np.matrix(y)
first = np.multiply(-y, np.log(sigmoid(X * theta.T)))
second = np.multiply((1 - y), np.log(1 - sigmoid(X * theta.T)))
return np.sum(first - second) / (len(X))
#初始化X,y,θ
# 加一列常數列
data.insert(0, 'Ones', 1)
# 初始化X,y,θ 此時X y是table有label
# 初始化X和y shape可顯示martix幾行幾列,shape[0]代表幾行,shape[1]代表幾列
#https://blog.csdn.net/W_weiying/article/details/81411257
# iloc[0] or loc["a"]是看行,iloc[:,[0]] or loc[:,"a"]是看列,iloc[[0,1],[0,1]] or loc[['a','b'],['A','B']]是看指定行列
cols = data.shape[1]
X = data.iloc[:,0:cols-1]
y = data.iloc[:,cols-1:cols]
theta = np.zeros(3)
# 轉換X,y的類型 此時X y是矩陣沒有label
X = np.array(X.values)
y = np.array(y.values)
# 檢查矩陣的維度
print(X.shape, theta.shape, y.shape)
# 用初始θ計算代價
cost(theta, X, y)
# 實現梯度計算的函數(並沒有更新θ)
def gradient(theta, X, y):
theta = np.matrix(theta)
X = np.matrix(X)
y = np.matrix(y)
parameters = int(theta.ravel().shape[1])
grad = np.zeros(parameters)
error = sigmoid(X * theta.T) - y
for i in range(parameters):
term = np.multiply(error, X[:,i])
grad[i] = np.sum(term) / len(X)
return grad