List of usage examples for weka.core Instance setValue
public void setValue(Attribute att, String value);
From source file:probcog.bayesnets.core.WEKADiscretizationFilter.java
License:Open Source License
public String getValueForContinuous(double continuous) { Instance inst = new Instance(1); inst.setValue(0, continuous); try {//w ww.j a v a 2 s.c om filter.input(inst); filter.batchFinished(); Instance newInst = filter.output(); int value = (int) newInst.value(0); return outputValues[value]; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:probcog.bayesnets.learning.CPTLearner.java
License:Open Source License
/** * learns all the examples in the result set. Each row in the result set represents one example. * All the random variables (nodes) in the network * need to be found in each result row as columns that are named accordingly, i.e. for each * random variable, there must be a column with a matching name in the result set. * @param rs the result set//w w w. java 2s .c o m * @throws Exception if the result set is empty * @throws SQLException particularly if there is no matching column for one of the node names */ public void learn(ResultSet rs) throws Exception { if (!initialized) init(); try { // if it's an empty result set, throw exception if (!rs.next()) throw new Exception("empty result set!"); BeliefNode[] nodes = bn.bn.getNodes(); ResultSetMetaData rsmd = rs.getMetaData(); int numCols = rsmd.getColumnCount(); // Now we can get much more nodes than attributes // if(numCols != nodes.length) // throw new Exception("Result does not contain suitable data (column count = " + numCols + "; node count = " + nodes.length + ")"); // map node indices to result set column indices int[] nodeIdx2colIdx = new int[nodes.length]; Arrays.fill(nodeIdx2colIdx, -1); for (int i = 1; i <= numCols; i++) { Set<String> nodeNames = bn.getNodeNamesForAttribute(rsmd.getColumnName(i)); for (String nodeName : nodeNames) { int node_idx = bn.getNodeIndex(nodeName); if (node_idx == -1) throw new Exception("Unknown node referenced in result set: " + rsmd.getColumnName(i)); nodeIdx2colIdx[node_idx] = i; } } // gather data, iterating over the result set int[] domainIndices = new int[nodes.length]; do { // for each row... // - get the indices into the domains of each node // that correspond to the current row of data // (sorted in the same order as the nodes are ordered // in the BeliefNetwork) for (int node_idx = 0; node_idx < nodes.length; node_idx++) { int domain_idx; if (clusterers[node_idx] == null) { Discrete domain = (Discrete) nodes[node_idx].getDomain(); String strValue; if (domain instanceof Discretized) { // If we have a discretized domain we discretize first... double value = rs.getDouble(nodeIdx2colIdx[node_idx]); strValue = (((Discretized) domain).getNameFromContinuous(value)); } else { strValue = rs.getString(nodeIdx2colIdx[node_idx]); } domain_idx = domain.findName(strValue); if (domain_idx == -1) throw new Exception(strValue + " not found in domain of " + nodes[node_idx].getName()); } else { Instance inst = new Instance(1); double value = rs .getDouble(bn.getAttributeNameForNode(bn.bn.getNodes()[node_idx].getName())); inst.setValue(0, value); domain_idx = clusterers[node_idx].clusterInstance(inst); } domainIndices[node_idx] = domain_idx; } // - update each node's CPT for (int i = 0; i < nodes.length; i++) { counters[i].count(domainIndices); } } while (rs.next()); } catch (SQLException ex) { // handle any database errors System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } }
From source file:probcog.bayesnets.learning.CPTLearner.java
License:Open Source License
/** * learns all the examples in the instances. Each instance in the instances represents one example. * All the random variables (nodes) in the network * need to be found in each instance as columns that are named accordingly, i.e. for each * random variable, there must be an attribute with a matching name in the instance. * @param instances the instances * @throws Exception if the result set is empty * @throws SQLException particularly if there is no matching column for one of the node names *//*from w w w . j a v a 2 s. c om*/ public void learn(Instances instances) throws Exception { if (!initialized) init(); // if it's an empty result set, throw exception if (instances.numInstances() == 0) throw new Exception("empty result set!"); BeliefNode[] nodes = bn.bn.getNodes(); int numAttributes = instances.numAttributes(); // Now we can get much more nodes than attributes // if(numAttributes != nodes.length) // throw new Exception("Result does not contain suitable data (attribute count = " + numAttributes + "; node count = " + nodes.length + ")"); // map node indices to attribute index int[] nodeIdx2colIdx = new int[nodes.length]; Arrays.fill(nodeIdx2colIdx, -1); for (int i = 0; i < numAttributes; i++) { Set<String> nodeNames = bn.getNodeNamesForAttribute(instances.attribute(i).name()); //logger.debug("Nodes for attribute "+instances.attribute(i).name()+": "+nodeNames); if (nodeNames == null) continue; for (String nodeName : nodeNames) { int node_idx = bn.getNodeIndex(nodeName); if (node_idx == -1) throw new Exception("Unknown node referenced in result set: " + instances.attribute(i).name()); nodeIdx2colIdx[node_idx] = i; } } // gather data, iterating over the result set int[] domainIndices = new int[nodes.length]; @SuppressWarnings("unchecked") Enumeration<Instance> instanceEnum = instances.enumerateInstances(); while (instanceEnum.hasMoreElements()) { Instance instance = instanceEnum.nextElement(); // for each row... // - get the indices into the domains of each node // that correspond to the current row of data // (sorted in the same order as the nodes are ordered // in the BeliefNetwork) for (int node_idx = 0; node_idx < nodes.length; node_idx++) { int domain_idx; if (clusterers[node_idx] == null) { Discrete domain = (Discrete) nodes[node_idx].getDomain(); String strValue; if (domain instanceof Discretized) { // If we have a discretized domain we discretize first... int colIdx = nodeIdx2colIdx[node_idx]; if (colIdx < 0) { //bn.dump(); /* for (int i = 0; i < numAttributes; i++) { logger.debug("Attribute "+i+": "+instances.attribute(i).name()); } StringBuffer sb = new StringBuffer(); for (int i = 0; i < nodeIdx2colIdx.length; i++) { sb.append(i+"\t"); } sb.append("\n"); for (int i = 0; i < nodeIdx2colIdx.length; i++) { sb.append(nodeIdx2colIdx[i]+"\t"); } logger.debug(sb); */ throw new Exception( "No attribute specified for " + bn.bn.getNodes()[node_idx].getName()); } double value = instance.value(colIdx); strValue = (((Discretized) domain).getNameFromContinuous(value)); /*if (domain.findName(strValue) == -1) { logger.debug(domain); logger.debug(strValue); }*/ } else { int colIdx = nodeIdx2colIdx[node_idx]; if (colIdx < 0) { throw new Exception( "No attribute specified for " + bn.bn.getNodes()[node_idx].getName()); } strValue = instance.stringValue(nodeIdx2colIdx[node_idx]); } domain_idx = domain.findName(strValue); if (domain_idx == -1) { /*String[] myDomain = bn.getDiscreteDomainAsArray(bn.bn.getNodes()[node_idx].getName()); for (int i=0; i<myDomain.length; i++) { logger.debug(myDomain[i]); }*/ throw new Exception(strValue + " not found in domain of " + nodes[node_idx].getName()); } } else { Instance inst = new Instance(1); inst.setValue(0, instance.value(nodeIdx2colIdx[node_idx])); domain_idx = clusterers[node_idx].clusterInstance(inst); } domainIndices[node_idx] = domain_idx; } // - update each node's CPT for (int i = 0; i < nodes.length; i++) { counters[i].count(domainIndices); } } }
From source file:probcog.bayesnets.learning.CPTLearner.java
License:Open Source License
/** * learns an example from a Map<String,String>. * This is the only learning method without using {@link BeliefNetworkEx#getAttributeNameForNode(String)}. * @param data a Map containing the data for one example. The names of all the random * variables (nodes) in the network must be found in the set of keys of the * hash map. //from w w w . j av a 2 s . c om * @throws Exception if required keys are missing from the HashMap */ public void learn(Map<String, String> data) throws Exception { if (!initialized) init(); // - get the indices into the domains of each node // that correspond to the current row of data // (sorted in the same order as the nodes are ordered // in the BeliefNetwork) BeliefNode[] nodes = bn.bn.getNodes(); int[] domainIndices = new int[nodes.length]; for (int node_idx = 0; node_idx < nodes.length; node_idx++) { int domain_idx; String value = data.get(nodes[node_idx].getName()); if (value == null) throw new Exception("Key " + nodes[node_idx].getName() + " not found in data!"); if (clusterers[node_idx] == null) { Discrete domain = (Discrete) nodes[node_idx].getDomain(); domain_idx = domain.findName(value); if (domain_idx == -1) throw new Exception(value + " not found in domain of " + nodes[node_idx].getName()); } else { Instance inst = new Instance(1); inst.setValue(0, Double.parseDouble(value)); domain_idx = clusterers[node_idx].clusterInstance(inst); } domainIndices[node_idx] = domain_idx; } // - update each node's CPT for (int i = 0; i < nodes.length; i++) { counters[i].count(domainIndices); } }
From source file:probcog.bayesnets.learning.DomainLearner.java
License:Open Source License
/** * learns all the examples in the result set. Each row in the result set * represents one example. All the random variables (nodes) that have been * scheduled for learning in the constructor need to be found in each result * row as columns that are named accordingly, i.e. for each random variable * for which the domain is to be learnt, there must be a column with a * matching name in the result set./*from w w w .ja va2s . c o m*/ * * @param rs * the result set * @throws Exception * if the result set is empty * @throws SQLException * particularly if there is no matching column for one of the * node names */ public void learn(ResultSet rs) throws Exception, SQLException { // if it's an empty result set, throw exception if (!rs.next()) throw new Exception("empty result set!"); // gather domain data int numDirectDomains = directDomains != null ? directDomains.length : 0; int numClusteredDomains = clusteredDomains != null ? clusteredDomains.length : 0; do { // for direct learning, add outcomes to the set of outcomes for (int i = 0; i < numDirectDomains; i++) { directDomainData.get(i).add(rs.getString(directDomains[i].getName())); } // for clustering, gather all instances for (int i = 0; i < numClusteredDomains; i++) { Instance inst = new Instance(1); inst.setValue(attrValue, rs.getDouble(clusteredDomains[i].nodeName)); clusterData[i].add(inst); } } while (rs.next()); }
From source file:probcog.bayesnets.learning.DomainLearner.java
License:Open Source License
/** * learns all the examples in the result set. Each row in the result set * represents one example. All the random variables (nodes) that have been * scheduled for learning in the constructor need to be found in each result * row as columns that are named accordingly, i.e. for each random variable * for which the domain is to be learnt, there must be a column with a * matching name in the result set.//from w w w . j a v a 2 s.c o m * * @param rs * the result set * @throws Exception * if the result set is empty * @throws SQLException * particularly if there is no matching column for one of the * node names */ public void learn(Instances instances) throws Exception, SQLException { // if it's an empty result set, throw exception if (instances.numInstances() == 0) throw new Exception("empty result set!"); // gather domain data int numDirectDomains = directDomains != null ? directDomains.length : 0; int numClusteredDomains = clusteredDomains != null ? clusteredDomains.length : 0; @SuppressWarnings("unchecked") Enumeration<Instance> instanceEnum = instances.enumerateInstances(); while (instanceEnum.hasMoreElements()) { Instance instance = instanceEnum.nextElement(); // for direct learning, add outcomes to the set of outcomes for (int i = 0; i < numDirectDomains; i++) { directDomainData.get(i).add(instance.stringValue(instances.attribute(directDomains[i].getName()))); } // for clustering, gather all instances for (int i = 0; i < numClusteredDomains; i++) { Instance inst = new Instance(1); inst.setValue(attrValue, instance.value(instances.attribute(clusteredDomains[i].nodeName))); clusterData[i].add(inst); } } }
From source file:probcog.bayesnets.learning.DomainLearner.java
License:Open Source License
/** * learns an example from a HashMap<String,String>. * //from w w w .jav a2s . c o m * @param data * a HashMap containing the data for one example. The names of * the random variables (nodes) for which the domains are to be * learnt must be found in the set of keys of the hash map. * @throws Exception * if required keys are missing from the HashMap */ public void learn(Map<String, String> data) throws Exception { int numDirectDomains = directDomains != null ? directDomains.length : 0; int numClusteredDomains = clusteredDomains != null ? clusteredDomains.length : 0; // for direct learning, add outcomes to the set of outcomes for (int i = 0; i < numDirectDomains; i++) { String val = data.get(directDomains[i]); if (val == null) throw new Exception("Key " + clusteredDomains[i].nodeName + " not found in data!"); directDomainData.get(i).add(val); } // for clustering, gather all instances for (int i = 0; i < numClusteredDomains; i++) { Instance inst = new Instance(1); String val = data.get(clusteredDomains[i].nodeName); if (val == null) { throw new Exception("Key " + clusteredDomains[i].nodeName + " not found in data!"); } inst.setValue(attrValue, Double.parseDouble(val)); clusterData[i].add(inst); } }
From source file:probcog.clustering.BasicClusterer.java
License:Open Source License
public void addInstance(double value) { Instance inst = new Instance(1); inst.setValue(attrValue, value); instances.add(inst);//from w w w. j a v a 2 s . co m }
From source file:probcog.clustering.BasicClusterer.java
License:Open Source License
public int classify(double value) throws Exception { Instance inst = new Instance(1); inst.setValue(attrValue, value); return clusterer.clusterInstance(inst); }
From source file:probcog.J48Reader.java
License:Open Source License
public static Instances readDB(String dbname) throws IOException, ClassNotFoundException, DDException, FileNotFoundException, Exception { Database db = Database.fromFile(new FileInputStream(dbname)); probcog.srldb.datadict.DataDictionary dd = db.getDataDictionary(); //the vector of attributes FastVector fvAttribs = new FastVector(); HashMap<String, Attribute> mapAttrs = new HashMap<String, Attribute>(); for (DDAttribute attribute : dd.getObject("object").getAttributes().values()) { if (attribute.isDiscarded() && !attribute.getName().equals("objectT")) { continue; }//from ww w .j a v a 2 s .com FastVector attValues = new FastVector(); Domain dom = attribute.getDomain(); for (String s : dom.getValues()) attValues.addElement(s); Attribute attr = new Attribute(attribute.getName(), attValues); fvAttribs.addElement(attr); mapAttrs.put(attribute.getName(), attr); } Instances instances = new Instances("name", fvAttribs, 10000); instances.setClass(mapAttrs.get("objectT")); //for each object add an instance for (Object o : db.getObjects()) { if (o.hasAttribute("objectT")) { Instance instance = new Instance(fvAttribs.size()); for (Entry<String, String> e : o.getAttributes().entrySet()) { if (!dd.getAttribute(e.getKey()).isDiscarded()) { instance.setValue(mapAttrs.get(e.getKey()), e.getValue()); } } instances.add(instance); } } return instances; }