import random as rd
import math class LogisticRegressionPySpark:
def __init__(self,MaxItr=100,eps=0.01,c=0.1):
self.max_itr = MaxItr
self.eps = eps
self.c = c def train(self,data):
#data为RDD,每条数据的最后一项为类别的标签 0 或者1
k = len(data.take(1)[0])
#初始化w
self.w = [rd.uniform(0,1) for i in range(k)]#第一个是截距b
n = data.count() for i in range(self.max_itr):
wadd = data.map(self.gradientDescent).reduce(lambda a,b:[a[i]+b[i] for i in range(k)]).collect()
for i in range(k):
#b没有加入正规化项,所以这里加了一个(i>0)
self.w[i] += (wadd[i]/n-self.c*self.w[i]*(i>0))*self.eps return self.w def gradientDescent(self,x):
h = 1/(1+math.exp(-sum(x[i]*self.w[i+1] for i in range(len(x)-1)))-self.w[0])
if x[len(x)-1]==0:
h = 1-h
return [h if i==0 else h*x[i-1] for i in range(len(x))] def predict(self,data):
return data.map(lambda x:1/(1+math.exp(-sum(self.w[0] if i==0 else self.w[i]*x[i-1] for i in range(len(x)+1)))))

pyspark 写 logistic regression的更多相关文章

  1. 原创:logistic regression实战(一):SGD Without lasso

    logistic regression是分类算法中非常重要的算法,也是非常基础的算法.logistic regression从整体上考虑样本预测的精度,用判别学习模型的条件似然进行参数估计,假设样本遵 ...

  2. 逻辑回归 Logistic Regression

    逻辑回归(Logistic Regression)是广义线性回归的一种.逻辑回归是用来做分类任务的常用算法.分类任务的目标是找一个函数,把观测值匹配到相关的类和标签上.比如一个人有没有病,又因为噪声的 ...

  3. [OpenCV] Samples 06: [ML] logistic regression

    logistic regression,这个算法只能解决简单的线性二分类,在众多的机器学习分类算法中并不出众,但它能被改进为多分类,并换了另外一个名字softmax, 这可是深度学习中响当当的分类算法 ...

  4. Stanford机器学习笔记-2.Logistic Regression

    Content: 2 Logistic Regression. 2.1 Classification. 2.2 Hypothesis representation. 2.2.1 Interpretin ...

  5. Logistic Regression Vs Decision Trees Vs SVM: Part I

    Classification is one of the major problems that we solve while working on standard business problem ...

  6. Logistic Regression逻辑回归

    参考自: http://blog.sina.com.cn/s/blog_74cf26810100ypzf.html http://blog.sina.com.cn/s/blog_64ecfc2f010 ...

  7. 在opencv3中实现机器学习之:利用逻辑斯谛回归(logistic regression)分类

    logistic regression,注意这个单词logistic ,并不是逻辑(logic)的意思,音译过来应该是逻辑斯谛回归,或者直接叫logistic回归,并不是什么逻辑回归.大部分人都叫成逻 ...

  8. Stanford机器学习---第三讲. 逻辑回归和过拟合问题的解决 logistic Regression & Regularization

    原文:http://blog.csdn.net/abcjennifer/article/details/7716281 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...

  9. Machine Learning - 第3周(Logistic Regression、Regularization)

    Logistic regression is a method for classifying data into discrete outcomes. For example, we might u ...

随机推荐

  1. Fragment销毁时replace和add两个方法的区别

    这个首先从一个bug说起,如图:   我们都知道fragment切换有两种方式: 1. replace方式 transaction.replace(R.id.content, IndexFragmen ...

  2. MSSQL WITH (NOLOCK) 脏读

    缺点: 1.会产生脏读 2.只适用与select查询语句 优点: 1.有些文件说,加了WITH (NOLOCK)的SQL查询效率可以增加33%. 2.可以用于inner join 语句 脏读: 一个用 ...

  3. EMMA: 免费java代码测试覆盖工具

    From:http://emma.sourceforge.net/ EMMA: a free Java code coverage tool   Code coverage for free: a b ...

  4. UESTC_秋实大哥与快餐店 2015 UESTC Training for Data Structures<Problem C>

    C - 秋实大哥与快餐店 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

  5. 修改MySQL 5.5的max_allowed_packet属性的方法

    今天在部署一个实验系统的时候,报出下面这个错: Your 'max_allowed_packet' variable is set to less than 16777216 Byte (16MB). ...

  6. SVN在ubuntu的安装和使用

    安装: svn客户端:apt-get install subversion ,然后根据提示一步一步,就完成了svn的安装.当然,也可以源码安装 svn,下载 subversion 一个最新版本的源码包 ...

  7. MD5加密算法(转)

    获取字符串的MD5摘要 原文更详细: http://www.weixuehao.com/archives/474 代码如下: import java.security.MessageDigest; p ...

  8. Radar Installation(贪心,可以转化为今年暑假不ac类型)

    Radar Installation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) ...

  9. jQuery——AJAX(一)

    jQuery.ajax(options):通过HTTP请求加载远程数据, 参数:options(可选),ajax请求设置.所有选项都是可选的. 返回值:XMLHttpRequest function ...

  10. 浅谈postMessage多页面监听事件

    最近做了一个Echarts和Highcharts多图多页面连动的效果,就用到postMessage 如下介绍: 最开始在最外围的页面也就是所有页面的父级页面添加postMessage监听事件以便监听下 ...