📄 apprentissage.java
字号:
import java.util.*;import java.io.*; public class Apprentissage{ private Data data; private Gain gainM; private Vector eRegles; private Vector posNonCouv; private double Entropie; Apprentissage(String file) { this.data = new Data(file); this.gainM = new GainAsk(); } Apprentissage(String file, Gain gain) { this.data = new Data(file); this.gainM = gain; this.setEntropie(this.data); } public void setGain(Gain ga) { this.gainM = ga; } public void setData(Data dt) { this.data = dt; } public void setEntropie(Data dt) { Vector positif = data.getPos(); Vector negatif = data.getNeg(); int gp = positif.size(); int gn = negatif.size(); this.Entropie = entropie(gp, gn); } public double getEntropie() { return Entropie; } /** * methode pour calculer l'Entropie * @param p est le nombre des exemples positifs * @param n est le nombre des exemples negatifs * @return entropie */ public double entropie(int p,int n) { float p1 = (float)p; float n1 = (float)n; if ((p1!=0)&&(n1!=0)) { return (float)(- (p1/(p1+n1))*(Math.log(p1/(p1+n1))/Math.log(2.0)) - (n1/(p1+n1))*(Math.log(n1/(p1+n1))/Math.log(2.0))); } else { return 0; } } /** * methode pour retourne le gain d'un attribut * @param attr est le nom d'un attribut * @param pos est le Vector exemples positifs * @param neg est le Vector exemples negatifs */ public double calculGain(String attr, Vector pos, Vector neg) { int pg = pos.size(); int ng = neg.size(); Vector attrV = getAttributValues1(attr); Iterator it = attrV.iterator(); double entropie = 0, gainAttribut = 0, Ent = 0; while (it.hasNext()) { String litteral = ""; litteral = attr+"="+(String)it.next(); Vector satisfaitLPos = satisfait(litteral,pos); Vector satisfaitLNeg = satisfait(litteral,neg); int p = satisfaitLPos.size(); int n = satisfaitLNeg.size(); entropie = entropie + (float)(p+n)/(float)(pg+ng)*entropie(p,n); } Ent = entropie(pg, ng); gainAttribut = (Ent -entropie); return gainAttribut; } /** * methode permettant de recuperer le valeur de Gain pour chaque attribut * @param attr * @return le Gain par chaque attribut */ public double calculGain(String attr) { int pg = data.getPos().size(); int ng = data.getNeg().size(); Vector attrV = getAttributValues1(attr); Iterator it = attrV.iterator(); double entropie = 0, gainAttribut = 0, Ent = 0; while (it.hasNext()) { String litteral = ""; litteral = attr+"="+(String)it.next(); Vector satisfaitLPos = satisfait(litteral,data.getPos()); Vector satisfaitLNeg = satisfait(litteral,data.getNeg()); int p = satisfaitLPos.size(); int n = satisfaitLNeg.size(); entropie = entropie + (float)(p+n)/(float)(pg+ng)*entropie(p,n); } Ent = entropie(pg, ng); gainAttribut = (Ent -entropie); return gainAttribut; } /** * methode pour construire l'arbre par l'attribut ce que choisi par l'utilisateur * @param attrName * @return l'Arbre decision */ public String construArbreParUtilisateur(String attrName){ Vector pos = data.getPos(); Vector neg = data.getNeg(); Vector postemp = new Vector(); Vector negtemp = new Vector(); Attribut attribut = data.getAttributOfAttribut(attrName); String Resultat = ""; Vector attrAll = getAttributsAllVector(); attrAll.remove(attrAll.lastElement()); Vector values = attribut.getValues(); Iterator it = values.iterator(); attrAll.removeElement(attrName); Arbre arbre = new Arbre(); while (it.hasNext()) { Vector attrAllTemp = (Vector)attrAll.clone(); String st = (String)it.next(); String lit = attribut.getAttribut()+"="+st; postemp = satisfait(lit,pos); negtemp = satisfait(lit,neg); Iterator itt = attrAllTemp.iterator(); Noeud NoeudRacine = new Noeud(); NoeudRacine.setNoeud(lit); // Resultat += NoeudRacine.getNoeud(); if(postemp.size()!=0 && negtemp.size()!=0) { NoeudRacine.setNegV(negtemp); NoeudRacine.setPosV(postemp); double maxGain= 0; String literal = ""; String literaltmp = ""; String removeAttr = ""; while (itt.hasNext()) { String stt = (String)itt.next(); double gainAttribut = calculGain(stt, postemp, negtemp); if(gainAttribut > maxGain && gainAttribut >0) { maxGain = gainAttribut; Attribut MaxGainAttribut = data.getAttributOfAttribut(stt); removeAttr =stt; Vector valeurs = MaxGainAttribut.getValues(); Iterator ittt = valeurs.iterator(); literaltmp = ""; while (ittt.hasNext()) { String sttt = (String)ittt.next(); literaltmp += "| "+MaxGainAttribut.getAttribut()+"="+sttt+"\n"; } } } attrAllTemp.removeElement(removeAttr); literal += literaltmp; NoeudRacine.addFils(literal); NoeudRacine.setfilsAttrName(removeAttr); NoeudRacine.setAttrL(attrAllTemp); } else if(postemp.size()==0 && negtemp.size()!=0) { NoeudRacine.setPosV(postemp); }else if(postemp.size()!=0 && negtemp.size()==0) { NoeudRacine.setNegV(postemp); } arbre.addNoeud(NoeudRacine); } Vector noeuds = arbre.getNoeuds(); Arbre arbreFinal = arbre; Iterator itNoeud = noeuds.iterator(); Resultat = arbreFinal.getArbre(); while(itNoeud.hasNext()) { Noeud noeud = (Noeud)itNoeud.next(); boolean test = sonFilsIsFeuille(noeud); if(!test){ Resultat +=constru(noeud, arbreFinal); } } return Resultat; } /** * methode pour construire toutes les noeuds * @param noe sont les noeuds ce qu'on a cr閑 par le premier etape * @param arbre de d閏ision * @return expression d'arbre de d閏ision */ public String constru(Noeud noe, Arbre arbre) { String ResultatTmp = ""; Vector pos = noe.getPosV(); Vector neg = noe.getNegV(); Vector attrL = noe.getAttrL(); String noeudName = noe.getNoeud(); String attrName = noe.getfilsAttrName(); boolean SonFilsisFeuille = sonFilsIsFeuille(noe); if(!SonFilsisFeuille) { Attribut attribut = data.getAttributOfAttribut(attrName); Vector values = attribut.getValues(); Iterator itt = values.iterator(); attrL.removeElement(attrName); while (itt.hasNext()) { Vector postemp = new Vector(); Vector negtemp = new Vector(); Vector attrAllTemp = (Vector)attrL.clone(); String st = (String)itt.next(); String lit = attribut.getAttribut()+"="+st; postemp = satisfait(lit,pos); negtemp = satisfait(lit,neg); Iterator ittt = attrAllTemp.iterator(); if(postemp.size()!=0 && negtemp.size()!=0) { Noeud noeudProchaine = new Noeud(); noeudProchaine.setNoeud(noeudName+"&&"+lit); noeudProchaine.setNegV(negtemp); noeudProchaine.setPosV(postemp); double maxGain= 0; String literal = ""; String literaltmp = ""; String removeAttr = ""; while (ittt.hasNext()) { String sttt = (String)ittt.next(); double gainAttribut = calculGain(sttt, postemp, negtemp); if(gainAttribut > maxGain && gainAttribut >0) { maxGain = gainAttribut; Attribut MaxGainAttribut = data.getAttributOfAttribut(sttt); removeAttr =sttt; Vector valeurs = MaxGainAttribut.getValues(); Iterator itttt = valeurs.iterator(); literaltmp = ""; while (itttt.hasNext()) { String stttt = (String)itttt.next(); literaltmp += "| "+MaxGainAttribut.getAttribut()+"="+stttt+"\n"; } } } Vector attrAllTemp1 = (Vector)attrAllTemp.clone(); attrAllTemp1.removeElement(removeAttr); literal += literaltmp; noeudProchaine.addFils(literal); noeudProchaine.setfilsAttrName(removeAttr); noeudProchaine.setAttrL(attrAllTemp1); Arbre arbrehe = new Arbre(); arbrehe.add(noeudProchaine); ResultatTmp +=arbrehe.getArbre(); if(!sonFilsIsFeuille(noeudProchaine)){ ResultatTmp +=constru(noeudProchaine, arbrehe); } } } } return ResultatTmp; } /** * methode pour verifier si le noeud est un feuille ou non * @param noeud * @return true si il est un feuille, sinon return false */ public boolean sonFilsIsFeuille(Noeud noe) { boolean retfils = false; Vector posfils = noe.getPosV(); Vector negfils = noe.getNegV(); String attrNamefils = noe.getfilsAttrName(); if(attrNamefils.equals("")) { retfils = true; } else { Attribut attributfils = data.getAttributOfAttribut(attrNamefils); Vector valuesfils = attributfils.getValues(); Iterator itfils = valuesfils.iterator(); while (itfils.hasNext()) { String stfils = (String)itfils.next(); String litfils = noe.getfilsAttrName()+"="+stfils; Vector postempfils = new Vector(); Vector negtempfils = new Vector(); postempfils = satisfait(litfils,posfils); negtempfils = satisfait(litfils,negfils); if (postempfils.size()!=0 && negtempfils.size()!=0) { return false; } else retfils = true; } } return retfils; } /** * methode permettant d'apprendre un concept, le dernier des attributs * ceci a partir d'exmples , cet esemble de donnees etant contenu dans l'objet de type Exemples attribut de cette classe * @return Vector regle le veteur des regles permettant de conclure sur le concept */ public Vector apprendre(){ posNonCouv = new Vector(); Vector pos = data.getPos(); Vector neg = data.getNeg(); Vector regle = new Vector(); while (!pos.isEmpty()) { String conditions_regle = ""; Vector neg2 = (Vector)neg.clone(); Vector pos2 = (Vector)pos.clone(); while (!neg2.isEmpty()) { String litteral = ""; Vector attrCondR = getAttributCondRegle(conditions_regle); litteral = this.maxGain(pos2,neg2,attrCondR); if (litteral.equals("")) { neg2.removeAllElements(); conditions_regle = ""; } else { if (!conditions_regle.equals("")) { conditions_regle += " && "; } conditions_regle += litteral; this.retirer_non_satisfaisable(neg2,litteral); this.retirer_non_satisfaisable(pos2,litteral); } } if (conditions_regle.equals("")) { posNonCouv.addAll(pos); pos.removeAllElements(); regle.add("Le gain utilis
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -