weka.core.pmml.VectorInstance Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of weka-stable Show documentation
Show all versions of weka-stable Show documentation
The Waikato Environment for Knowledge Analysis (WEKA), a machine
learning workbench. This is the stable version. Apart from bugfixes, this version
does not receive any other updates.
/*
* 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 3 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, see .
*/
/*
* VectorInstance.java
* Copyright (C) 2010-2012 University of Waikato, Hamilton, New Zealand
*
*/
package weka.core.pmml;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* Class encapsulating a PMML VectorInstance construct
*
* @author Mark Hall (mhall{[at]}pentaho{[dot]}com)
* @version $Revision: 8034 $
*/
public class VectorInstance implements Serializable {
/** For serialization */
private static final long serialVersionUID = -7543200367512646290L;
/** The ID of this instance */
protected String m_ID;
/** The usually sparse elements of this vector */
protected Array m_values;
/** The fields indexed by this VectorInstance */
protected List m_vectorFields;
/**
* Constructor
*
* @param values the Array of values for this vector instance
* @param vectorFields the mining fields indexed by this vector instance
*/
public VectorInstance(Array values, List vectorFields) {
m_values = values;
m_vectorFields = vectorFields;
}
/**
* Constructor
*
* @param vecElement PMML element containing this vector instance
* @param vectorFields the mining fields indexed by this vector instance
* @throws Exception if something goes wrong
*/
public VectorInstance(Element vecElement, List vectorFields)
throws Exception {
m_vectorFields = vectorFields;
// get the ID
String id = vecElement.getAttribute("id");
if (id == null || id.length() == 0) {
throw new Exception("[VectorInstance] no ID attribute defined!");
}
m_ID = id;
// check for both types of array
NodeList s_arrL = vecElement.getElementsByTagName("REAL-SparseArray");
NodeList d_arrL = vecElement.getElementsByTagName("REAL-ARRAY");
if (s_arrL.getLength() == 0 && d_arrL.getLength() == 0) {
throw new Exception("[VectorInstance] no arrays defined!");
}
NodeList arrL = (s_arrL.getLength() > 0)
? s_arrL
: d_arrL;
// should be just one array per vector instance
Element theArray = (Element)arrL.item(0);
m_values = Array.create(theArray);
}
/**
* Get the ID of this vector instance
*
* @return the ID of this vector instance
*/
public String getID() {
return m_ID;
}
/**
* Get the Array of values encapsulated in this vector instance
*
* @return the Array of values encapsulated in this vector instance
*/
public Array getValues() {
return m_values;
}
/**
* Get the mining fields that are indexed by this vector instance
*
* @return the mining fields that are indexed by this vector instance
*/
public List getVectorFields() {
return m_vectorFields;
}
/**
* Subtract the values in the supplied array from this vector instance
*
* @param other an array of values
* @return a new VectorInstance containing the result of the subtraction
* @throws Exception if something goes wrong
*/
public VectorInstance subtract(double[] other) throws Exception {
// other is a non-sparse array of values
ArrayList