LogisticRegression Python实现
来源:互联网 发布:php 电商系统 编辑:程序博客网 时间:2024/06/14 10:18
from numpy import *import matplotlib.pyplot as pltdef sigmoid(x): return 1/(1+exp(-x))class LogRegressionClassifier(object): def __init__(self): self.dataMat = list() self.labelMat = list() self.weights = list() def loadDataSet(self, filename): fr = open(filename) for line in fr.readlines(): lineArr = line.strip().split() dataLine = [1.0] for i in lineArr: dataLine.append(float(i)) label = dataLine.pop() self.dataMat.append(dataLine) self.labelMat.append(int(label)) self.dataMat = mat(self.dataMat) self.labelMat = mat(self.labelMat).transpose() def train(self): self.weights = self.stocGradAscent1() def batchGradAscent(self): m,n = shape(self.dataMat) alpha = 0.001 maxCycles = 500 weights = ones((n,1)) for k in range(maxCycles): h = sigmoid(self.dataMat * weights) error = (self.labelMat-h) weights += alpha * self.dataMat.transpose()*error return weights def stocGradAscend1(self): m,n = shape(self.dataMat) alpha = 0.01 weights = ones(n,1) for i in range(m): h = sigmoid(sum(self.dataMat[i] * weights)) error = self.labelMat[i] - h weights += (alpha * error * self.dataMat[i]) return weights def stocGradAscend2(self): numIter = 2 m,n = shape(self.dataMat) weights = ones((n,1)) for j in range(numIter): alpha = 4/(1.0+j+i) +0.0001 #alpha decreases with iteration randIndex = int(random.uniform(0, len(dataIndex))) h = sigmoid( sum(self.dataMat[randIndex] * weights)) error = self.labelMat[randIndex] - h weights += (alpha * error * self.dataMat[randIndex]).transpose() del (dataIndex[randIndex]) return weights def classify(self, X): prob = sigmod( sum( X*self.weights)) if prob > 0.5: return 1.0 else: return 0.0 def test(self): self.loadDataSet("testData.dat") weights0 = self.batchGradAscent() weights1 = self.stocGradAscend1() weights2 = self.stocGradAscend2() print "batchGradAscent:" + weights0 print "stocGradAscent0:" + weights1 print "stocGradAscent1:" + weights2if __name__=='__main__': lr = LogRegressionClassifier() lr.test()
0 0
- LogisticRegression Python实现
- LogisticRegression
- LogisticRegression
- LogisticRegression
- c++实现LogisticRegression/用c++实现逻辑回归
- 【甘道夫】基于scikit-learn实现逻辑回归LogisticRegression
- Spark LogisticRegression
- pyspark-Logisticregression
- LogisticRegression相关
- LogisticRegression Digital
- 【spark+python】采用LogisticRegression(MLLib)对MNIST的0-1数字进行识别
- 【python数据挖掘课程】十六.逻辑回归LogisticRegression分析鸢尾花数据
- LogisticRegression.py 解析
- LogisticRegression逻辑回归
- LinearRegression LogisticRegression And OtherRegression
- Sklearn-LogisticRegression逻辑回归
- 逻辑回归LogisticRegression
- MLLib之LogisticRegression
- Centos 安装lamp环境(php5.6)
- linux配置永久ip不生效解决方法
- IGraphBuilder
- js面向对象编程
- Oracle 11g学习笔记--角色
- LogisticRegression Python实现
- <第一章>Node 学习笔记 |>Mac Node安装<|
- 苹果向高管团队授予2500万美元激励股票
- Ensemble learning 集成学习(二)
- 用HttpURLConnection 下载文件工具类
- Netty5用户手册之五:netty中流数据的传输处理问题
- IUnknown
- 哈密顿绕行世界问题
- c语言变量