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

📄 multipletomultiplemapping.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    int nfMap = 0;
    if (featureMap != null)
      nfMap = featureMap.length;
    for (int i = 0; i < nfMap; i++) {
      double attributeValue   = vector.getValue( ((OneToOneMapping) featureMap[i]).getSourceNameDynamic() );
      double transformedValue = ((OneToOneMapping) featureMap[i]).transformAttributeValue( attributeValue );
      values[k]               = transformedValue;
      k = k + 1;
    };

    // Transformations from mining classifier feature map:
    int ncfMap = 0;
    if (cfMap != null)
      ncfMap = cfMap.length;
    for (int i = 0; i < ncfMap; i++) {
      String[] sourceNameDyn = ( (OneToMultipleMapping) cfMap[i] ).getSourceNameDynamic();
      double[] attributeValue = new double[ sourceNameDyn.length ];
      for (int j = 0; j < sourceNameDyn.length; j++)
        attributeValue[j] = vector.getValue( sourceNameDyn[j] );
      double transformedValue[] = ( (OneToMultipleMapping) cfMap[i] ).transformAttributeValue(attributeValue);
      System.arraycopy(transformedValue, 0, values, k, transformedValue.length);
      k = k + transformedValue.length;
    }

    // Transformations from mining classifier class extensions:
    if (containsMultipleToMultipleMapping()) {
      String[] sourceNameDyn  = getSourceNameDynamic();
      //<<15/03/2005, Frank J. Xu
      //double[] attributeValue = new double[ sourceNameDyn.length ];
      //for (int j = 0; j < sourceNameDyn.length; j++)
      //  attributeValue[j] = vector.getValue( sourceNameDyn[j] );
      //double transformedValue[] = transformAttributeValue(attributeValue);
      //System.arraycopy( transformedValue, 0, values, k, transformedValue.length);
      int nTransformedValueLen = 0;
      for (int j = 0; j < sourceNameDyn.length; j++){
         if(vector.getValue(sourceNameDyn[j]) != MiningVector.INVALID_INDEX)       		
         		nTransformedValueLen++;
      }
      
      if(nTransformedValueLen > 0){
      	double[] attributeValue = new double[nTransformedValueLen];
      	int valIndex = 0;
      	double val = MiningVector.INVALID_INDEX;
        for (int j = 0; j < sourceNameDyn.length; j++){
        	val = vector.getValue( sourceNameDyn[j]);
        	if(val != MiningVector.INVALID_INDEX)
        		attributeValue[valIndex++] = val;         	
        }
        double transformedValue[] = transformAttributeValue(attributeValue);
        System.arraycopy( transformedValue, 0, values, k, transformedValue.length);
        k = k + transformedValue.length;
      }           
      //15/03/2005, Frank J. Xu>>
    };

    // Add remaining attributes:
    for (int i = k; i < transformedMetaData.getAttributesNumber(); i++)
      values[i] = vector.getValue( transformedMetaData.getMiningAttribute(i) );

    // Create transformed mining vector:
    MiningVector transformedVector = new MiningVector( values );

    return transformedVector;
  }

  /**
   * Transforms meta data. Often, this is an idential transformation.
   *
   * @param metaData meta data to transform
   * @return transformed meta data
   * @exception MiningException cannot transform meta data
   */
  public MiningDataSpecification transform( MiningDataSpecification metaData ) throws MiningException {

    // Caching:
    pretransformedMetaData = metaData;

    // Init:
    transformedMetaData = new MiningDataSpecification();
    transformedMetaData.setRelationName( "t_" + metaData.getRelationName() );
    String[] sourceNameDyn = getSourceNameDynamic();

    // Vector of attributes already used in transformation:
    Vector usedSourceAtt = new Vector();

    // Transformations from mining feature map:
    int nfMap = 0;
    if (featureMap != null)
      nfMap = featureMap.length;
    for (int i = 0; i < nfMap; i++) {
      MiningAttribute attribute = ((OneToOneMapping) featureMap[i]).transformAttribute();
      transformedMetaData.addMiningAttribute( attribute );

      // Add to list of used source attributes:
      if ( ((OneToOneMapping) featureMap[i]).isRemoveSourceAttribute() )
        usedSourceAtt.addElement( ((OneToOneMapping) featureMap[i]).getSourceNameDynamic() );
    };

    // Transformations from mining classifier feature map:
    int ncfMap = 0;
    if (cfMap != null)
      ncfMap = cfMap.length;
    for (int i = 0; i < ncfMap; i++) {
      MiningAttribute[] attributes = ((OneToMultipleMapping) cfMap[i]).transformAttribute();
      for (int j = 0; j < attributes.length; j++)
        transformedMetaData.addMiningAttribute( attributes[j] );

      // Add to list of used source attributes:
      if ( ((OneToMultipleMapping) cfMap[i]).isRemoveSourceAttributes() ) {
        String[] sndyn = ((OneToMultipleMapping) cfMap[i]).getSourceNameDynamic();
        for (int j = 0; j < sndyn.length; j++)
          usedSourceAtt.addElement(sndyn[j]);
      };
    };

    // Transformations from mining classifier class extensions:
    if (containsMultipleToMultipleMapping()) {
      MiningAttribute[] attributes = transformAttribute();
      for (int j = 0; j < attributes.length; j++)
      {
        //<<15/03/2005, Frank J. Xu,
      	//only add non-null attributes.
      	if(attributes[j] != null)
      		transformedMetaData.addMiningAttribute( attributes[j] );
        //15/03/2005, Frank J. Xu,>>
      }
      // Add to list of used source attributes:
      if (isRemoveSourceAttributes()) {
        for (int i = 0; i < sourceNameDyn.length; i++)
          usedSourceAtt.addElement( sourceNameDyn[i] );
      };
    };

    // Add all remaining attributes:
    for (int i = 0; i < metaData.getAttributesNumber(); i++) {
      MiningAttribute att = metaData.getMiningAttribute(i);
      if (! usedSourceAtt.contains( att.getName() ) )
        transformedMetaData.addMiningAttribute(att);
    };

    return transformedMetaData;
  }

  // -----------------------------------------------------------------------
  //  Methods of PMML handling
  // -----------------------------------------------------------------------
  /**
   * Creates PMML object of this object.
   *
   * @return array of DerivedValue
   * @exception MiningException cannot create PMML object
   */
  public Object[] createPmmlObjects() throws MiningException
  {
    Vector derivedField = new Vector();

    // Derived fields from mining feature map:
    int nfMap = 0;
    if (featureMap != null)
      nfMap = featureMap.length;
    for (int i = 0; i < nfMap; i++) {
      try {
        DerivedField field = (DerivedField) ((OneToOneMapping) featureMap[i]).createPmmlObject();
        derivedField.addElement( field );
      }
      catch (Exception ex) {};
    };

    // Derived fields from mining classifier feature map:
    int ncfMap = 0;
    if (cfMap != null)
      ncfMap = cfMap.length;
    for (int i = 0; i < ncfMap; i++) {
      try {
        DerivedField[] field = (DerivedField[]) ((OneToMultipleMapping) cfMap[i]).createPmmlObjects();
        for (int j = 0; j < field.length; j++)
          derivedField.addElement( field[j] );
      }
      catch (Exception ex) {};
    };

    // Change vector into array:
    DerivedField[] fields = new DerivedField[ derivedField.size() ];
    for (int i = 0; i < derivedField.size(); i++)
      fields[i] = (DerivedField) derivedField.elementAt(i);

    return fields;
  }

  /**
   * Creates this object from array of DerivedValue.
   *
   * @param pmml array of DerivedValue
   * @exception MiningException cannot parse PMML object
   */
  public void parsePmmlObjects(Object[] pmml) throws MiningException
  {
    DerivedField[] fields = (DerivedField[]) pmml;

    Vector allMaps        = new Vector();
    // -------------------------------------------------------------------------
    // Vector oneToMultInds:
    //
    // This vector is used to resolve the binary attributes for binning. In PMML
    // any category of an attribute that was binned, is stored as NormDiscrete
    // element in a separate (!) DerivedField. Therefore we first must find
    // out which derived fields of the NormDiscrete type belong to the same
    // source attribute. This information is stored in oneToMultInds.
    //
    // Example: The categorical attribute a1 with the categories b1, b2, b3
    // was binned as well as the categorical attribute a2 with the categories
    // c1, c2, c3, c4. Suppose, the following list of derived fields was
    // obtained:
    //
    //     0. b1 - a1
    //     1. c3 - a2
    //     2. b2 - a1
    //     3. b3 - a1
    //     4. c1 - a2
    //     5. c4 - a2
    //     6. c3 - a2
    //
    // Then oneToMultInds would look like:
    //
    //      --      --- --- ---
    //     |a1| -> | 0 | 2 | 3 |
    //      --      --- --- ---
    //     |a2| -> | 1 | 4 | 5 | 6 |
    //      --      --- --- --- ---
    // -------------------------------------------------------------------------
    Vector oneToMultInds  = new Vector();
    int nOne2One          = 0;
    int nOne2Mult         = 0;
    for (int i = 0; i < fields.length; i++) {
      OneToOneMapping one2oneMap       = null;
      OneToMultipleMapping one2multMap = null;
      DerivedField[] multFields        = null;

      // One-to-one mapping:
      one2oneMap = OneToOneMapping.createOneToOneMappingFromPmml( fields[i] );
      if (one2oneMap != null) {
        // nothing more to do
      }
      // One-to-multiple mapping:
      else if ( fields[i].getNormDiscrete() != null ) {
          String sourceName = fields[i].getNormDiscrete().getField();
          boolean mapExists = false;
          for (int k = 0; k < oneToMultInds.size(); k++) {
            IntVector iv = (IntVector) oneToMultInds.elementAt(k);
            int ind = iv.IntegerAt(0);
            String sName = fields[ind].getNormDiscrete().getField();
            if ( sName.equals(sourceName) ) {
              iv.addElement(i);
              mapExists = true;

              boolean lastMap = true;
              for (int l = i+1; l < fields.length; l++) {
                NormDiscrete nd = fields[l].getNormDiscrete();
                if (nd != null && nd.getField().equals(sourceName) ) {
                  lastMap = false;
                  break;
                };
              };
              if (lastMap) {
                one2multMap = new com.prudsys.pdm.Transform.OneToMultiple.Binning();
                multFields = new DerivedField[ iv.size() ];
                for (int l = 0; l < iv.size(); l++)
                  multFields[l] = fields[ iv.IntegerAt(l) ];
              };

              break;
            };
          };
          if (! mapExists) {
            IntVector iv = new IntVector();
            iv.addElement(i);
            oneToMultInds.addElement(iv);
          };
      }

      // Unknown derived field => ignore:
      else
          continue;

      // Parse transformations:
      if (one2oneMap != null) {
        one2oneMap.parsePmmlObject(fields[i]);
        one2oneMap.classifierMap = this;
        allMaps.addElement(one2oneMap);
        nOne2One = nOne2One + 1;
      };
      if (one2multMap != null) {
        one2multMap.parsePmmlObjects(multFields);
        one2multMap.classifierMap = this;
        allMaps.addElement(one2multMap);
        nOne2Mult = nOne2Mult + 1;
      };
    };

    // Assamble mapping arrays:
    featureMap = new OneToOneMapping[nOne2One];
    cfMap      = new OneToMultipleMapping[nOne2Mult];
    nOne2One   = 0;
    nOne2Mult  = 0;
    for (int i = 0; i < allMaps.size(); i++) {
      if ( allMaps.elementAt(i) instanceof OneToOneMapping) {
        featureMap[nOne2One] = (OneToOneMapping) allMaps.elementAt(i);
        nOne2One = nOne2One + 1;
      }
      else if ( allMaps.elementAt(i) instanceof OneToMultipleMapping) {
        cfMap[nOne2Mult] = (OneToMultipleMapping) allMaps.elementAt(i);
        nOne2Mult = nOne2Mult + 1;
      };
    };
  }

  /**
   * Returns string representation of multiple-to-multiple map.
   *
   * @return mining multiple-to-multiple map string representation
   */
  public String toString() {

    String res = "--------Multiple-to-multiple map. " + "\n";
    res        = res + "Source names: ";
    if (sourceName != null)
      for (int i = 0; i < sourceName.length; i++)
        res = res + sourceName[i] + ", ";
    res        = res + "\n";
    res        = res + "Target names: ";
    if (targetName != null)
      for (int i = 0; i < targetName.length; i++)
        res = res + targetName[i] + ", ";
    res        = res + "\n";
    res        = res + "removeSourceAttributes: " + removeSourceAttributes + "\n";
    int nfMap = 0;
    if (featureMap != null)
      nfMap = featureMap.length;
    int ncfMap = 0;
    if (cfMap != null)
      ncfMap = cfMap.length;
    res        = res + "Contains " +
                 String.valueOf( nfMap ) +
                 " one-to-one maps and " +
                 String.valueOf( ncfMap ) +
                 " one-to-multiple maps: " +
                 "\n";
    for (int i = 0; i < nfMap; i++) {
      res = res + "        OneToOneMapping " + String.valueOf(i) + ":" + "\n" +
            featureMap[i].toString();
      if (i < featureMap.length - 1)
        res = res + "\n";
    };
    for (int i = 0; i < ncfMap; i++) {
      res = res + "        OneToMultipleMapping " + String.valueOf(i) + ":" + "\n" +
            cfMap[i].toString();
      if (i < cfMap.length - 1)
        res = res + "\n";
    };

    return res;
  }
}

⌨️ 快捷键说明

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