数据挖掘中ID3算法实现zz
id3 function D = ID3(train_features, train_targets, params, region) % Classify using Quinlan's ID3 algorithm
% Inputs:
% features - Train features
% targets - Train targets
% params - [Number of bins for the data, Percentage of incorrectly assigned samples at a node]
% region - Decision region vector: [-x x -y y number_of_points]
%
% Outputs
% D - Decision sufrace [Ni, M] = size(train_features); %Get parameters
[Nbins, inc_node] = process_params(params);
inc_node = inc_node*M/100; %For the decision region
N = region(5);
mx = ones(N,1) * linspace (region(1),region(2),N);
my = linspace (region(3),region(4),N)' * ones(1,N);
flatxy = [mx(:), my(:)]'; %Preprocessing
[f, t, UW, m] = PCA(train_features, train_targets, Ni, region);
train_features = UW * (train_features - m*ones(1,M));;
flatxy = UW * (flatxy - m*ones(1,N^2));; %First, bin the data and the decision region data
[H, binned_features]= high_histogram(train_features, Nbins, region);
[H, binned_xy] = high_histogram(flatxy, Nbins, region); %Build the tree recursively
disp('Building tree')
tree = make_tree(binned_features, train_targets, inc_node, Nbins); %Make the decision region according to the tree
disp('Building decision surface using the tree')
targets = use_tree(binned_xy, 1:N^2, tree, Nbins, unique(train_targets)); D = reshape(targets,N,N);
%END function targets = use_tree(features, indices, tree, Nbins, Uc)
%Classify recursively using a tree targets = zeros(1, size(features,2)); if (size(features,1) == 1),
%Only one dimension left, so work on it
for i = 1:Nbins,
in = indices(find(features(indices) == i));
if ~isempty(in),
if isfinite(tree.child(i)),
targets(in) = tree.child(i);
else
%No data was found in the training set for this bin, so choose it randomally
n = 1 + floor(rand(1)*length(Uc));
targets(in) = Uc(n);
end
end
end
break
end %This is not the last level of the tree, so:
%First, find the dimension we are to work on
dim = tree.split_dim;
dims= find(~ismember(1:size(features,1), dim)); %And classify according to it
for i = 1:Nbins,
in = indices(find(features(dim, indices) == i));
targets = targets + use_tree(features(dims, :), in, tree.child(i), Nbins, Uc);
end %END use_tree function tree = make_tree(features, targets, inc_node, Nbins)
%Build a tree recursively [Ni, L] = size(features);
Uc = unique(targets); %When to stop: If the dimension is one or the number of examples is small
if ((Ni == 1) | (inc_node > L)),
%Compute the children non-recursively
for i = 1:Nbins,
tree.split_dim = 0;
indices = find(features == i);
if ~isempty(indices),
if (length(unique(targets(indices))) == 1),
tree.child(i) = targets(indices(1));
else
H = hist(targets(indices), Uc);
[m, T] = max(H);
tree.child(i) = Uc(T);
end
else
tree.child(i) = inf;
end
end
break
end %Compute the node's I
for i = 1:Ni,
Pnode(i) = length(find(targets == Uc(i))) / L;
end
Inode = -sum(Pnode.*log(Pnode)/log(2)); %For each dimension, compute the gain ratio impurity
delta_Ib = zeros(1, Ni);
P = zeros(length(Uc), Nbins);
for i = 1:Ni,
for j = 1:length(Uc),
for k = 1:Nbins,
indices = find((targets == Uc(j)) & (features(i,:) == k));
P(j,k) = length(indices);
end
end
Pk = sum(P);
P = P/L;
Pk = Pk/sum(Pk);
info = sum(-P.*log(eps+P)/log(2));
delta_Ib(i) = (Inode-sum(Pk.*info))/-sum(Pk.*log(eps+Pk)/log(2));
end %Find the dimension minimizing delta_Ib
[m, dim] = max(delta_Ib); %Split along the 'dim' dimension
tree.split_dim = dim;
dims = find(~ismember(1:Ni, dim));
for i = 1:Nbins,
indices = find(features(dim, :) == i);
tree.child(i) = make_tree(features(dims, indices), targets(indices), inc_node, Nbins);
end
数据挖掘中ID3算法实现zz的更多相关文章
- 数据挖掘之决策树ID3算法(C#实现)
决策树是一种非常经典的分类器,它的作用原理有点类似于我们玩的猜谜游戏.比如猜一个动物: 问:这个动物是陆生动物吗? 答:是的. 问:这个动物有鳃吗? 答:没有. 这样的两个问题顺序就有些颠倒,因为一般 ...
- 决策树-预测隐形眼镜类型 (ID3算法,C4.5算法,CART算法,GINI指数,剪枝,随机森林)
1. 1.问题的引入 2.一个实例 3.基本概念 4.ID3 5.C4.5 6.CART 7.随机森林 2. 我们应该设计什么的算法,使得计算机对贷款申请人员的申请信息自动进行分类,以决定能否贷款? ...
- Python实现ID3算法
自己用Python写的数据挖掘中的ID3算法,现在觉得Python是实现算法的最好工具: 先贴出ID3算法的介绍地址http://wenku.baidu.com/view/cddddaed0975f4 ...
- 机器学习之决策树(ID3)算法与Python实现
机器学习之决策树(ID3)算法与Python实现 机器学习中,决策树是一个预测模型:他代表的是对象属性与对象值之间的一种映射关系.树中每个节点表示某个对象,而每个分叉路径则代表的某个可能的属性值,而每 ...
- 决策树 -- ID3算法小结
ID3算法(Iterative Dichotomiser 3 迭代二叉树3代),是一个由Ross Quinlan发明的用于决策树的算法:简单理论是越是小型的决策树越优于大的决策树. 算法归 ...
- 机器学习笔记----- ID3算法的python实战
本文申明:本文原创,如有转载请申明.数据代码来自实验数据都是来自[美]Peter Harrington 写的<Machine Learning in Action>这本书,侵删. Hell ...
- 决策树笔记:使用ID3算法
决策树笔记:使用ID3算法 决策树笔记:使用ID3算法 机器学习 先说一个偶然的想法:同样的一堆节点构成的二叉树,平衡树和非平衡树的区别,可以认为是"是否按照重要度逐渐降低"的顺序 ...
- paper 56 :机器学习中的算法:决策树模型组合之随机森林(Random Forest)
周五的组会如约而至,讨论了一个比较感兴趣的话题,就是使用SVM和随机森林来训练图像,这样的目的就是 在图像特征之间建立内在的联系,这个model的训练,着实需要好好的研究一下,下面是我们需要准备的入门 ...
- ID3算法 决策树的生成(2)
# coding:utf-8 import matplotlib.pyplot as plt import numpy as np import pylab def createDataSet(): ...
随机推荐
- 转 GraphQL Schema Stitching explained: Schema Delegation
转自官方文档 In the last article, we discussed the ins and outs of remote (executable) schemas. These remo ...
- column count of mysql.proc is wrong. expected 20,found 16. the table is probably corruptd.
1558 1547 column count of mysql.proc is wrong. expected 20,found 16. the table is probably corruptd. ...
- emacs之配置gtags
~/emacsConfig/gtags-setting.el (if (eq system-type 'darwin) (add-to-list 'load-path "/usr/local ...
- TCL数组
数组是一组使用索引对应元素的排列方式.常规数组的语法如下所示. set ArrayName(Index) value 用于创建简单数组的例子,如下所示. #!/usr/bin/tclsh set la ...
- java之mail发送
一.邮箱的应用在实际的Java开发过程中,也是非常常见的.我这里就是写了一个简单的邮箱发送功能. 二.邮箱的基本配置,使用的是JavaMailSender的提供的方式来实现邮件的发送 1)qq邮箱的配 ...
- PHP session 跨子域问题总结 ini_set('session.cookie_domain', ".domain.com")
Session主要分两部分: 一个是Session数据,该数据默认情况下是存放在服务器的tmp文件下的,是以文件形式存在 另一个是标志着Session数据的Session Id,Session ID, ...
- 使用PHP自带zlib函数 几行代码实现PHP文件打包下载zip
<?php //获取文件列表 function list_dir($dir){ $result = array(); if (is_dir($dir)){ $file_dir = scandir ...
- Ruby中数组的遍历
转自:http://www.jianshu.com/p/8de9b60f9350 Ruby中有几个常用的遍历数组的方法,本人经常容易搞混.这里列出来总结一下. each: 从数组中取出一个元素,作为某 ...
- C++ 并发编程 01 线程api
1.使用多线程的好处: 提高性能,分离关注点 2. 多线程所在头文件 <thread> 3. 使用线程方式为std::thread(functioncall),如: #include & ...
- Unix socket的准备(一)
套接字地址结构 套接字编程中,五元组是广为人知的. (host_ip, host_port, target_ip, target_port, protocol). 其中 ip 和 port 就是由套接 ...