📄 sparsegridsminingmodel.java
字号:
/**
* Write SG model to PMML element.
*
* @return PMML element of SG model
* @exception MiningException
*/
public Object createPmmlObject() throws MiningException
{
// Create SG model:
SparseGridModel sgModel = new SparseGridModel();
// Set attributes:
sgModel.setModelName( "sparse grid model" );
sgModel.setFunctionName("regression");
sgModel.setAlgorithmName(algorithm);
if (sgType == SparseGridsSettings.SG_TENSOR_PRODUCT_BASIS_TYPE)
sgModel.setSgType( "Tensor_Product_Basis_Type" );
if (sgType == SparseGridsSettings.SG_SIMPLICIAL_BASIS_TYPE)
sgModel.setSgType( "Simplicial_Basis_Type" );
sgModel.setBasisDegree( String.valueOf( basisDegree ) );
if (waveletBasis)
sgModel.setIsWaveletBasis( "1" );
else
sgModel.setIsWaveletBasis( "0" );
if (coarseGrid)
sgModel.setUseCoarseGrid( "1");
else
sgModel.setUseCoarseGrid( "0" );
sgModel.setLevel( String.valueOf(level) );
// Add mining schema:
sgModel.setMiningSchema( (MiningSchema) inputSpec.createPmmlObject() );
// Add attribute levels:
if (attributeLevels != null && attributeLevels.length > 0) {
AttributeLevels attLev = new AttributeLevels();
String sAttLev = "";
for (int i = 0; i < attributeLevels.length; i++) {
sAttLev = sAttLev + String.valueOf( attributeLevels[i] );
if (i < attributeLevels.length - 1)
sAttLev = sAttLev + " ";
};
attLev.setText( sAttLev );
sgModel.setAttributeLevels( attLev );
};
// Add sparse grids:
com.prudsys.pdm.Adapters.PmmlVersion20.SparseGrid[] grids =
new com.prudsys.pdm.Adapters.PmmlVersion20.SparseGrid[ getNumberOfGrids() ];
for (int i = 0; i < getNumberOfGrids(); i++) {
grids[i] = (com.prudsys.pdm.Adapters.PmmlVersion20.SparseGrid)
sparseGrids[i].createPmmlObject();
};
sgModel.setSparseGrid( grids );
return sgModel;
}
/**
* Read SG model from PMML document.
*
* @param reader reader for the PMML document
* @exception MiningException always thrown
*/
public void readPmml( Reader reader ) throws MiningException
{
// com.borland.xml.toolkit.XmlUtil.setEncoding( "UTF-8" );
PMML pmml = PMML.unmarshal( reader );
if( pmml.getSparseGridModelCount() == 0 )
throw new MiningException("no sparse grid model found");
// Read data dictionary:
DataDictionary dictionary = pmml.getDataDictionary();
MiningDataSpecification newMetaData = new MiningDataSpecification();
newMetaData.parsePmmlObject( dictionary );
// Read 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);
};
// Init settings:
SparseGridsSettings sgSettings = new SparseGridsSettings();
sgSettings.setDataSpecification( newMetaData );
setMiningSettings( sgSettings );
// Read SG model:
SparseGridModel[] sgModel = pmml.getSparseGridModel();
parsePmmlObject( sgModel[0] );
}
/**
* Read SG model from PMML element.
*
* @param pmmlObject PMML element to read in
* @exception MiningException could not parse PMML object
*/
public void parsePmmlObject( Object pmmlObject ) throws MiningException
{
SparseGridModel sgm = (SparseGridModel) pmmlObject;
// Get mining schema:
MiningSchema schema = sgm.getMiningSchema();
// Create input specification, get target attribute and inner trafo from schema:
inputSpec = new ApplicationInputSpecification();
inputSpec.parsePmmlObject( schema );
target = inputSpec.getTargetApplicationAttribute();
miningTransform = inputSpec.createInnerTrafoFromInputSpec( miningSettings.getDataSpecification() );
// Add target attribute to mining settings:
SparseGridsSettings sgs = (SparseGridsSettings) miningSettings;
String classificationAttributeName = target.getName();
sgs.setPredictedAttributeName( classificationAttributeName );
MiningAttribute targetAtt = sgs.getDataSpecification().getMiningAttribute( classificationAttributeName );
sgs.setTarget( targetAtt );
// Get attributes:
modelName = sgm.getModelName();
String type = sgm.getSgType();
if (type.equals("Tensor_Product_Basis_Type"))
sgType = SparseGridsSettings.SG_TENSOR_PRODUCT_BASIS_TYPE;
else if(type.equals("Simplicial_Basis_Type"))
sgType = SparseGridsSettings.SG_SIMPLICIAL_BASIS_TYPE;
else
throw new MiningException("unknown sg type");
basisDegree = Integer.parseInt( sgm.getBasisDegree() );
String wavBas = sgm.getIsWaveletBasis();
if (wavBas.equals("0"))
waveletBasis = false;
else if(wavBas.equals("1"))
waveletBasis = true;
else
throw new MiningException("unknown inclusion of wavelet basis");
String coGrid = sgm.getUseCoarseGrid();
if (coGrid.equals("0"))
coarseGrid = false;
else if(coGrid.equals("1"))
coarseGrid = true;
else
throw new MiningException("unknown inclusion of coarse grid");
level = Integer.parseInt( sgm.getLevel() );
// Get AttributeLevels:
AttributeLevels al = sgm.getAttributeLevels();
// Get sparse grids:
com.prudsys.pdm.Adapters.PmmlVersion20.SparseGrid[] sg = sgm.getSparseGrid();
sparseGrids = new SparseGrid[ sg.length ];
for (int i = 0; i < sg.length; i++) {
sparseGrids[i] = new SparseGrid();
sparseGrids[i].parsePmmlObject( sg[i] );
};
}
// -----------------------------------------------------------------------
// Other export methods
// -----------------------------------------------------------------------
/**
* Returns string representation (just few words).
*
* @return string representation
*/
public String toString()
{
return "Sparse grids mining model";
}
// -----------------------------------------------------------------------
// Test
// -----------------------------------------------------------------------
/**
* Test.
*
* @param args arguments (ignored)
*/
public static void main(String[] args)
{
try {
double xorArr[][] = { {0,0,-1}, {1,0,+1}, {0,1,+1}, {1,1,-1} };
MiningInputStream inputData = new MiningArrayStream(xorArr);
MiningDataSpecification metaData = inputData.getMetaData();
SparseGridsSettings sgSet = new SparseGridsSettings();
sgSet.setDataSpecification( metaData );
sgSet.setTarget( metaData.getMiningAttribute( 2 ) );
SparseGridsMiningModel sgmm = new SparseGridsMiningModel();
sgmm.setMiningSettings( sgSet );
sgmm.inputSpec = new ApplicationInputSpecification( metaData );
sgmm.inputSpec.initApplicationInputSpecification( sgSet );
sgmm.setModelName( " XOR Sparse Grid ");
SparseGrid[] sg = new SparseGrid[3];
sg[0] = new SparseGrid();
sg[0].setGridName( "G 1" );
sg[0].setNumberOfAttributes(2);
sg[0].setSparseGridHeader("2_1");
double values[] = new double[15];
values[0] = 2;
values[1] = 2.1;
values[2] = -7.54;
values[6] = 18;
values[12] = 2;
values[14] = 2.7;
sg[0].setSparseGridCoefficients( values );
sg[1] = new SparseGrid();
sg[1].setGridName( "G 2" );
sg[1].setNumberOfAttributes(2);
sg[1].setSparseGridHeader("1_2");
values = new double[15];
values[1] = 1.988;
values[5] = 24;
values[2] = 11;
values[7] = 0.3;
values[12] = 1;
values[13] = 6.2;
sg[1].setSparseGridCoefficients( values );
sg[2] = new SparseGrid();
sg[2].setGridName( "G 3" );
sg[2].setNumberOfAttributes(2);
sg[2].setSparseGridHeader("1_1");
values = new double[9];
values[0] = 1;
values[1] = 2.2;
values[2] = 3;
values[6] = 2;
values[4] = 4;
values[5] = 4.7;
sg[2].setSparseGridCoefficients( values );
sgmm.setSparseGrids( sg );
// Write to PMML:
FileWriter writer = new FileWriter("data/pmml/SparseGridsModel.xml");
sgmm.writePmml(writer);
// Show in explorer:
String explorer = "explorer";
String path ="";
String file = "data\\pmml\\SparseGridsModel.xml";
Runtime.getRuntime().exec(explorer + " " + path + file);
FileReader reader = new FileReader("data/pmml/SparseGridsModel.xml");
sgmm.readPmml( reader );
for (int i = 0; i < sgmm.sparseGrids.length; i++)
System.out.println("SG: " + sgmm.sparseGrids[i].toString());
}
catch (Exception ex) {
ex.printStackTrace();
};
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -