这一节我们实践逻辑回归,哎又折腾了一番。废话少说!
教程链接:http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=MachineLearning&doc=exercises/ex4/ex4.html
1、理论
(1)数据加载,绘制数据
数据加载的代码和前两篇博客没有什么不同,可以去第一篇博客查找代码,加载数据后,绘制数据图
x = data.load_data('ex4x.dat') y = data.load_label('ex4y.dat') #绘图 pos = find(y,1.0) neg = find(y,0.0) fig = plt.figure(1)#创建一个图像实例,也保证下面所绘制的东西都在一个图像中,很重要 fig = plt.xlabel(r'Exam 1 score') fig = plt.ylabel(r'Exam 2 score') fig = plt.title(r'LG') #fig = plt.legend() for i in pos: plt.plot(x[i][1],x[i][2],'b+') for i in neg: plt.plot(x[i][1],x[i][2],'bo')结果:
结果还可以哈!我们发现代码中有个find()函数,那个是我自己编写的,目的是返回列表下标
def find(List,x): return [ i for i in range(len(List)) if List[i] == x](2)加载完数据,就是根据公式,编写计算部分了
代码:
for i in range(1200): h = exp(np.dot(x,theat.transpose()))#此处的exp是自己编写的函数 J = 1.0/m*((-1)*np.dot(y,np.log(h)) - (np.dot((1-y),np.log(1-h)))) det_t = 1.0/m*(np.dot(x.transpose(),(h-y.transpose()))) temp = np.multiply(h,(1-h)) h_x = np.multiply(temp,x) H = np.dot(h_x.transpose(),x) theat = theat - np.dot(H.I,det_t).transpose() print theat这需要注意的是矩阵的点乘和矩阵乘法
np.dot(A,B)是矩阵的普通乘法,A是2X1,B是1X2,结果是2x2的矩阵
np.multiply(A,B)是A,B矩阵相对应元素相乘,因此A,B的维度必须相同
Python 求矩阵的逆
H.I 就是计算H矩阵的逆矩阵
关于更详细的Python矩阵操作可以参考我的另一篇博客:http://blog.csdn.net/sunshine_in_moon/article/details/50278723
经过循环后theat就可以收敛了。
(3)画图。得到收敛的theat后,把结果画到到图像上,方便观察
def Y(theat,x): #print theat[0,0],theat[0,1] y = (-1)*(theat[0,0]+theat[0,1]*x)/theat[0,2] return y
x0 = np.linspace(10,70,50) y0 = Y(theat,x0) plt.plot(x0,y0)结果:
(4)预测自己的数据,这一步我没有做。大家可以尝试。根据公式y = theat*x。
注意的问题是要将自己数据x第一列添加1.0.
完整代码:
data.py
def load_data(data_name): dataMat = [] fr = open(data_name) lines = fr.readlines() for line in lines: lineArr = line.strip().split() dataMat.append([1.0,float(lineArr[0]),float(lineArr[1])]) fr.close() return dataMat def load_label(label_name): labelMat = [] fr = open(label_name) lines = fr.readlines() for line in lines: lineArr = line.strip().split() labelMat.append(float(lineArr[0])) fr.close() return labelMat if __name__== "__main__": x = load_data('ex4x.dat')
<span style="font-size:18px;">import matplotlib.pyplot as plt import data import numpy as np def find(List,x): return [ i for i in range(len(List)) if List[i] == x] def exp(x): return 1.0/(1.0+np.exp(-x)) def Y(theat,x): #print theat[0,0],theat[0,1] y = (-1)*(theat[0,0]+theat[0,1]*x)/theat[0,2] return y if __name__ == "__main__": x = data.load_data('ex4x.dat') y = data.load_label('ex4y.dat') #绘图 pos = find(y,1.0) neg = find(y,0.0) fig = plt.figure(1) fig = plt.xlabel(r'Exam 1 score') fig = plt.ylabel(r'Exam 2 score') fig = plt.title(r'LG') #fig = plt.legend() for i in pos: plt.plot(x[i][1],x[i][2],'b+') for i in neg: plt.plot(x[i][1],x[i][2],'bo') x = np.mat(x) y = np.mat(y) m,n = np.shape(x) theat = np.mat(np.zeros(n)) for i in range(1200): h = exp(np.dot(x,theat.transpose())) J = 1.0/m*((-1)*np.dot(y,np.log(h)) - (np.dot((1-y),np.log(1-h)))) det_t = 1.0/m*(np.dot(x.transpose(),(h-y.transpose()))) temp = np.multiply(h,(1-h)) h_x = np.multiply(temp,x) H = np.dot(h_x.transpose(),x) theat = theat - np.dot(H.I,det_t).transpose() print theat x0 = np.linspace(10,70,50) y0 = Y(theat,x0) plt.plot(x0,y0)</span>欢迎与我多多交流!