logisticregression
from numpy import *
import random
import time
st = time.time() def loaddata(filename):
fr = open(''.join([filename, '.txt'])).readlines()
trainx = [[1] + map(float, line.split()[:-1]) for line in fr] # trainx = [[1,12.2,22.4],[1,22.3,31.2],...]
trainy = [[float(line.split()[-1])] for line in fr] # trainy = [0,1,1,0,...]
return trainx, trainy def sigmod(z):
return 1.0 / (1 + exp(-z)) def optimizaion(trainx, trainy):
trainxmat = mat(trainx)
m = len(trainx)
# beta = [0,0,0]
beta = ones((len(trainx[0]),1)) # array
# maxiter
M = 500
"""
# error permid
e =
"""
"""
for i in xrange(M):
#if error2sum > e:
# z = betat.T * x = trainx (matricdoc)* beta = [beta.Tx1,beta.Tx2,...,beta.Txn]
sigmodz = sigmod(trainxmat * beta)
# [error_i = yi - sigmod(zi)]
error = trainy - sigmodz
# update beta
beta += alpha * trainxmat.T * error
print beta
"""
# random gradascent
for j in xrange(M):
for i in xrange(m):
# per span
alpha = 0.01 + 4 / (1.0 + i +j)
randid = random.randint(0, m - 1)
sigmodz = sigmod(trainxmat[randid] * beta)
error = trainy[randid] - sigmodz
beta += alpha * trainxmat[randid].T * error
#print beta return beta def logregress(testx, beta):
if mat(testx) * beta > 0: return [1.0]
else: return [0.0] def main():
# step 1: loading data...
print "step 1: loading data..."
trainx, trainy = loaddata('horseColicTraining')
testx, testy = loaddata('horseColicTest')
"""
print 'trainx', trainx
print 'trainy', trainy
print 'testx', testx
print 'testy', testy
print 'testy[2]',testy[2]
""" # step 2: training...
print "step 2: training..."
beta = optimizaion(trainx, trainy)
#print "beta = ",beta # step 3: testing...
print "step 3: testing..."
numTests = 10; errorSum = 0.0; l = len(testx)
for j in xrange(numTests):
errorcount = 0.0
#print 'the total number is: ',l
for i in xrange(l):
if logregress(testx[i], beta) != testy[i]:
errorcount += 1
#print "the number of error is: ", errorcount
print "the error rate is: ", (errorcount / l)
errorSum += (errorcount / l)
print "after %d iterations the average error rate is: %f" %(numTests, errorSum/numTests) """
trainx, trainy = loaddata('testSet')
print trainy
optimizaion(trainx, trainy)
""" main() print "cost time: ", (time.time() - st) """ lineregres
# ssi = sigmod(zi) - sigmod(zi) ** 2
ss = [sigmodzi - sigmodzi ** 2 for sigmodzi in sigmodz]
# errssi = errori * ssi
errss = map(lambda x, y: x * y, error, ss)
# treri = errssi * trainxi(vector)
trer = [errss[i] * array(trainx[i]) for i in xrange(m)]
"""
logisticregression的更多相关文章
- theano中的logisticregression代码学习
1 class LogisticRegression (object): 2 def __int__(self,...): 3 4 #定义一些与逻辑回归相关的各种函数 5 6 def method1( ...
- 【deep learning学习笔记】注释yusugomori的LR代码 --- LogisticRegression.h
继续看yusugomori的代码,看逻辑回归.在DBN(Deep Blief Network)中,下面几层是RBM,最上层就是LR了.关于回归.二类回归.以及逻辑回归,资料就是前面转的几篇.套路就是设 ...
- 【deep learning学习笔记】注释yusugomori的LR代码 --- LogisticRegression.cpp
模型实现代码,关键是train函数和predict函数,都很容易. #include <iostream> #include <string> #include <mat ...
- python 10大算法之二 LogisticRegression 笔记
使用的包 import matplotlib.pyplot as plt import pandas as pd import numpy as npfrom sklearn import datas ...
- sklearn.linear_model.LogisticRegression参数说明
目录 sklearn.linear_model.LogisticRegression sklearn.linear_model.LogisticRegressionCV sklearn.linear_ ...
- 基于jieba,TfidfVectorizer,LogisticRegression进行搜狐新闻文本分类
一.简介 此文是对利用jieba,word2vec,LR进行搜狐新闻文本分类的准确性的提升,数据集和分词过程一样,这里就不在叙述,读者可参考前面的处理过程 经过jieba分词,产生24000条分词结果 ...
- python3二元Logistics Regression 回归分析(LogisticRegression)
纲要 boss说增加项目平台分析方法: T检验(独立样本T检验).线性回归.二元Logistics回归.因子分析.可靠性分析 根本不懂,一脸懵逼状态,分析部确实有人才,反正我是一脸懵 首先解释什么是二 ...
- Spark LogisticRegression 逻辑回归之建模
导入包 import org.apache.spark.sql.SparkSession import org.apache.spark.sql.Dataset import org.apache.s ...
- Spark LogisticRegression 逻辑回归之简介
LogisticRegression简介
- LogisticRegression 和 LogisticRegressionCV
在scikit-learn中,与逻辑回归有关的主要是这3个类.LogisticRegression, LogisticRegressionCV 和logistic_regression_path.其中 ...
随机推荐
- Linux下的库操作工具-nm、ar、ldd、ldconfig和ld.so
Linux下的库操作工具-nm.ar.ldd.ldconfig和ld.so .nm [options] file 列出file中的所有符号 [option] -c 将符号转化为用户级的名字 -s 当用 ...
- python3、selenium、autoit3,通过flash控件上传文件
autoit.au3 #include <Constants.au3> WinWait(); //暂停执行脚本,直到上传对话框出现 WinActive("打开") Wi ...
- PCRE
http://blog.sina.com.cn/s/blog_6a1837e901010ckv.html http://hubeihuyanwei.blog.163.com/blog/static/2 ...
- 《Linear Algebra and Its Applications》-chaper2-矩阵代数中的基本性质
之前我们曾经提及,完成了线性方程组-向量方程-矩阵方程的等价转化之后,我们对于现实问题中的线性方程组,只需将其转移到矩阵(向量)方程,然后利用矩阵代数中的各种方法和性质进行计算或者化简即可,而下面我们 ...
- Asp.Net Mvc+Angular.Js自测小Demo
参考:http://www.cnblogs.com/eedc/p/6082052.html 一.引用anguler: 1.angular.js 2.angular-route.js 3.app.js ...
- UINavigationController 导航控制器 ,根据文档写的一些东西
今天讲了导航控制器UINavigationController 和标签栏视图控制器UITabBarController 先来说一说导航视图控制器 UINavigationController 导航控 ...
- 枚举的基本使用方法 Enumerations
枚举的基本使用方法 Enumerations Enumeration enum SomeEnumeration{ case enumeration1 case enumeration2 case ...
- 你的第一个Windows程序——绘制窗口
MSDN原文(英文) 绘制窗口 你已经创建了你的窗口,现在你想在它里面显示东西.在WIndows术语里,这就是所谓的绘制窗口.混合隐喻,一个窗口是一个空白画布,等待你去填充它. 有时你的程序将启动绘制 ...
- [Redux] Reducer Composition with Arrays
In the previous lesson we created a reducer that can handle two actions, adding a new to-do, and tog ...
- shell 判断文件、目录是否存在
shell判断文件是否存在 1. shell判断文件,目录是否存在或者具有权限 2. #!/bin/sh 3. 4. myPath="/var/log/httpd/" 5. m ...