定义
条件概率分布:
P
(
Y
=
1
∣
x
)
=
e
x
p
(
ω
⋅
x
+
b
)
1
+
e
x
p
(
ω
⋅
x
+
b
)
P(Y=1|x)=\dfrac{exp(\omega \cdot x + b)}{ 1 + exp( \omega \cdot x + b )}
P(Y=1∣x)=1+exp(ω⋅x+b)exp(ω⋅x+b)
P
(
Y
=
0
∣
x
)
=
1
1
+
e
x
p
(
ω
⋅
x
+
b
)
P(Y=0|x)=\dfrac{ 1 }{ 1 + exp( \omega \cdot x + b )}
P(Y=0∣x)=1+exp(ω⋅x+b)1
输入:
x
∈
R
n
x \in R^n
x∈Rn
输出:
Y
∈
{
0
,
1
}
Y \in \{ 0,1 \}
Y∈{0,1}
参数:
权值向量:
ω
∈
R
n
\omega \in R^n
ω∈Rn ,
偏置:$b \in R $
ω
和
x
的内积
\omega和x的内积
ω和x的内积:
ω
⋅
x
+
b
\omega \cdot x + b
ω⋅x+b
输入空间
T= { ( x 1 , y 1 ) , ( x 2 , y 2 ) , … , ( x N , y N ) } \left\{(x_1,y_1),(x_2,y_2),\dots,(x_N,y_N)\right\} {(x1,y1),(x2,y2),…,(xN,yN)}
import numpy as np
def loadData(fileName):
'''
加载Mnist数据集 下载地址:https://download.csdn.net/download/nanxiaotao/89720991)
:param fileName:要加载的数据集路径
:return: 数据集
'''
# 存放数据及标记的list
dataList = []; labelList = []
# 打开文件
fr = open(fileName, 'r')
# 将文件按行读取
for line in fr.readlines():
curLine = line.strip().split(',')
if int(curLine[0]) == 0:
labelList.append(1)
else:
labelList.append(0)
dataList.append([int(num)/255 for num in curLine[1:]])
#返回data和label
return dataList, labelList
trainData, trainLabel = loadData('../Mnist/mnist_train.csv')
np.shape(trainData)
特征空间(Feature Space)
trainData[0][0:784]
统计学习方法
模型
P
(
Y
=
1
∣
x
)
=
e
x
p
(
ω
⋅
x
+
b
)
1
+
e
x
p
(
ω
⋅
x
+
b
)
P(Y=1|x)=\dfrac{exp(\omega \cdot x + b)}{ 1 + exp( \omega \cdot x + b )}
P(Y=1∣x)=1+exp(ω⋅x+b)exp(ω⋅x+b)
P
(
Y
=
0
∣
x
)
=
1
1
+
e
x
p
(
ω
⋅
x
+
b
)
P(Y=0|x)=\dfrac{ 1 }{ 1 + exp( \omega \cdot x + b )}
P(Y=0∣x)=1+exp(ω⋅x+b)1
策略
L ( ω ) m a x = m a x ( ∑ i = 1 n [ y i ( ω ⋅ x i ) − l o g ( 1 + e x p ( ω ⋅ x i ) ) ] ) L(\omega)_{max} = max \big(\sum_{i=1}^n\big[ y_i(\omega \cdot x_i)-log(1+exp(\omega \cdot x_i)) \big]\big) L(ω)max=max(∑i=1n[yi(ω⋅xi)−log(1+exp(ω⋅xi))])
算法
x i ∗ y i − e x p ( ω ∗ x i ) ∗ x i 1 + e x p ( ω ∗ x i ) x_i * y_i - \dfrac{exp(\omega * x_i) * x_i}{1 + exp(\omega * x_i)} xi∗yi−1+exp(ω∗xi)exp(ω∗xi)∗xi
def logisticRegression(trainDataList, trainLabelList, iter = 200):
'''
逻辑斯蒂回归训练过程
:param trainDataList:训练集
:param trainLabelList: 标签集
:param iter: 迭代次数
:return: 习得的w
'''
for i in range(len(trainDataList)):
trainDataList[i].append(1)
trainDataList = np.array(trainDataList)
w = np.zeros(trainDataList.shape[1])
#设置步长
h = 0.001
#迭代iter次进行随机梯度下降
for i in range(iter):
#每次迭代冲遍历一次所有样本,进行随机梯度下降
for j in range(trainDataList.shape[0]):
wx = np.dot(w, trainDataList[j])
yi = trainLabelList[j]
xi = trainDataList[j]
#梯度上升
w += h * (xi * yi - (np.exp(wx) * xi) / ( 1 + np.exp(wx)))
#返回学到的w
return w
w = logisticRegression(trainData, trainLabel)
假设空间(Hypothesis Space)
{ f ∣ f ( x ) = m a x ( ∑ i = 1 n [ y i ( ω ⋅ x i ) − l o g ( 1 + e x p ( ω ⋅ x i ) ) ] ) } \left\{f|f(x) = max \big(\sum_{i=1}^n\big[ y_i(\omega \cdot x_i)-log(1+exp(\omega \cdot x_i)) \big]\big) \right\} {f∣f(x)=max(∑i=1n[yi(ω⋅xi)−log(1+exp(ω⋅xi))])}
输出空间
Y ∈ { 0 , 1 } Y \in \{ 0,1 \} Y∈{0,1}
模型评估
训练误差
testData, testLabel = loadData('../Mnist/mnist_test.csv')
def predict(w, x):
'''
预测标签
:param w:训练过程中学到的w
:param x: 要预测的样本
:return: 预测结果
'''
#dot为两个向量的点积操作,计算得到w * x
wx = np.dot(w, x)
#计算标签为1的概率
P1 = np.exp(wx) / (1 + np.exp(wx))
#如果为1的概率大于0.5,返回1
if P1 >= 0.5:
return 1
#否则返回0
return 0
def model_test(testDataList, testLabelList, w):
'''
验证
:param testDataList:测试集
:param testLabelList: 测试集标签
:param w: 训练过程中学到的w
:return: 正确率
'''
for i in range(len(testDataList)):
testDataList[i].append(1)
#错误值计数
errorCnt = 0
#对于测试集中每一个测试样本进行验证
for i in range(len(testDataList)):
#如果标记与预测不一致,错误值加1
if testLabelList[i] != predict(w, testDataList[i]):
errorCnt += 1
#返回准确率
return 1 - errorCnt / len(testDataList)
accuracy = model_test(testData, testLabel, w)
# 打印准确率
print('the accuracy is:', accuracy)