日撸java_day61-62

决策树

package machineLearning.decisiontree;import weka.core.Instance;
import weka.core.Instances;import java.io.FileReader;
import java.util.Arrays;/*** ClassName: ID3* Package: machineLearning.decisiontree* Description:  The ID3 decision tree inductive algorithm.** @Author: luv_x_c* @Create: 2023/8/7 14:55*/
public class ID3 {/*** The data.*/Instances dataset;/*** Is the dataset pure(Only one label)?*/boolean pure;/*** The number of classes. For binary classification it is 2.*/int numClasses;/*** Available instances. Other instances do not belong this branch.*/int[] availableInstances;/*** Available attributes. Other attributes have been selected int the path from the root.*/int[] availableAttributes;/*** The selected attribute.*/int splitAttributes;/*** The children nodes.*/ID3[] children;/*** My label. Inner nodes also have a label. For example, <outlook =sunny ,humidity = high > never appear it the* training data, but <humidity =high> is valid  in other cases.*/int label;/*** The prediction, including queried and predicted labels.*/int[] predicts;/*** Small block cannot be split further.*/static int smallBlockThreshold = 3;/*** The constructor.** @param paraFileName The given file.*/public ID3(String paraFileName) {dataset = null;try {FileReader fileReader = new FileReader(paraFileName);dataset = new Instances(fileReader);fileReader.close();} catch (Exception ee) {System.out.println("Cannot read the file: " + paraFileName + "\r\n" + ee);System.exit(0);}// Of trydataset.setClassIndex(dataset.numAttributes() - 1);numClasses = dataset.classAttribute().numValues();availableInstances = new int[dataset.numInstances()];for (int i = 0; i < availableInstances.length; i++) {availableInstances[i] = i;}//Of for iavailableAttributes = new int[dataset.numAttributes() - 1];for (int i = 0; i < availableAttributes.length; i++) {availableAttributes[i] = i;}// OF for i//Initialize.children = null;label = getMajorityClass(availableInstances);pure = pureJudge(availableInstances);}// Of the first constructor/*** The constructor.*/public ID3(Instances paraDataset, int[] paraAvailableInstances, int[] paraAvailableAttributes) {//Copy its reference instead of clone the availableInstances.dataset = paraDataset;availableInstances = paraAvailableInstances;availableAttributes = paraAvailableAttributes;//Initialize.children = null;label = getMajorityClass(availableInstances);pure = pureJudge(availableInstances);}// OF the second constructor/*** Is the given block pure?** @param paraBlock The block.* @return True if pure.*/public boolean pureJudge(int[] paraBlock) {pure = true;for (int i = 1; i < paraBlock.length; i++) {if (dataset.instance(paraBlock[i]).classValue() != dataset.instance(paraBlock[0]).classValue()) {pure = false;break;}//Of if}//Of for ireturn pure;}//Of pureJudge/*** Compute the majority class of the given block for voting.** @param paraBlock The block.* @return The majority class.*/public int getMajorityClass(int[] paraBlock) {int[] tempClassCounts = new int[dataset.numClasses()];for (int i = 0; i < paraBlock.length; i++) {tempClassCounts[(int) dataset.instance(paraBlock[i]).classValue()]++;}//OF for iint resultMajorityClass = -1;int tempMaxCount = -1;for (int i = 0; i < tempClassCounts.length; i++) {if (tempMaxCount < tempClassCounts[i]) {resultMajorityClass = i;tempMaxCount = tempClassCounts[i];}//Of if}//Of for ireturn resultMajorityClass;}//Of getMajorityClass/*** Select the best attribute.** @return The best attribute index.*/public int selectBestAttribute() {splitAttributes = -1;double tempMinimalEntropy = 10000;double tempEntropy;for (int i = 0; i < availableAttributes.length; i++) {tempEntropy = conditionalEntropy(availableAttributes[i]);if (tempMinimalEntropy > tempEntropy) {tempMinimalEntropy = tempEntropy;splitAttributes = availableAttributes[i];}//Of if}//Of for ireturn splitAttributes;}//Of selectBestAttribute/*** Compute the conditional entropy of an attribute.** @param paraAttribute The given attribute.* @return The entropy.*/public double conditionalEntropy(int paraAttribute) {// Step1. Statistics.int tempNumClasses = dataset.numClasses();int tempNumValues = dataset.attribute(paraAttribute).numValues();int tempNumInstances = availableInstances.length;double[] tempValueCounts = new double[tempNumValues];double[][] tempCountMatrix = new double[tempNumValues][tempNumClasses];int tempClass, tempValue;for (int i = 0; i < tempNumInstances; i++) {tempClass = (int) dataset.instance(availableInstances[i]).classValue();tempValue = (int) dataset.instance(availableInstances[i]).value(paraAttribute);tempValueCounts[tempValue]++;tempCountMatrix[tempValue][tempClass]++;}//Of for i// Step2.double resultEntropy = 0;double tempEntropy, tempFraction;for (int i = 0; i < tempNumValues; i++) {if (tempValueCounts[i] == 0) {continue;}//Of iftempEntropy = 0;for (int j = 0; j < tempNumClasses; j++) {tempFraction = tempCountMatrix[i][j] / tempValueCounts[i];if (tempFraction == 0) {continue;}//Of iftempEntropy += -tempFraction * Math.log(tempFraction);}//Of for jresultEntropy += tempValueCounts[i] / tempNumInstances * tempEntropy;}//Of for ireturn resultEntropy;}//Of conditionalEntropy/*** Split the data according to the given attribute.** @param paraAttribute The given attribute.* @return The blocks.*/public int[][] splitData(int paraAttribute) {int tempNumValues = dataset.attribute(paraAttribute).numValues();int[][] resultBlocks = new int[tempNumValues][];int[] tempSizes = new int[tempNumValues];// First scan to count the size of each block.int tempValue;for (int i = 0; i < availableInstances.length; i++) {tempValue = (int) dataset.instance(availableInstances[i]).value(paraAttribute);tempSizes[tempValue]++;}//Of for i// Allocate space.for (int i = 0; i < tempNumValues; i++) {resultBlocks[i] = new int[tempSizes[i]];}//Of for i// Second scan to fill.Arrays.fill(tempSizes, 0);for (int i = 0; i < availableInstances.length; i++) {tempValue = (int) dataset.instance(availableInstances[i]).value(paraAttribute);// Copy dataresultBlocks[tempValue][tempSizes[tempValue]] = availableInstances[i];tempSizes[tempValue]++;}// OF for ireturn resultBlocks;}//Of splitData/*** Build the tree recursively.*/public void buildTree() {if (pureJudge(availableInstances)) {return;}//OF ifif (availableInstances.length <= smallBlockThreshold) {return;}//Of ifselectBestAttribute();int[][] tempSubBlocks = splitData(splitAttributes);children = new ID3[tempSubBlocks.length];//Construct the remaining attribute set.int[] tempRemainingAttributes = new int[availableAttributes.length - 1];for (int i = 0; i < availableAttributes.length; i++) {if (availableAttributes[i] < splitAttributes) {tempRemainingAttributes[i] = availableAttributes[i];} else if (availableAttributes[i] > splitAttributes) {tempRemainingAttributes[i - 1] = availableAttributes[i];}//Of if}//Of for i//Construct children.for (int i = 0; i < children.length; i++) {if ((tempSubBlocks[i] == null) || (tempSubBlocks[i].length == 0)) {children[i] = null;} else {children[i] = new ID3(dataset, tempSubBlocks[i], tempRemainingAttributes);children[i].buildTree();}//Of if}//OF for i}//OF buildTree/*** Classify an instance,** @param paraInstance The given instance.* @return The prediction.*/public int classify(Instance paraInstance) {if (children == null) {return label;}//Of ifID3 tempChild = children[(int) paraInstance.value(splitAttributes)];if (tempChild == null) {return label;}//Of ifreturn tempChild.classify(paraInstance);}//Of classify/*** Test on n testing set.** @param paraDataset The given testing set.* @return The accuracy.*/public double test(Instances paraDataset) {double tempCorrect = 0;for (int i = 0; i < paraDataset.numInstances(); i++) {if (classify(paraDataset.instance(i)) == (int) paraDataset.instance(i).classValue()) {tempCorrect++;}//Of if}//Of for ireturn tempCorrect / paraDataset.numInstances();}//Of test/*** Test on the training set.** @return The accuracy.*/public double selfTest() {return test(dataset);}//Of selfTest@Overridepublic String toString() {String resultString = "";String tempAttributeName = dataset.attribute(splitAttributes).name();if (children == null) {resultString += "class = " + label;} else {for (int i = 0; i < children.length; i++) {if (children[i] == null) {resultString += tempAttributeName + " = " + dataset.attribute(splitAttributes).value(i) + " : " + "class= " + label + "\r\n";} else {resultString += tempAttributeName + " = " + dataset.attribute(splitAttributes).value(i) + " : " + children[i] + "\r\n";}//OF if}//Of for i}//Of ifreturn resultString;}//OF toString/*** Test this class.*/public static void id3Test() {ID3 tempID3 = new ID3("E:\\java_code\\data\\sampledata\\weather.arff");ID3.smallBlockThreshold = 3;tempID3.buildTree();System.out.println("The tree is: \r\n" + tempID3);double tempAccuracy = tempID3.selfTest();System.out.println("The accuracy is: " + tempAccuracy);}//Of id3Test/*** The entrance of the program.** @param args Not used now.*/public static void main(String[] args) {id3Test();}//OF main
}//Of class ID3

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/87322.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

css flex 上下结构布局

display: flex; flex-flow: column; justify-content: space-between;

(原创)Flutter与Native页面互相跳转

前言 实际开发混合项目时&#xff0c;常常会有页面跳转的需求 如果是原生界面和flutter界面需要互相跳转 这种情况应该怎么处理呢&#xff1f; 今天这篇博客主要就来介绍下这个情况 其实想一下&#xff0c;这个问题可以拆成四个小的问题来分析&#xff1a; 1&#xff1a;原生界…

Ubuntu22.04安装docker

在ubuntu22.04上安装docker还是比较容易的&#xff0c;之前在公司的centos6上边装docker&#xff0c;那才真是一言难尽呀&#xff0c;废话不多说&#xff0c;开始安装 1、更新包管理器 apt update 2、安装必要的软件包&#xff0c;以便允许 apt 使用 HTTPS 仓库 sudo apt i…

[数据集][目标检测]钢材表面缺陷目标检测数据集VOC格式2279张10类别

数据集格式&#xff1a;Pascal VOC格式(不包含分割路径的txt文件和yolo格式的txt文件&#xff0c;仅仅包含jpg图片和对应的xml) 图片数量(jpg文件个数)&#xff1a;2279 标注数量(xml文件个数)&#xff1a;2279 标注类别数&#xff1a;10 标注类别名称:["yueyawan",&…

2023-08-12 LeetCode每日一题(合并 K 个升序链表)

2023-08-12每日一题 一、题目编号 23. 合并 K 个升序链表二、题目链接 点击跳转到题目位置 三、题目描述 给你一个链表数组&#xff0c;每个链表都已经按升序排列。 请你将所有链表合并到一个升序链表中&#xff0c;返回合并后的链表。 示例 1&#xff1a; 示例 2&…

大数据培训课程-《机器学习从入门到精通》上新啦

《机器学习从入门到精通》课程是一门专业课程&#xff0c;面向人工智能技术服务&#xff0c;课程系统地介绍了Python编程库、分类、回归、无监督学习和模型使用技巧以及算法和案例充分融合。 《机器学习从入门到精通》课程亮点&#xff1a; 课程以任务为导向&#xff0c;逐步学…

Docker+Consul+Registrator 实现服务注册与发现

第四阶段 时 间&#xff1a;2023年8月8日 参加人&#xff1a;全班人员 内 容&#xff1a; DockerConsulRegistrator 实现服务注册与发现 目录 一、服务注册中心引言 CAP理论是分布式架构中重要理论&#xff1a; 二、服务注册中心软件 &#xff08;一&#xff09;Zoo…

C语言——将一串字符进行倒序

//将一串字符进行倒序 #include<stdio.h> #define N 6 int main() {int a[N]{0,1,2,3,4,5};int i,t;printf("原数组数值&#xff1a; ");for(i0;i<N;i)printf("%d",a[i]);for(i0;i<N/2;i){ta[i];a[i]a[N-1-i];a[N-1-i]t;}printf("\n排序…

Docker mysql+nacos单机部署

docker 网络创建 由于nacos需要访问mysql的数据&#xff0c;因此mysql容器和nacos容器之间需要进行通信。容器间通信有很多方式&#xff0c;在这里采用同一网络下的方式进行实现。因此需要创建网络。创建网络的命令如下&#xff1a; docker network create --driver bridge n…

案例14 Spring MVC文件上传案例

基于Spring MVC实现文件上传&#xff1a; 使用commons-fileupload实现上传文件到本地目录。 实现上传文件到阿里云OSS和从阿里云OSS下载文件到本地。 1. 创建项目 选择Maven快速构建web项目&#xff0c;项目名称为case14-springmvc03。 ​ 2. 配置Maven依赖 <?xml ver…

C++语法中bitset位图介绍及模拟实现

一、位图的引入 先来看下边一道面试题&#xff1a; 给40亿个不重复的无符号整数&#xff0c;没排过序。给一个无符号整数&#xff0c;如何快速判断一个数是否在这40亿个数中。 经过我们之前的学习&#xff0c;我们可能会有以下的思路&#xff1a; 对这些数进行排序&#xff…

Selenium 是什么?简单明了的介绍

Selenium Selenium 是什么 Selenium 是一款 Web UI 测试工具&#xff0c;是一款 自动化测试 工具&#xff0c;使用 Selenium 测试工具进行的测试通常被称为 Selenium Testing&#xff0c;各种支持如下列表&#xff1a; UI 元素的支持与管理&#xff1a;自写代码实现浏览器支…

Android 视频播放器dkplayer

gihub地址&#xff1a; https://github.com/Doikki/DKVideoPlayer GitHub - Doikki/DKVideoPlayer: Android Video Player. 安卓视频播放器&#xff0c;封装MediaPlayer、ExoPlayer、IjkPlayer。模仿抖音并实现预加载&#xff0c;列表播放&#xff0c;悬浮播放&#xff0c;广…

adb用法,安卓的用户CA证书放到系统CA证书下

设备需root&#xff01;&#xff01;设备需root&#xff01;&#xff01;设备需root&#xff01;&#xff01; ​​​​​​​测试环境&#xff1a;redmi 5 plus、miui10 9.9.2dev&#xff08;安卓8.1&#xff09;、已root win下安装手机USB驱动&#xff08;过程略&#xff0c…

大数据扫盲(1): 数据仓库与ETL的关系及ETL工具推荐

在数字化时代&#xff0c;数据成为了企业决策的关键支持。然而&#xff0c;随着数据不断增长&#xff0c;有效地管理和利用这些数据变得至关重要。数据仓库和ETL工具作为数据管理和分析的核心&#xff0c;将帮助企业从庞杂的数据中提取有价值信息。 一、ETL是什么&#xff1f; …

redis学习笔记(九)

文章目录 python对redis基本操作&#xff08;1&#xff09;连接redis&#xff08;2&#xff09;数据类型操作 python对redis基本操作 &#xff08;1&#xff09;连接redis # 方式1 import redisr redis.Redis(host127.0.0.1, port6379) r.set(foo, Bar) print(r.get(foo))# …

VS中.cu文件属性中项目类型没有cuda

问题 VS中.cu文件属性中项目类型没有cuda 解决办法 右键项目“自定义” ![请添加图片描述](https://img-blog.csdnimg.cn/9717093332604b5982e67b15108c9ec8.png 再回到cu文件右键属性就会出现cuda选项了 请添加图片描述

(7)原神各属性角色的max与min

在对全部角色进行分析之后&#xff0c;还有必要对各属性角色的生命值/防御力/攻击力进行max与min显示&#xff1a; 话不多说&#xff0c;上货&#xff01; from pyecharts.charts import Radar from pyecharts import options as opts import pandas as pd from pyecharts.ch…

插入排序(Java实例代码)

目录 插入排序 一、概念及其介绍 二、适用说明 三、过程图示 四、Java 实例代码 InsertionSort.java 文件代码&#xff1a; 插入排序 一、概念及其介绍 插入排序(InsertionSort)&#xff0c;一般也被称为直接插入排序。 对于少量元素的排序&#xff0c;它是一个有效的算…

【LangChain】Memory

概要 大多数LLM应用都有对话界面。对话的一个重要组成部分是能够引用对话中先前介绍的信息。至少&#xff0c;对话系统应该能够直接访问过去消息的某些窗口。更复杂的系统需要有一个不断更新的世界模型&#xff0c;这使得它能够执行诸如维护有关实体及其关系的信息之类的事情。…