谁能告诉我matlab程序c4.5算法怎么调用吗?下面是程序代码,告诉我怎么调用(具体的使用举例)

作者&投稿:苍梧昂 (若有异议请与网页底部的电邮联系)
matlab程序c4.5算法怎么调用吗~

function test_targets = C4_5(train_patterns, train_targets, test_patterns, inc_node, Nu)

% Classify using Quinlan's C4.5 algorithm
% Inputs:
% training_patterns - Train patterns 模式
%training_targets- Train targets 目标
% test_patterns - Test patterns 测试模式
%inc_node - Percentage of incorrectly assigned samples at a node
% 一个节点中不正确的样本分配比例
% Outputs
%test_targets - Predicted targets 预测目标

%NOTE: In this implementation it is assumed that a pattern vector with fewer than 10 unique values (the parameter Nu)
%is discrete, and will be treated as such. Other vectors will be treated as continuous

[Ni, M]= size(train_patterns);
inc_node = inc_node*M/100;

%Find which of the input patterns are discrete, and discretisize the corresponding
%dimension on the test patterns
discrete_dim = zeros(1,Ni);
for i = 1:Ni,
Ub = unique(train_patterns(i,:));%获得第i行中不重复的元素构成的向量
Nb = length(Ub);
if (Nb <= Nu),
%This is a discrete pattern 这是一个离散的模式
discrete_dim(i)= Nb;
dist = abs(ones(Nb ,1)*test_patterns(i,:) - Ub'*ones(1, size(test_patterns,2)));
[m, in] = min(dist);
test_patterns(i,:) = Ub(in);
end
end

%Build the tree recursively
%disp('Building tree')
tree = make_tree(train_patterns, train_targets, inc_node, discrete_dim, max(discrete_dim), 0);

%Classify test samples
%disp('Classify test samples using the tree')
test_targets = use_tree(test_patterns, 1:size(test_patterns,2), tree, discrete_dim, unique(train_targets));

%END

function targets = use_tree(patterns, indices, tree, discrete_dim, Uc)
%Classify recursively using a tree

targets = zeros(1, size(patterns,2));

if (tree.dim == 0)
%Reached the end of the tree
targets(indices) = tree.child;
return
end

%This is not the last level of the tree, so:
%First, find the dimension we are to work on
dim = tree.dim;
dims= 1:size(patterns,1);

%And classify according to it
if (discrete_dim(dim) == 0),
%Continuous pattern
in= indices(find(patterns(dim, indices) <= tree.split_loc));
targets= targets + use_tree(patterns(dims, :), in, tree.child(1), discrete_dim(dims), Uc);
in= indices(find(patterns(dim, indices) > tree.split_loc));
targets= targets + use_tree(patterns(dims, :), in, tree.child(2), discrete_dim(dims), Uc);
else
%Discrete pattern
Uf= unique(patterns(dim,:));
for i = 1:length(Uf),
if any(Uf(i) == tree.Nf) %Has this sort of data appeared before? If not, do nothing
in = indices(find(patterns(dim, indices) == Uf(i)));
targets= targets + use_tree(patterns(dims, :), in, tree.child(find(Uf(i)==tree.Nf)), discrete_dim(dims), Uc);
end
end
end

%END use_tree

function tree = make_tree(patterns, targets, inc_node, discrete_dim, maxNbin, base)
%Build a tree recursively

[Ni, L] = size(patterns);
Uc = unique(targets);
tree.dim= 0;
%tree.child(1:maxNbin)= zeros(1,maxNbin);
tree.split_loc= inf;

if isempty(patterns),
return
end

%When to stop: If the dimension is one or the number of examples is small
if ((inc_node > L) | (L == 1) | (length(Uc) == 1)),
H= hist(targets, length(Uc));
[m, largest] = max(H);
tree.Nf = [];
tree.split_loc = [];
tree.child = Uc(largest);
return
end

%Compute the node's I
for i = 1:length(Uc),
Pnode(i) = length(find(targets == Uc(i))) / L;
end
Inode = -sum(Pnode.*log(Pnode)/log(2));

%For each dimension, compute the gain ratio impurity
%This is done separately for discrete and continuous patterns
delta_Ib = zeros(1, Ni);
split_loc= ones(1, Ni)*inf;

for i = 1:Ni,
data= patterns(i,:);
Ud = unique(data);
Nbins= length(Ud);
if (discrete_dim(i)),
%This is a discrete pattern
P= zeros(length(Uc), Nbins);
for j = 1:length(Uc),
for k = 1:Nbins,
indices = find((targets == Uc(j)) & (patterns(i,:) == Ud(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));
else
%This is a continuous pattern
P= zeros(length(Uc), 2);

%Sort the patterns
[sorted_data, indices] = sort(data);
sorted_targets = targets(indices);

%Calculate the information for each possible split
I= zeros(1, L-1);
for j = 1:L-1,
%for k =1:length(Uc),
% P(k,1) = sum(sorted_targets(1:j) == Uc(k));
% P(k,2) = sum(sorted_targets(j+1:end) == Uc(k));
%end
P(:, 1) = hist(sorted_targets(1:j) , Uc);
P(:, 2) = hist(sorted_targets(j+1:end) , Uc);
Ps= sum(P)/L;
P= P/L;

Pk = sum(P);
P1 = repmat(Pk, length(Uc), 1);
P1 = P1 + eps*(P1==0);

info= sum(-P.*log(eps+P./P1)/log(2));
I(j)= Inode - sum(info.*Ps);
end
[delta_Ib(i), s] = max(I);
split_loc(i) = sorted_data(s);
end
end

%Find the dimension minimizing delta_Ib
[m, dim] = max(delta_Ib);
dims = 1:Ni;
tree.dim = dim;

%Split along the 'dim' dimension
Nf= unique(patterns(dim,:));
Nbins= length(Nf);
tree.Nf = Nf;
tree.split_loc = split_loc(dim);

%If only one value remains for this pattern, one cannot split it.
if (Nbins == 1)
H= hist(targets, length(Uc));
[m, largest] = max(H);
tree.Nf = [];
tree.split_loc = [];
tree.child = Uc(largest);
return
end

if (discrete_dim(dim)),
%Discrete pattern
for i = 1:Nbins,
indices = find(patterns(dim, :) == Nf(i));
tree.child(i)= make_tree(patterns(dims, indices), targets(indices), inc_node, discrete_dim(dims), maxNbin, base);
end
else
%Continuous pattern
indices1 = find(patterns(dim,:) <= split_loc(dim));
indices2 = find(patterns(dim,:) > split_loc(dim));
if ~(isempty(indices1) | isempty(indices2))
tree.child(1)= make_tree(patterns(dims, indices1), targets(indices1), inc_node, discrete_dim(dims), maxNbin, base+1);
tree.child(2)= make_tree(patterns(dims, indices2), targets(indices2), inc_node, discrete_dim(dims), maxNbin, base+1);
else
H= hist(targets, length(Uc));
[m, largest] = max(H);
tree.child = Uc(largest);
tree.dim = 0;
end
end

将代码保存在matlab中就可以用了
例如:function[poly]=polyadd(poly1,poly2)
%polyadd(poly1,poly2) adds two polynominals possibly of uneven length
if length(poly1)<length(poly2)
short=poly1;
long=poly2;
else
short=poly2;
long=poly1;
end
mz=length(long)-length(short);
if mz>0
poly=[zeros(1,mz),short]+long;
else
poly=long+short;
end
保存名为polyadd.M 在matlab中输入polyadd就可以调用了

function test_targets = C4_5(train_patterns, train_targets, test_patterns, inc_node, Nu)

% Classify using Quinlan's C4.5 algorithm
% Inputs:
% training_patterns - Train patterns 模式
% training_targets - Train targets 目标
% test_patterns - Test patterns 测试模式
% inc_node - Percentage of incorrectly assigned samples at a node
% 一个节点中不正确的样本分配比例
% Outputs
% test_targets - Predicted targets 预测目标

%NOTE: In this implementation it is assumed that a pattern vector with fewer than 10 unique values (the parameter Nu)
%is discrete, and will be treated as such. Other vectors will be treated as continuous

[Ni, M] = size(train_patterns);
inc_node = inc_node*M/100;

%Find which of the input patterns are discrete, and discretisize the corresponding
%dimension on the test patterns
discrete_dim = zeros(1,Ni);
for i = 1:Ni,
Ub = unique(train_patterns(i,:));%获得第i行中不重复的元素构成的向量
Nb = length(Ub);
if (Nb <= Nu),
%This is a discrete pattern 这是一个离散的模式
discrete_dim(i) = Nb;
dist = abs(ones(Nb ,1)*test_patterns(i,:) - Ub'*ones(1, size(test_patterns,2)));
[m, in] = min(dist);
test_patterns(i,:) = Ub(in);
end
end

%Build the tree recursively
%disp('Building tree')
tree = make_tree(train_patterns, train_targets, inc_node, discrete_dim, max(discrete_dim), 0);

%Classify test samples
%disp('Classify test samples using the tree')
test_targets = use_tree(test_patterns, 1:size(test_patterns,2), tree, discrete_dim, unique(train_targets));

%END

function targets = use_tree(patterns, indices, tree, discrete_dim, Uc)
%Classify recursively using a tree

targets = zeros(1, size(patterns,2));

if (tree.dim == 0)
%Reached the end of the tree
targets(indices) = tree.child;
return
end

%This is not the last level of the tree, so:
%First, find the dimension we are to work on
dim = tree.dim;
dims= 1:size(patterns,1);

%And classify according to it
if (discrete_dim(dim) == 0),
%Continuous pattern
in = indices(find(patterns(dim, indices) <= tree.split_loc));
targets = targets + use_tree(patterns(dims, :), in, tree.child(1), discrete_dim(dims), Uc);
in = indices(find(patterns(dim, indices) > tree.split_loc));
targets = targets + use_tree(patterns(dims, :), in, tree.child(2), discrete_dim(dims), Uc);
else
%Discrete pattern
Uf = unique(patterns(dim,:));
for i = 1:length(Uf),
if any(Uf(i) == tree.Nf) %Has this sort of data appeared before? If not, do nothing
in = indices(find(patterns(dim, indices) == Uf(i)));
targets = targets + use_tree(patterns(dims, :), in, tree.child(find(Uf(i)==tree.Nf)), discrete_dim(dims), Uc);
end
end
end

%END use_tree

function tree = make_tree(patterns, targets, inc_node, discrete_dim, maxNbin, base)
%Build a tree recursively

[Ni, L] = size(patterns);
Uc = unique(targets);
tree.dim = 0;
%tree.child(1:maxNbin) = zeros(1,maxNbin);
tree.split_loc = inf;

if isempty(patterns),
return
end

%When to stop: If the dimension is one or the number of examples is small
if ((inc_node > L) | (L == 1) | (length(Uc) == 1)),
H = hist(targets, length(Uc));
[m, largest] = max(H);
tree.Nf = [];
tree.split_loc = [];
tree.child = Uc(largest);
return
end

%Compute the node's I
for i = 1:length(Uc),
Pnode(i) = length(find(targets == Uc(i))) / L;
end
Inode = -sum(Pnode.*log(Pnode)/log(2));

%For each dimension, compute the gain ratio impurity
%This is done separately for discrete and continuous patterns
delta_Ib = zeros(1, Ni);
split_loc = ones(1, Ni)*inf;

for i = 1:Ni,
data = patterns(i,:);
Ud = unique(data);
Nbins = length(Ud);
if (discrete_dim(i)),
%This is a discrete pattern
P = zeros(length(Uc), Nbins);
for j = 1:length(Uc),
for k = 1:Nbins,
indices = find((targets == Uc(j)) & (patterns(i,:) == Ud(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));
else
%This is a continuous pattern
P = zeros(length(Uc), 2);

%Sort the patterns
[sorted_data, indices] = sort(data);
sorted_targets = targets(indices);

%Calculate the information for each possible split
I = zeros(1, L-1);
for j = 1:L-1,
%for k =1:length(Uc),
% P(k,1) = sum(sorted_targets(1:j) == Uc(k));
% P(k,2) = sum(sorted_targets(j+1:end) == Uc(k));
%end
P(:, 1) = hist(sorted_targets(1:j) , Uc);
P(:, 2) = hist(sorted_targets(j+1:end) , Uc);
Ps = sum(P)/L;
P = P/L;

Pk = sum(P);
P1 = repmat(Pk, length(Uc), 1);
P1 = P1 + eps*(P1==0);

info = sum(-P.*log(eps+P./P1)/log(2));
I(j) = Inode - sum(info.*Ps);
end
[delta_Ib(i), s] = max(I);
split_loc(i) = sorted_data(s);
end
end

%Find the dimension minimizing delta_Ib
[m, dim] = max(delta_Ib);
dims = 1:Ni;
tree.dim = dim;

%Split along the 'dim' dimension
Nf = unique(patterns(dim,:));
Nbins = length(Nf);
tree.Nf = Nf;
tree.split_loc = split_loc(dim);

%If only one value remains for this pattern, one cannot split it.
if (Nbins == 1)
H = hist(targets, length(Uc));
[m, largest] = max(H);
tree.Nf = [];
tree.split_loc = [];
tree.child = Uc(largest);
return
end

if (discrete_dim(dim)),
%Discrete pattern
for i = 1:Nbins,
indices = find(patterns(dim, :) == Nf(i));
tree.child(i) = make_tree(patterns(dims, indices), targets(indices), inc_node, discrete_dim(dims), maxNbin, base);
end
else
%Continuous pattern
indices1 = find(patterns(dim,:) <= split_loc(dim));
indices2 = find(patterns(dim,:) > split_loc(dim));
if ~(isempty(indices1) | isempty(indices2))
tree.child(1) = make_tree(patterns(dims, indices1), targets(indices1), inc_node, discrete_dim(dims), maxNbin, base+1);
tree.child(2) = make_tree(patterns(dims, indices2), targets(indices2), inc_node, discrete_dim(dims), maxNbin, base+1);
else
H = hist(targets, length(Uc));
[m, largest] = max(H);
tree.child = Uc(largest);
tree.dim = 0;
end
end

这是该题的程序,请给出调用举例~~~急~

对train_patterns, train_targets, test_patterns, inc_node, Nu这几个参数赋相应的值,在matlab命令窗口中直接输入c4.5回车就行了


win10系统matlab应用怎么安装?win10系统安装matlab的方法
一些用户不知道怎么在win10免激活版系统中安装matlab,针对此疑问,接下来小编告诉大家在win10系统安装matlab的方法。具体步骤如下:1下载的安装文件一般是iso文件格式,是一种镜像文件,打开这个文件有两种办法:1、安装虚拟光驱;2、一般的压缩软件就可以打开,前提是压缩软件设置能够打开iso文件。我使用的...

谁能告诉我这段matlab程序的详细意思,要求每一段都注释
谁能告诉我这段matlab程序的详细意思,要求每一段都注释 30 clearall;clc;N_ofdm=2048;f_delta=15e3;N_block=1000;N_subcarrier=1320;N_CP=144;Symbol_number=14;conv_poly=[23,25];K=5;trel=poly2trellis(K,conv_poly);tail=zeros(1,K-1);mod_... clear all;clc;N_ofdm=2048;f_delta=15e3...

求MATLAB的英文介绍
MATLAB is the "Matrix Laboratory", and so provides many convenient ways for creating matrices of various dimensions. In the MATLAB vernacular, a vector refers to a one dimensional (1×N or N×1) matrix, commonly referred to as an array in other programming languages. A matrix generally refer...

谁能告诉我matlab程序里的这句mesh(X,Y,Z(:,:,3))是什么意思?
mesh函数中x是一个(N维)向量表示在x轴方向的选取的所有格点值。相应的,y也是一个(N维)向量表示在y轴方向的选取的所有格点值。第三个输入z应该是一个N乘N的方阵,代表在这个曲面上对应某个(x,y)的z值。但是在你的程序中,Z并不是一个N乘N的方阵,而是一个N乘N乘m(根据程序,你应该知道m是...

谁能告诉我这些在MATLAB中的代码什么意思
if DNA(i)~='A'&&DNA(i)~='G'&&DNA(i)~='G' %DNA(i)是一个数组,如果第i个数不等于A且不等于G等,则DNA(i)=T DNA(i)='T';end end for i=2:m %建立0矩阵 A1=zeros(m,1);T1=zeros(m,1);G1=zeros(m,1);C1=zeros(m,1);A2=zeros(m);T2=zeros(m);G2=...

谁能告诉我MATLAB输出结果什么样子的?
灰白现成的图片,是指你已经通过MATLAB编程实现了图形了,如果是这样,颜色这些很好解决。图形窗口里,有个箭头图标,点后点击你要修改的坐标轴,弹出该坐标轴的属性,然后有个inspector图标,进去就可以修改你的需要的颜色了。

...谁能支援一下 告诉我这个用matlab怎么编程序啊
程序代码:x=0:0.001:2*pi;\/\/x间隔取0.001,范围[0,2pi];y=3*(1+cos(x));\/\/写出公式 polar(x,y,'r')\/\/画出图像,r表示曲线颜色 代码解析:定义x从0到2π间隔为0.001,然后描述心形线函数,第三行绘图,其中引号内的r代表心形线用红色标记。

我在网上下载了matlab的源代码,谁能告诉我怎么使用
将代码保存在matlab中就可以用了 例如:function[poly]=polyadd(poly1,poly2)polyadd(poly1,poly2) adds two polynominals possibly of uneven length if length(poly1)<length(poly2)short=poly1;long=poly2;else short=poly2;long=poly1;end mz=length(long)-length(short);if mz>0 poly=[...

谁能告诉我matlab程序c4.5算法怎么调用吗?下面是程序代码,告诉我怎么调 ...
function test_targets = C4_5(train_patterns, train_targets, test_patterns, inc_node, Nu)Classify using Quinlan's C4.5 algorithm Inputs:training_patterns - Train patterns 模式 training_targets- Train targets 目标 test_patterns - Test patterns 测试模式 inc_node - Percent...

...别人已经编好的代码,想在matlab7.0上直接使用,可以详细告诉我...
1.打开MATLAB 2.点菜单中的file-->open,找到代码文件并打开 3.在弹出的editor窗口中按快捷键F5运行文件即可 如果是有输入参数的函数文件那对不起,你要么问清编程的人输入参数的意义和输入格式,要么把程序打开把代码看懂。

北湖区13448614556: 谁能告诉我matlab程序c4.5算法怎么调用吗?下面是程序代码,告诉我怎么调用(具体的使用举例) -
晋苛韦迪: function test_targets = C4_5(train_patterns, train_targets, test_patterns, inc_node, Nu)% Classify using Quinlan's C4.5 algorithm% Inputs:% training_patterns - Train patterns 模式% training_targets - Train targets 目标% test_patterns - Test ...

北湖区13448614556: 我在网上下载了matlab的源代码,谁能告诉我怎么使用 -
晋苛韦迪: 存入当前工作区, 默认的是C:\matlab\wok 如果是函数就能直接用了

北湖区13448614556: 谁给我编一下这个matlab程序,告诉我每一步, -
晋苛韦迪: 你的程序写错误 主程序 clear clc m0=[9,9,9,9,9,9,5,5,5,5]*1e+5; %各层质点质量 k0=[1474,1474,1474,1474,1474,1474,572,572,572,572,572]*1e+4; %各层刚度 m=diag(m0); cn=length(m0); [k]=matrixju(k0,cn); %刚度聚合 [x,d]=eig(k,m); d=sqrt...

北湖区13448614556: 高手们帮我注释下这个MATLAB程序吧,越详细越好,最好能告诉我代码的思路,谢啦. -
晋苛韦迪: n=100;du=0.1;%以下都不用改Tmax=-inf; %最大利润for u=10:du:12 x=u+1*randn(n,1); Tx=(x<10)*(-1)+(x>=10&x<=12)*20+(x>12)*(-5); T=mean(Tx); %所有利润的平均值 if Tmax<T Tmax=T; bestu=u; end endTmaxbestu%由于用的是随机数,每次结果都不同.但是n取越大,du取越小,应当是越接近于真实问题

北湖区13448614556: 跪求C4.5算法,C语言的…… -
晋苛韦迪: 具体算法步骤如下; 1创建节点N 2如果训练集为空,在返回节点N标记为Failure 3如果训练集中的所有记录都属于同一个类别,则以该类别标记节点N 4如果候选属性为空,则返回N作为叶节点,标记为训练集中最普通的类; 5for each 候选...

北湖区13448614556: 谁能告诉我一个正确的matlab代码,知道前几个数据,然后预测后几个的.急用,谢谢啦 -
晋苛韦迪: 简单一点:对已有数据进行拟合,根据拟合函数预测后面几个数据; 高级一点:基于BP神经网络进行数据预测

北湖区13448614556: matlab中如何绘制.懂的人告诉我下程序,谢谢... -
晋苛韦迪: 这个图不是matlab画的.但是可以用matlab做出类似的图.如下:s0=0.5;sL=0.85;sym xezplot('0.5*exp(3.5*x)',[s0,sL]);hold onezplot('-0.5*exp(3.5*x)',[s0,sL]);plot([0.85,0.85],0.5*exp(3.5*0.85)*[1,-1]);plot([0.5,0.5],0.5*exp(3.5*0.5)*[1,-1]);

北湖区13448614556: 哪位大神可以告诉我在matlab里用最小二乘法求未知系数怎么写程序,公式为y=ax+b,x和y是已知,求a和b -
晋苛韦迪: 例如: x=[4281 6184 6696 8386 8610 10124 10665 11941] y=[33.4445 42.0849 47.2552 51.0583 55.6392 58.3847 61.8866 64.3721] fun=inline('c(1)+c(2)*x','c','x'); c=lsqcurvefit(fun,[1,16],x,y); a=c(2),b=c(1) a = 0.0041 b = 17.8463

北湖区13448614556: MATLAB程序运行不出来啦!高手指点啊 -
晋苛韦迪: 我也出现过此情况. 比如写了一个名字为 xx.m 的文件. 在另一个程序中如果无意写有 xxx=xx; 类似的命令 系统就会提示出错. 因为xxx=xx, 会让matlab 务认为是函数(function)调用xx.m

北湖区13448614556: 在matlab中如何设计一个程序 -
晋苛韦迪: 代码已经验证:徐文力 新浪博客 clc clear all close all%% flag=1; while flag==1 data=input('Please enter something:','s'); if strcmp(data,'')==0 %判断输入是否为空 msgbox('I get the command!')%若非空 if strcmp(data,'exit') %若非空,且为exit ...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网