Logistic回归编程实战
上一节学习了Logistic回归,这一节就针对该算法进行编程实战,所使用的是python2.7。
以下是本项目的全部代码
# -*- coding:utf-8 -*-
from numpy import *
import matplotlib.pyplot as plt
class Logister(object):
def __init__(self,path):
self.path=path
#将导入的文件转换为矩阵形式
def file2matrix(self,delimiter):
recordlist = []
fp = open(self.path,"rb") # 读取文件内容
content = fp.read()
fp.close()
rowlist = content.splitlines() # 按行转换为一维表
# 逐行遍历 # 结果按分隔符分割为行向量
recordlist=[map(eval, row.split(delimiter)) for row in rowlist if row.strip()]
return mat(recordlist) # 返回转换后的矩阵形式
# 绘制分类点
def drawScatterbyLabel(self,plt,Input):
m,n=shape(Input)
target = Input[:,-1]
for i in xrange(m):
if target[i]==0:
plt.scatter(Input[i,0],Input[i,1],c='blue',marker='o')
else:
plt.scatter(Input[i,0],Input[i,1],c='red',marker='s')
#Logistic函数
def logistic(self,wTx):
return 1.0/(1.0+exp(-wTx))
def buildMat(self,dataSet):
m,n=shape(dataSet)
dataMat = zeros((m,n))
dataMat[:,0] = 1
dataMat[:,1:] = dataSet[:,:-1]
return dataMat
# 分类函数
def classifier(self,testData, weights):
prob = self.logistic(sum(testData*weights)) # 求取概率--判别算法
if prob > 0.5:
return 1.0 # prob>0.5 返回为1
else:
return 0.0 # prob<=0.5 返回为0
if __name__ == "__main__":
logis = Logister("testSet.txt")
input_data = logis.file2matrix("\t")
target = input_data[:,-1]
[m,n] = shape(input_data)
#print input_data
#print m,n
logis.drawScatterbyLabel(plt,input_data)
dataMat = logis.buildMat(input_data)
#print dataMat
#main()
alpha = 0.001 #梯度下降的参数
steps = 500 #迭代次数
weights = ones((n,1)) #初始化权重向量
for k in xrange(steps):
gradient = dataMat*mat(weights)
output = logis.logistic(gradient)
errors = target-output
weights = weights + alpha*dataMat.T*errors #梯度下降
#print weights
#应用模型到测试数据中
testdata = mat([-0.147324,2.874846]) #测试数据
m,n = shape(testdata)
testmat = zeros((m,n+1))
testmat[:,0] = 1
testmat[:,1:] = testdata
print logis.classifier(testmat,weights) #weights为前面训练得出的
#绘制决策边界
X = linspace(-5,5,100)
Y = -(double(weights[0])+X*(double(weights[1])))/double(weights[2])
plt.plot(X,Y)
plt.show()
运行结果:
1.0
代码中设计的数据集文件”testSet.txt”,可在http://download.csdn.net/detail/louishao/9747154下载查看。