📄 generate_decision_tree_predict4.1.m
字号:
function predictionResult=Generate_decision_tree_predict(DataBase,TableName)
logintimeout(15);
conn = database('DecisionTreeTest', '', '');
exec(conn,['use',' ',DataBase]);
curs=exec(conn,['select * from',' ',TableName]);
curs=fetch(curs);
predictdata=curs.data;
load('KK2P.mat','KK');
%KK中存储由前面程序得到的决策树模型
curs1=exec(conn,['select b.name from sysobjects a inner join syscolumns b on a.id=b.id where a.name=''',TableName,'''']);
curs1=fetch(curs1);
TableAttributeName=curs1.data;
%读入预测数据在predictdata中,读入表头在
%i代表predictdata中的行号
flag=1;
p=1;
q=1;
ss=0;
ff=0;
sf=0;
fs=0;
for i=1:length(predictdata)
j=1;
while isempty(KK(j).divSen)
nowattributSen=KK(j).attributSen;
%for循环属性列表中查找这个属性的位置flag
for I=1:length(TableAttributeName)
if strcmp(nowattributSen,TableAttributeName{I,1})
flag=I;
break;
end
end
x=predictdata{i,flag};
KKCHAR=whos('x');
if strcmp(KKCHAR(1).class,'double')
predictdata{i,flag}=int2str(predictdata{i,flag});
end
%在j的孩子中找到与第i条记录predictdata{i,flag}对应的孩子
for k=1:length(KK(j).Child)
num=KK(j).Child(k);%取第k个孩子的值
x=KK(num).PNUM;
KKCHAR=whos('x');
if strcmp(KKCHAR(1).class,'double')
x=int2str(x);
end
%如果找到与predictdata{i,flag}匹配的孩子,记录下孩子的标记,并使j更新为这个标志,一直到找到的是叶子结点为止
if strcmp(x,predictdata{i,flag})
j=num;
break;
end
end
end
%while做完,相当于已走到叶子,这样记录下叶子的类标志KK(j).divSen
%因这个例子的类标志只有0,1两种情况,这样把各记录的ID按KK(j).divSen分别记录到相应数组,最后做一次更新。
switch (KK(j).divSen)
case 0
ArrayListA(p)=predictdata{i,1};
p=p+1;
switch predictdata{i,32}
case 0
ff=ff+1;
case 1
sf=sf+1;
end
case 1
ArrayListB(q)=predictdata{i,1};
q=q+1;
switch predictdata{i,32}
case 0
fs=fs+1;
case 1
ss=ss+1;
end
end
end
%把满足0和1的记录写在where语句
for I=1:length(ArrayListA)
x=ArrayListA(I);
KKCHAR=whos('x');
if strcmp(KKCHAR(1).class,'double')
x=int2str(x);
end
if I==1
whereSenA=['where CUSTOMER_ID=',x];
else
whereSenA=[whereSenA,' or CUSTOMER_ID=',x];
end
end
for I=1:length(ArrayListB)
x=ArrayListB(I);
KKCHAR=whos('x');
if strcmp(KKCHAR(1).class,'double')
x=int2str(x);
end
if I==1
whereSenB=['where CUSTOMER_ID=',x];
else
whereSenB=[whereSenB,' or CUSTOMER_ID=',x];
end
end
exec(conn,['update',' ',TableName,' set TARGET_FLAG_predict=0',' ',whereSenA]);
exec(conn,['update',' ',TableName,' set TARGET_FLAG_predict=1',' ',whereSenB]);
m=3
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -