📄 unit2.pas
字号:
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('update iris set PW='+' ''D1'' where (petal_width>= '+FloattoStr(min_pw)+' ) and petal_width<'+FloattoStr(delta_pw+min_pw)+'');
adoquery1.execSQL;
//同理,把落在[min_sw+delta_sw,min_sw+2*delta_sw)区间的值 标记为 'D2'
//(默认是'D3',所以最后一个划分区间可不写)
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('update iris set PW='+' ''D2'' where (petal_width>= '+FloattoStr(min_pw+delta_pw)+' ) and petal_width<'+FloattoStr(2*delta_pw+min_pw)+'');
adoquery1.execSQL;
//└---------------------划分end-------------------------┘
//┌-----------------------------------------------------┐
//|现在数据简化为 sepal_length划分为三个等级A1,A2,A3 |
//| sepal_width划分为三个等级B1,B2,B3 |
//| petal_length划分为三个等级C1,C2,C3 |
//| petal_width划分为三个等级D1,D2,D3 |
//└-----------------------------------------------------┘
//查询P(C1| setosa)概率 :即class='Iris-setosa' 下 PL='C1'的概率 ,并显示出来
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select count(*) setosa下C1数 from iris where class='+'''Iris-setosa'' and PL='+'''C1'' ');
adoquery1.Open;
nC1_setosa:=adoquery1.FieldByName('setosa下C1数').AsInteger;
p_C1_setosa:= nC1_setosa/nSt;
Edit1.Text:=FloattoStr(p_C1_setosa);
//查询P(C1| versicolor)概率
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select count(*) versicolor下C1数 from iris where class='+'''Iris-versicolor'' and PL='+'''C1'' ');
adoquery1.Open;
nC1_vc:=adoquery1.FieldByName('versicolor下C1数').AsInteger;
p_C1_vc:= nC1_vc/nVc;
Edit2.Text:=FloattoStr(p_C1_vc);
//查询P(C1| virginica)概率=1-P(C1| setosa)- P(C1| versicolor)
p_C1_vg:=1-p_C1_setosa-p_C1_vc;
Edit3.Text:=FloattoStr(p_C1_vg);
//--------------------
//查询P(C2| setosa)概率 :即class='Iris-setosa' 下 PL='C2'的概率 ,并显示出来
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select count(*) setosa下C2数 from iris where class='+'''Iris-setosa'' and PL='+'''C2'' ');
adoquery1.Open;
nC2_setosa:=adoquery1.FieldByName('setosa下C2数').AsInteger;
p_C2_setosa:= nC2_setosa/nSt;
Edit4.Text:=FloattoStr(p_C2_setosa);
//查询P(C2| versicolor)概率
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select count(*) versicolor下C2数 from iris where class='+'''Iris-versicolor'' and PL='+'''C2'' ');
adoquery1.Open;
nC2_vc:=adoquery1.FieldByName('versicolor下C2数').AsInteger;
p_C2_vc:= nC2_vc/nSt;
Edit5.Text:=FloattoStr(p_C2_vc);
//查询P(C2| virginica)概率=1-P(C2| setosa)- P(C2| versicolor)
p_C2_vg:=1-p_C2_setosa-p_C2_vc;
Edit6.Text:=FloattoStr(p_C2_vg);
//--------------------
//查询P(C3| setosa)概率 :即class='Iris-setosa' 下 PL='C3'的概率 ,并显示出来
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select count(*) setosa下C3数 from iris where class='+'''Iris-setosa'' and PL='+'''C3'' ');
adoquery1.Open;
nC3_setosa:=adoquery1.FieldByName('setosa下C3数').AsInteger;
p_C3_setosa:= nC3_setosa/ nSt;
Edit7.Text:=FloattoStr(p_C3_setosa);
//查询P(C3| versicolor)概率
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select count(*) versicolor下C3数 from iris where class='+'''Iris-versicolor'' and PL='+'''C3'' ');
adoquery1.Open;
nC3_vc:=adoquery1.FieldByName('versicolor下C3数').AsInteger;
p_C3_vc:= nC3_vc/ nVc;
Edit8.Text:=FloattoStr(p_C3_vc);
//查询P(C3| virginica)概率=1-P(C3| setosa)- P(C3| versicolor)
p_C3_vg:=1-p_C3_setosa-p_C3_vc;
Edit9.Text:=FloattoStr(p_C3_vg);
//------------------------------------------------------------------------------
//查询P(D1| setosa)概率 :即class='Iris-setosa' 下 PL='D1'的概率 ,并显示出来
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select count(*) setosa下D1数 from iris where class='+'''Iris-setosa'' and PW='+'''D1'' ');
adoquery1.Open;
nD1_setosa:=adoquery1.FieldByName('setosa下D1数').AsInteger;
p_D1_setosa:= nD1_setosa/ nSt;
Edit10.Text:=FloattoStr(p_D1_setosa);
//查询P(C1| versicolor)概率
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select count(*) versicolor下D1数 from iris where class='+'''Iris-versicolor'' and PW='+'''D1'' ');
adoquery1.Open;
nD1_vc:=adoquery1.FieldByName('versicolor下D1数').AsInteger;
p_D1_vc:= nD1_vc/ nVc;
Edit11.Text:=FloattoStr(p_D1_vc);
//查询P(D1| virginica)概率=1-P(D1| setosa)- P(D1| versicolor)
p_D1_vg:=1-p_C1_setosa-p_D1_vc;
Edit12.Text:=FloattoStr(p_D1_vg);
//--------------------
//查询P(C2| setosa)概率 :即class='Iris-setosa' 下 PL='C2'的概率 ,并显示出来
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select count(*) setosa下D2数 from iris where class='+'''Iris-setosa'' and PW='+'''D2'' ');
adoquery1.Open;
nD2_setosa:=adoquery1.FieldByName('setosa下D2数').AsInteger;
p_D2_setosa:= nD2_setosa/ nSt;
Edit13.Text:=FloattoStr(p_D2_setosa);
//查询P(C2| versicolor)概率
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select count(*) versicolor下D2数 from iris where class='+'''Iris-versicolor'' and PW='+'''D2'' ');
adoquery1.Open;
nD2_vc:=adoquery1.FieldByName('versicolor下D2数').AsInteger;
p_D2_vc:= nD2_vc/ nVc;
Edit14.Text:=FloattoStr(p_D2_vc);
//查询P(D2| virginica)概率=1-P(D2| setosa)- P(D2| versicolor)
p_D2_vg:=1-p_D2_setosa-p_D2_vc;
Edit15.Text:=FloattoStr(p_D2_vg);
//--------------------
//查询P(D3| setosa)概率 :即class='Iris-setosa' 下 PW='D3'的概率 ,并显示出来
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select count(*) setosa下D3数 from iris where class='+'''Iris-setosa'' and PW='+'''D3'' ');
adoquery1.Open;
nD3_setosa:=adoquery1.FieldByName('setosa下D3数').AsInteger;
p_D3_setosa:= nD3_setosa/nSt;
Edit16.Text:=FloattoStr(p_D3_setosa);
//查询P(C3| versicolor)概率
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select count(*) versicolor下D3数 from iris where class='+'''Iris-versicolor'' and PW='+'''D3'' ');
adoquery1.Open;
nD3_vc:=adoquery1.FieldByName('versicolor下D3数').AsInteger;
p_D3_vc:= nD3_vc/nVc;
Edit17.Text:=FloattoStr(p_D3_vc);
//查询P(D 3| virginica)概率=1-P(D3| setosa)- P(D3| versicolor)
p_D3_vg:=1-p_D3_setosa-p_D3_vc;
Edit18.Text:=FloattoStr(p_D3_vg);
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select * from iris ');
adoquery1.Open;
end;
procedure TForm2.BitBtn2Click(Sender: TObject);
var
nRight,nPredict:integer;
TP,FP:double;
begin
Label20.Caption:='训练集数据30个(原表的ID:41-50,91-100,141-150组成)';
//┌-----------------------------------------------------------------------┐
//|说明: |
//|本程序的预测部分 |
//| 没有用sepal_length,sepal_width,petal_length,petal_width这四个值来预测 |
//| 而是用petal_length,petal_width两个值来分类. |
//| 这样处理便于计算并且 petal_length,petal_width在划分上较另两个显著 |
//└-----------------------------------------------------------------------┘
//对sepal_length(花瓣长)进行划分,把落在区间[min_pl,min_pl+delta_pl) 的值 标记为 'C1'
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('update iris_predict set PL='+' ''C1'' where (petal_length>= '+FloattoStr(min_pl)+' ) and petal_length<'+FloattoStr(delta_pl+min_pl)+'');
adoquery2.execSQL;
//对sepal_length(花瓣长)进行划分,把落在区间[min_pl+delta_pl,min_pl+2*delta_pl) 的值 标记为 'C2'
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('update iris_predict set PL='+' ''C2'' where (petal_length>= '+FloattoStr(min_pl+delta_pl)+' ) and petal_length<'+FloattoStr(2*delta_pl+min_pl)+'');
adoquery2.execSQL;
//对sepal_length(花瓣长)进行划分,把落在区间[min_pl+2*delta_pl,min_pl+3*delta_pl] 的值 标记为 'C3'
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('update iris_predict set PL='+' ''C3'' where (petal_length>= '+FloattoStr(min_pl+2*delta_pl)+' ) and petal_length<='+FloattoStr(3*delta_pl+min_pl)+'');
adoquery2.execSQL;
// ----------------------------
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('update iris_predict set PW='+' ''D1'' where (petal_length>= '+FloattoStr(min_pl)+' ) and petal_length<'+FloattoStr(delta_pl+min_pl)+'');
adoquery2.execSQL;
//
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('update iris_predict set PW='+' ''D2'' where (petal_length>= '+FloattoStr(min_pl+delta_pl)+' ) and petal_length<'+FloattoStr(2*delta_pl+min_pl)+'');
adoquery2.execSQL;
//
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('update iris_predict set PW='+' ''D3'' where (petal_length>= '+FloattoStr(min_pl+2*delta_pl)+' ) and petal_length<='+FloattoStr(3*delta_pl+min_pl)+'');
adoquery2.execSQL;
//┌-------------------------------------------------------------------------┐
//| C1D1 -> Iris-setosa , C2D1 -> Uknown , C3D1 -> Uknown |
//| C1D2 -> Uknown , C2D2 -> Iris-versicolor , C3D2 -> Iris-versicolor|
//| C1D3 -> Uknown , C2D3 -> Iris-Virginica , C3D3 -> Iris-Virginica |
//└-------------------------------------------------------------------------┘
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('select * from iris_predict');
adoquery2.Open;
//-------------------------预测C1D1---------------------------
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('update iris_predict set predict=''Iris-setosa'' where PL='+'''C1'' and PW='+'''D1'' ');
adoquery2.execSQL;
//-------------------------预测C1D2---------------------------
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('update iris_predict set predict='+' ''Uknown'' where PL='+'''C1'' and PW='+'''D2'' ');
adoquery2.execSQL;
//-------------------------预测C1D3---------------------------
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('update iris_predict set predict='+'''Uknown'' where PL='+'''C1'' and PW='+'''D3'' ');
adoquery2.execSQL;
//-------------------------预测C2D1---------------------------
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('update iris_predict set predict='+'''Uknown'' where PL='+'''C2'' and PW='+'''D1''');
adoquery2.execSQL;
//-------------------------预测C2D2---------------------------
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('update iris_predict set predict='+'''Iris-versicolor'' where PL='+'''C2'' and PW='+'''D2''');
adoquery2.execSQL;
//-------------------------预测C2D3---------------------------
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('update iris_predict set predict='+'''Iris-virginica'' where PL='+'''C2'' and PW='+'''D3''');
adoquery2.execSQL;
//-------------------------预测C3D1---------------------------
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('update iris_predict set predict='+'''Uknown'' where PL='+'''C3'' and PW='+'''D1''');
adoquery2.execSQL;
//-------------------------预测C3D2---------------------------
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('update iris_predict set predict='+'''Iris-versicolor'' where PL='+'''C3'' and PW='+'''D2''');
adoquery2.execSQL;
//-------------------------预测C3D3---------------------------
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('update iris_predict set predict='+'''Iris-virginica'' where PL='+'''C3'' and PW='+'''D3''');
adoquery2.execSQL;
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('select * from iris_predict ');
adoquery2.Open;
//计算TP,FP
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('select count(*) 匹配个数 from iris_predict i where i.class=i.predict');
adoquery2.Open;
nRight:=adoquery2.FieldByName('匹配个数').AsInteger;
//
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('select count(*) 总个数 from iris_predict');
adoquery2.Open;
nPredict:=adoquery2.FieldByName('总个数').AsInteger;
TP:= nRight/nPredict;
Edit19.Text:=FloattoStr(TP);
FP:=1-TP;
Edit20.Text:=FloattoStr(FP);
// 显示所有
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('select * from iris_predict');
adoquery2.Open;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -