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

📄 wekasupervisedminingmodel.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
字号:
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

 /**
  * Title: XELOPES Data Mining Library
  * Description: The XELOPES library is an open platform-independent and data-source-independent library for Embedded Data Mining.
  * Copyright: Copyright (c) 2002 Prudential Systems Software GmbH
  * Company: ZSoft (www.zsoft.ru), Prudsys (www.prudsys.com)
  * @author Michael Thess
  * @version 1.0
  */

package com.prudsys.pdm.Adapters.Weka;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.StringTokenizer;

import com.prudsys.pdm.Adapters.PmmlVersion20.AttributeInstance;
import com.prudsys.pdm.Adapters.PmmlVersion20.DataDictionary;
import com.prudsys.pdm.Adapters.PmmlVersion20.Header;
import com.prudsys.pdm.Adapters.PmmlVersion20.PMML;
import com.prudsys.pdm.Adapters.PmmlVersion20.TransformationDictionary;
import com.prudsys.pdm.Adapters.PmmlVersion20.VectorDictionary;
import com.prudsys.pdm.Adapters.PmmlVersion20.VectorInstance;
import com.prudsys.pdm.Core.MiningDataSpecification;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Models.Supervised.SupervisedMiningModel;
import com.prudsys.pdm.Models.Supervised.SupervisedMiningSettings;
import com.prudsys.pdm.Transform.MiningTransformationActivity;
import com.prudsys.pdm.Utils.PmmlUtils;

/**
  * Representation of a Weka supervised mining model.
  */
public class WekaSupervisedMiningModel extends SupervisedMiningModel
{

  /**
   * Default constructor.
   */
  public WekaSupervisedMiningModel()
  {
  }

    /**
     * Creates PMML document of classifier model.
     * Serialization is used for the model. Just experimental.
     *
     * @param writer writer for PMML model
     * @exception MiningException cannot write PMML model
     * @see com.prudsys.pdm.Adapters.PmmlVersion20.TreeModel
     */
    public void writePmml( Writer writer ) throws MiningException
    {
        PMML pmml = new PMML();
        pmml.setVersion( "2.0" );
        pmml.setHeader( (Header)PmmlUtils.getHeader() );

        MiningDataSpecification metaData = miningSettings.getDataSpecification();
        if ( metaData.isTransformed() )
        {
            pmml.setDataDictionary( (DataDictionary)metaData.getPretransformedMetaData().createPmmlObject() );
            pmml.setTransformationDictionary( (com.prudsys.pdm.Adapters.PmmlVersion20.TransformationDictionary)metaData.getMiningTransformationActivity().createPmmlObject() );
        }
        else
        {
            pmml.setDataDictionary( (DataDictionary)metaData.createPmmlObject() );
        };

        // Serialize classifier object:
        ByteArrayOutputStream ostream = new ByteArrayOutputStream();;
        try {
          ObjectOutputStream out = new ObjectOutputStream(ostream);
          out.writeObject(classifier);
          out.flush();
          out.close();
        }
        catch (Exception ex){;};
        
        //<<21/02/2005, Frank J. Xu
        /*
        //To solve the speed problem, delete the following code lines temporarily.
        byte[] barr = ostream.toByteArray();
        String arrSt = "";
        for (int i = 0; i < barr.length; i++) {
          arrSt = arrSt + String.valueOf( (int)barr[i] );
          if (i < barr.length-1)
            arrSt = arrSt + " ";
        };

        // Add classifier object to Vector dictionary:
        AttributeInstance attI = new AttributeInstance();
        attI.setValue(arrSt);
        VectorInstance vi = new VectorInstance();
        vi.setId("1");
        vi.addAttributeInstance(attI);
        VectorDictionary vd = new VectorDictionary();
        vd.addVectorInstance(vi);
        vd.setNumberOfVectors("1");

        pmml.setVectorDictionary(vd);
        */
        //21/02/2005, Frank J. Xu>>

        // Add encoding and write to document:
        PmmlUtils.setEncoding();
        PmmlUtils.marshalPmml(writer, pmml);
    }

    /**
     * Read classifier model from PMML document. Just experimental.
     *
     * @param reader reader for the PMML document
     * @exception MiningException cannot read PMML model
     */
    public void readPmml( Reader reader ) throws MiningException
    {
//        com.borland.xml.toolkit.XmlUtil.setEncoding( "UTF-8" );
        PMML pmml = PMML.unmarshal( reader );

        DataDictionary dictionary = pmml.getDataDictionary();
        MiningDataSpecification newMetaData = new MiningDataSpecification();
        newMetaData.parsePmmlObject( dictionary );

        // Get transformation dictionary:
        TransformationDictionary transDict = pmml.getTransformationDictionary();
        if (transDict != null) {
          MiningTransformationActivity mta = new MiningTransformationActivity();
          mta.parsePmmlObject(transDict);
          MiningDataSpecification tmds = mta.transform(newMetaData);
          tmds.setPretransformedMetaData(newMetaData);
          newMetaData = tmds;
          newMetaData.setMiningTransformationActivity( mta );
          newMetaData.setTransformed(true);
        };

        SupervisedMiningSettings cls = new SupervisedMiningSettings();
        cls.setDataSpecification( newMetaData );
        setMiningSettings( cls );

        VectorDictionary vd = pmml.getVectorDictionary();
        if (vd == null)
          return;
        VectorInstance vi[] = vd.getVectorInstance();
        AttributeInstance ai[] = vi[0].getAttributeInstance();
        String arrSt = ai[0].getValue();

        arrSt = arrSt.trim();
        StringTokenizer st = new StringTokenizer(arrSt, " ");
        ArrayList<String> arrList = new ArrayList<String>();
        while (st.hasMoreTokens())
          arrList.add(st.nextToken());
        byte[] arrBytes = new byte[arrList.size()];
        for (int i = 0; i < arrList.size(); i++)
          arrBytes[i] = (byte) Integer.parseInt( arrList.get(i) );

        // Deserialize classifier object:
        ByteArrayInputStream istream = new ByteArrayInputStream(arrBytes);
        try {
          ObjectInputStream in = new ObjectInputStream(istream);
          classifier = (WekaClassifier) in.readObject();
          in.close();
        }
        catch (Exception ex){;};
    }

}

⌨️ 快捷键说明

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