⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplemi.java

📁 Java 编写的多种数据挖掘算法 包括聚类、分类、预处理等
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    return new SelectedTag(m_TransformMethod, TAGS_TRANSFORMMETHOD);  }  /**    * Implements MITransform (3 type of transformation) 1.arithmatic average;   * 2.geometric centor; 3.merge minima and maxima attribute value together   *   * @param train the multi-instance dataset (with relational attribute)     * @return the transformed dataset with each bag contain mono-instance   * (without relational attribute) so that any classifier not for MI dataset   * can be applied on it.   * @throws Exception if the transformation fails   */  public Instances transform(Instances train) throws Exception{    Attribute classAttribute = (Attribute) train.classAttribute().copy();    Attribute bagLabel = (Attribute) train.attribute(0);    double labelValue;    Instances newData = train.attribute(1).relation().stringFreeStructure();    //insert a bag label attribute at the begining    newData.insertAttributeAt(bagLabel, 0);    //insert a class attribute at the end    newData.insertAttributeAt(classAttribute, newData.numAttributes());    newData.setClassIndex(newData.numAttributes()-1);    Instances mini_data = newData.stringFreeStructure();    Instances max_data = newData.stringFreeStructure();    Instance newInst = new Instance (newData.numAttributes());     Instance mini_Inst = new Instance (mini_data.numAttributes());    Instance max_Inst = new Instance (max_data.numAttributes());    newInst.setDataset(newData);    mini_Inst.setDataset(mini_data);    max_Inst.setDataset(max_data);    double N= train.numInstances( );//number of bags       for(int i=0; i<N; i++){	      int attIdx =1;      Instance bag = train.instance(i); //retrieve the bag instance      labelValue= bag.value(0);      if (m_TransformMethod != TRANSFORMMETHOD_MINIMAX)	            newInst.setValue(0, labelValue);      else {        mini_Inst.setValue(0, labelValue);        max_Inst.setValue(0, labelValue);      }      Instances data = bag.relationalValue(1); // retrieve relational value for each bag       for(int j=0; j<data.numAttributes( ); j++){ 	        double value;        if(m_TransformMethod == TRANSFORMMETHOD_ARITHMETIC){          value = data.meanOrMode(j);           newInst.setValue(attIdx++, value);        }        else if (m_TransformMethod == TRANSFORMMETHOD_GEOMETRIC){          double[] minimax = minimax(data, j);          value = (minimax[0]+minimax[1])/2.0;          newInst.setValue(attIdx++, value);        }        else {  //m_TransformMethod == TRANSFORMMETHOD_MINIMAX          double[] minimax = minimax(data, j);          mini_Inst.setValue(attIdx, minimax[0]);//minima value          max_Inst.setValue(attIdx, minimax[1]);//maxima value          attIdx++;        }      }      if (m_TransformMethod == TRANSFORMMETHOD_MINIMAX) {        if (!bag.classIsMissing())          max_Inst.setClassValue(bag.classValue()); //set class value        mini_data.add(mini_Inst);         max_data.add(max_Inst);      }      else{        if (!bag.classIsMissing())          newInst.setClassValue(bag.classValue()); //set class value        newData.add(newInst);		      }      }    if (m_TransformMethod == TRANSFORMMETHOD_MINIMAX) {      mini_data.setClassIndex(-1);      mini_data.deleteAttributeAt(mini_data.numAttributes()-1); //delete class attribute for the minima data      max_data.deleteAttributeAt(0); // delete the bag label attribute for the maxima data      newData = Instances.mergeInstances(mini_data, max_data); //merge minima and maxima data      newData.setClassIndex(newData.numAttributes()-1);    }	    return newData;  }  /**   * Get the minimal and maximal value of a certain attribute in a certain data   *   * @param data the data   * @param attIndex the index of the attribute   * @return the double array containing in entry 0 for min and 1 for max.   */  public static double[] minimax(Instances data, int attIndex){    double[] rt = {Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY};    for(int i=0; i<data.numInstances(); i++){      double val = data.instance(i).value(attIndex);      if(val > rt[1])        rt[1] = val;      if(val < rt[0])        rt[0] = val;    }    for(int j=0; j<2; j++)      if(Double.isInfinite(rt[j]))        rt[j] = Double.NaN;    return rt;  }  /**   * Returns default capabilities of the classifier.   *   * @return      the capabilities of this classifier   */  public Capabilities getCapabilities() {    Capabilities result = super.getCapabilities();    // attributes    result.enable(Capability.NOMINAL_ATTRIBUTES);    result.enable(Capability.RELATIONAL_ATTRIBUTES);    result.enable(Capability.MISSING_VALUES);    // class    result.disableAllClasses();    result.disableAllClassDependencies();    if (super.getCapabilities().handles(Capability.NOMINAL_CLASS))      result.enable(Capability.NOMINAL_CLASS);    if (super.getCapabilities().handles(Capability.BINARY_CLASS))      result.enable(Capability.BINARY_CLASS);    result.enable(Capability.MISSING_CLASS_VALUES);        // other    result.enable(Capability.ONLY_MULTIINSTANCE);        return result;  }  /**   * Returns the capabilities of this multi-instance classifier for the   * relational data.   *   * @return            the capabilities of this object   * @see               Capabilities   */  public Capabilities getMultiInstanceCapabilities() {    Capabilities result = super.getCapabilities();        // attributes    result.enable(Capability.NOMINAL_ATTRIBUTES);    result.enable(Capability.NUMERIC_ATTRIBUTES);    result.enable(Capability.DATE_ATTRIBUTES);    result.enable(Capability.MISSING_VALUES);    // class    result.disableAllClasses();    result.enable(Capability.NO_CLASS);        return result;  }  /**   * Builds the classifier   *   * @param train the training data to be used for generating the   * boosted classifier.   * @throws Exception if the classifier could not be built successfully   */  public void buildClassifier(Instances train) throws Exception {    // can classifier handle the data?    getCapabilities().testWithFail(train);    // remove instances with missing class    train = new Instances(train);    train.deleteWithMissingClass();        if (m_Classifier == null) {      throw new Exception("A base classifier has not been specified!");    }    if (getDebug())      System.out.println("Start training ...");    Instances data = transform(train);     data.deleteAttributeAt(0); // delete the bagID attribute    m_Classifier.buildClassifier(data);    if (getDebug())      System.out.println("Finish building model");  }		  /**   * Computes the distribution for a given exemplar   *   * @param newBag the exemplar for which distribution is computed   * @return the distribution   * @throws Exception if the distribution can't be computed successfully   */  public double[] distributionForInstance(Instance newBag)    throws Exception {    double [] distribution = new double[2];    Instances test = new Instances (newBag.dataset(), 0);	    test.add(newBag);	    test = transform(test);    test.deleteAttributeAt(0);    Instance newInst=test.firstInstance();    distribution = m_Classifier.distributionForInstance(newInst);    return distribution;	     }  /**   * Gets a string describing the classifier.   *   * @return a string describing the classifer built.   */  public String toString() {	    return "SimpleMI with base classifier: \n"+m_Classifier.toString();  }  /**   * Main method for testing this class.   *   * @param argv should contain the command line arguments to the   * scheme (see Evaluation)   */  public static void main(String[] argv) {    try {      System.out.println(Evaluation.evaluateModel(new SimpleMI(), argv));    } catch (Exception e) {      e.printStackTrace();      System.err.println(e.getMessage());    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -