logistic回归 java_【JAVA实现】用Logistic回归进行分类-程序员宅基地

技术标签: logistic回归 java  

package logistic;

import java.util.List;

public class Horse {

private List attributes;

private String label;

public List getAttributes() {

return attributes;

}

public void setAttributes(List attributes) {

this.attributes = attributes;

}

public String getLabel() {

return label;

}

public void setLabel(String label) {

this.label = label;

}

}

package logistic;

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

public class Logistic {

public List initDataSet(String fileName) {

List dataSet = new ArrayList();

BufferedReader bufferedReader = null;

try {

bufferedReader = new BufferedReader(

new InputStreamReader(

new FileInputStream(

"/home/shenchao/Desktop/MLSourceCode/machinelearninginaction/Ch05/"+fileName)));

String line = null;

while((line = bufferedReader.readLine()) != null) {

Horse horse = new Horse();

String[] s = line.split("\t");

List list = new ArrayList();

for (int i = 0; i < s.length-1; i++) {

list.add(Double.parseDouble(s[i]));

}

horse.setAttributes(list);

horse.setLabel(s[s.length-1]);

dataSet.add(horse);

}

return dataSet;

} catch (Exception e) {

e.printStackTrace();

throw new RuntimeException();

} finally {

try {

bufferedReader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 改进的随机梯度上升算法

*

* @param trainDataSet 训练集

* @param numIter 迭代次数

* @return 权重向量

*/

public List stocGradAscent(List trainDataSet, int numIter) {

//初始化回归系数

List weights = new ArrayList();

for (int i = 0; i < trainDataSet.get(0).getAttributes().size(); i++) {

weights.add(1.0);

}

for (int i = 0; i < numIter; i++) {

for (int j = 0; j < trainDataSet.size(); j++) {

double alpha = 4.0/(1.0+i+j) + 0.01;

int randIndex = new Random().nextInt(trainDataSet.size());

double h = sigmoid(vecMultipVec(trainDataSet.get(randIndex).getAttributes(), weights));

double error = Double.parseDouble(trainDataSet.get(randIndex).getLabel()) - h;

weights = vecAddVec(weights,alpha, error, trainDataSet.get(randIndex).getAttributes());

trainDataSet.remove(randIndex);

}

}

return weights;

}

private List vecAddVec(List weights, double alpha,

double error, List attributes) {

List list = new ArrayList();

for (int i = 0; i < weights.size(); i++) {

list.add(weights.get(i) + alpha * error * attributes.get(i));

}

return list;

}

/**

* 计算向量的内积

* @param attributes

* @param weights

* @return

*/

private double vecMultipVec(List attributes, List weights) {

double sum = 0.0;

for (int i = 0; i < attributes.size(); i++) {

sum += attributes.get(i) * weights.get(i);

}

return sum;

}

/**

* @param x

* @return

*/

private double sigmoid(double x) {

return 1.0 / (1 + Math.exp(-x));

}

public double test() {

List trainDataSet = initDataSet("horseColicTraining.txt");

List testDataSet = initDataSet("horseColicTest.txt");

List trainWeights = stocGradAscent(trainDataSet, 500);

int errorCount = 0;

for (Horse horse : testDataSet) {

if ((int)classifyVector(horse.getAttributes() , trainWeights) != (int)(Double.parseDouble(horse.getLabel()))) {

++errorCount;

}

}

System.out.println("the error rate of this test is: " + (double) errorCount / testDataSet.size());

return (double) errorCount / testDataSet.size();

}

private double classifyVector(List attributes,

List trainWeights) {

double prob = sigmoid(vecMultipVec(attributes, trainWeights));

if (prob > 0.5) {

return 1.0;

}

return 0.0;

}

public static void main(String[] args) {

Logistic logistic = new Logistic();

double sum = 0.0;

for (int i = 0; i < 10; i++) {

sum += logistic.test();

}

System.out.println("after 10 iterations the average error rate is: " + sum / 10 );

}

}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_29095587/article/details/114461695

智能推荐

easyui datagrid-detailview 嵌套高度自适应_fixdetailrowheight-程序员宅基地

文章浏览阅读3.2w次,点赞5次,收藏4次。实现效果原因异步加载,明细展开时,可能会遇到父列表不能自动适应子列表高度的变化具体代码$('#centerdatagrid').datagrid({ url:'${ctx}/offer/offer!projectPage.htm', title: '项目列表', iconCls:'iconfont icon-iconlist', _fixdetailrowheight

3GPP TS 29244-g30 中英文对照 | 6.2.6 PFCP Association Setup Procedure-程序员宅基地

文章浏览阅读634次。学习5G知识,势必要去阅读相关的技术标准,而纯英文的标准协议对于初学者来说是个门槛。个人在学习5G标准3GPP TS 29244-g30过程中,也发现google的机器翻译实在晦涩难懂,所以个人在借助百度翻译(中国人更懂中文)的同时,人工对每句话进行了校对。在自己学习的同时,也将翻译内容分享给给广大网友。由于个人能力有限,翻译不到位的地方还请各位网友们指正。6.2.6 PFCP Association Setup ProcedurePFCP偶联建立程序6.2.6.1 GeneralT.._pfcp association

CanOpen的SDO、PDO简单通信使用_canopen pdo怎么区分读和写的-程序员宅基地

文章浏览阅读9.5k次,点赞17次,收藏103次。CanOpen部分笔记:CanOpen的一些简单的使用记录。为了简单明了,假设现在是用CAN盒子向使用CanOpen协议的设备通信我(假设设备地址ID为0x01)。SDO通信:如果想要读取设备某个参数。则使用此数据格式发送。比如读取设备索引为0x60c1,子索引0x01的参数。则发送如下参数:发送:0x601 40 c1 60 01 00 00 00 00接收:0x581 43 c1 60 01 01 02 03 04其中发送中的0x40表示读取数据,接收中的0x43表示接收到4个字节,_canopen pdo怎么区分读和写的

【Windows Server 2019】存储服务器的配置和管理——NFS的安装与配置(上)_windows nfs防火墙设置-程序员宅基地

文章浏览阅读8.3k次。博文介绍了如何在 Windows Server 2019 上配置 NFS 服务器端,包括实验拓扑图,准备工作,安装NFS服务,查看防火墙以及配置NFS服务的服务器端。_windows nfs防火墙设置

知识图谱de构建与应用(三):知识融合_知识图谱知识融合-程序员宅基地

文章浏览阅读956次。对于很多需要构建知识图谱的领域,由于从业者甚多,自然而然地会沉淀出数量众多的数据库或知识库,比如在影视子领域,百度百科、维基百科、豆瓣等都是很好的知识库,因而在构建知识图谱的过程中往往会对多个知识库的知识进行合并,以期获得最全面的知识,这个过程就称为知识融合。此外,由于被合并的各个知识库里的知识难免会存在错误,因而知识融合会对多个知识库的知识进行交叉验证,知识融合之后获得的知识相比融合前的单知识库会更准确。由于自然语言的开放性,各个领域的知识融合任务往往面临以下四个难点。(1)异构问题。.........._知识图谱知识融合

在智能云服务中实现人工智能和自然语言处理的融合-程序员宅基地

文章浏览阅读298次,点赞3次,收藏7次。1.背景介绍在当今的数字时代,人工智能(AI)和自然语言处理(NLP)技术已经成为许多行业的核心驱动力。智能云服务(Cloud Services)为企业提供了高度可扩展、灵活的计算资源,使得在大规模数据集上运行复杂的AI和NLP算法变得更加容易。在这篇文章中,我们将探讨如何在智能云服务中实现人工智能和自然语言处理的融合,以及这种融合在实际应用中的优势和挑战。2.核心概念与联系2.1人工...

随便推点

#实验 2-2 帧中继配置_帧中继手动映射-程序员宅基地

文章浏览阅读1.1k次。实验 2-2 帧中继配置学习目标 掌握用户边缘设备(CE)上帧中继接口的配置方法 掌握Hub-Spoke网络中RIP的配置方法 掌握Hub-Spoke(NBMA)网络中OSPF的配置方法 掌握点到多点网络中OSPF的配置方法场景企业的总部和部分分支之间仍使用帧中继网络互连,作为企业的网络管理员,您需要在总部和分支的边缘路由器上配置帧中继功能,并配置本地DLCI与IP地址间的..._帧中继手动映射

Linux下的文件系统以及磁盘操作-程序员宅基地

文章浏览阅读69次。在文件系统方面,Linux可以算得上操作系统中的 “瑞士军刀”。Linux 支持许多种文件系统,从日志型文件系统到集群文件系统和加密文件系统,而“什么是文件系统”?文件系统是对一个存储设备上的数据和元数据进行组织的机制。常见的文件系统:ext2、ext3、ext4、xfs、jfs、reiserfs、iso9660、swap、VFAT、VFS等例如..._保存当前磁盘分区表 #sfdisk-d

学会python可以做什么兼职,能做兼职吗_python可以做什么副业-程序员宅基地

文章浏览阅读1.2k次。学会python可以做什么兼职,普通人能做兼职吗_python可以做什么副业

Failed to execute goal on project sky-server: Could not resolve dependencies for project-程序员宅基地

文章浏览阅读534次,点赞19次,收藏7次。在父工程中先clean,后install。

facebook的Android调试工具Stetho的简单使用-程序员宅基地

文章浏览阅读1.7k次。一. 项目Module的build.gradle配置dependencies { compile 'com.facebook.stetho:stetho:1.5.0' compile 'com.facebook.stetho:stetho-okhttp3:1.5.0'}二. 在Application 中初始化 @Override public