List of usage examples for weka.core Instances relationName
publicString relationName()
From source file:org.dynamicfactory.property.InstancesFactory.java
License:Open Source License
@Override public String exportToString(Instances type, Graph g) { StringBuffer ret = new StringBuffer(); ret.append(type.relationName()); Enumeration attributes = type.enumerateAttributes(); while (attributes.hasMoreElements()) { ret.append(","); outputAttribute(ret, (Attribute) attributes.nextElement()); }/* w ww . j a va 2 s. co m*/ return ret.toString(); }
From source file:org.mcennis.graphrat.property.database.InstancesDB.java
License:Open Source License
public int put(Instances object) { int ret = -1; ResultSet rs = null;//from ww w. ja v a 2 s. co m ResultSet rs2 = null; try { put.clearParameters(); getID.clearParameters(); put.setString(1, object.relationName()); getID.setString(1, object.relationName()); put.executeUpdate(); rs = getID.executeQuery(); if (rs.next()) { ret = rs.getInt("id"); for (int i = 0; i < object.numAttributes(); ++i) { getAttributeID.clearParameters(); getAttributeID.setString(1, object.attribute(i).name()); attributeFactory.put(object.attribute(i)); rs2 = getAttributeID.executeQuery(); if (rs2.next()) { putAttribute.clearParameters(); putAttribute.setInt(1, ret); putAttribute.setInt(2, i); putAttribute.setInt(3, rs2.getInt("id")); putAttribute.executeUpdate(); } } } } catch (SQLException ex) { Logger.getLogger(URLDB.class.getName()).log(Level.SEVERE, null, ex); } finally { if (rs != null) { try { rs.close(); } catch (SQLException ex) { } rs = null; } if (rs2 != null) { try { rs2.close(); } catch (SQLException ex) { } rs2 = null; } } return ret; }
From source file:org.openml.webapplication.io.Output.java
License:Open Source License
public static void instanes2file(Instances instances, Writer out, String[] leadingComments) throws IOException { BufferedWriter bw = new BufferedWriter(out); if (leadingComments != null) { for (int i = 0; i < leadingComments.length; ++i) { bw.write("% " + leadingComments[i] + "\n"); }/*from w ww . j a v a 2s . c om*/ } // Important: We can not use a std Instances.toString() approach, as instance files can grow bw.write("@relation " + instances.relationName() + "\n\n"); for (int i = 0; i < instances.numAttributes(); ++i) { bw.write(instances.attribute(i) + "\n"); } bw.write("\n@data\n"); for (int i = 0; i < instances.numInstances(); ++i) { if (i + 1 == instances.numInstances()) { bw.write(instances.instance(i) + ""); // fix for last instance } else { bw.write(instances.instance(i) + "\n"); } } bw.close(); }
From source file:org.opentox.jaqpot3.qsar.InstancesUtil.java
License:Open Source License
/** * Accepts /* ww w . j av a2s . c o m*/ * @param features * @param data * @param compoundURIposition * Position where the compound URI should be placed. If set to <code>-1</code> * the compound URI will not be included in the created dataset. * @return * A subset of the provided dataset (parameter data in this method) with the * features specified in the provided list with that exact order. The compound * URI feature (string) is placed in the position specified by the parameter * compoundURIposition. * @throws JaqpotException * A JaqpotException is thrown with error code {@link ErrorCause#FeatureNotInDataset FeatureNotInDataset} * in case you provide a feature that is not found in the sumbitted Instances. */ public static Instances sortByFeatureAttrList(List<String> features, final Instances data, int compoundURIposition) throws JaqpotException { int position = compoundURIposition > features.size() ? features.size() : compoundURIposition; if (compoundURIposition != -1) { features.add(position, "compound_uri"); } FastVector vector = new FastVector(features.size()); for (int i = 0; i < features.size(); i++) { String feature = features.get(i); Attribute attribute = data.attribute(feature); if (attribute == null) { throw new JaqpotException("The Dataset you provided does not contain feature:" + feature); } vector.addElement(attribute.copy()); } Instances result = new Instances(data.relationName(), vector, 0); Enumeration instances = data.enumerateInstances(); while (instances.hasMoreElements()) { Instance instance = (Instance) instances.nextElement(); double[] vals = new double[features.size()]; for (int i = 0; i < features.size(); i++) { vals[i] = instance.value(data.attribute(result.attribute(i).name())); } Instance in = new Instance(1.0, vals); result.add(in); } return result; }
From source file:org.opentox.jaqpot3.qsar.InstancesUtil.java
License:Open Source License
public static Instances sortForPMMLModel(List<Feature> list, List<Integer> trFieldsAttrIndex, final Instances data, int compoundURIposition) throws JaqpotException { List<String> features = new ArrayList<String>(); for (Feature feature : list) { features.add(feature.getUri().toString()); }//from w w w .ja v a2 s . c om int position = compoundURIposition > features.size() ? features.size() : compoundURIposition; if (compoundURIposition != -1) { features.add(position, "compound_uri"); } FastVector vector = new FastVector(features.size()); for (int i = 0; i < features.size(); i++) { String feature = features.get(i); Attribute attribute = data.attribute(feature); if (attribute == null) { throw new JaqpotException("The Dataset you provided does not contain feature:" + feature); } vector.addElement(attribute.copy()); } int attributeSize = features.size(); if (trFieldsAttrIndex.size() > 0) { for (int i = 0; i < trFieldsAttrIndex.size(); i++) { Attribute attribute = data.attribute(trFieldsAttrIndex.get(i)); if (attribute == null) { throw new JaqpotException("The Dataset you provided does not contain this pmml feature"); } vector.addElement(attribute.copy()); } attributeSize += trFieldsAttrIndex.size(); } Instances result = new Instances(data.relationName(), vector, 0); Enumeration instances = data.enumerateInstances(); while (instances.hasMoreElements()) { Instance instance = (Instance) instances.nextElement(); double[] vals = new double[attributeSize]; for (int i = 0; i < attributeSize; i++) { vals[i] = instance.value(data.attribute(result.attribute(i).name())); } Instance in = new Instance(1.0, vals); result.add(in); } return result; }
From source file:org.opentox.toxotis.factory.DatasetFactory.java
License:Open Source License
/** * Create a dataset using a <code>weka.core.Instances</code> object (based on * Weka, version 3.6.2). Since datasets structurally differ from Instances * object for they store the information in a more expanded way including meta * data and nodes that do not appear in Instances object (or ARFF files), the * provided object has to possess a certain structure: The first attribute of * it has to be always named <code>compound_uri</code> and be of type <code>string</code>. * This attribute stores the URIs of the compounds of the dataset. Second, the rest * attributes have to be of type <code>string</code> or <code>numeric</code> or * <code>nominal</code> and their name should be an acceptable feature URI (for * example <code>http://someserver.com:1234/opentox/feature/54234</code>). * * @param instances//from www .ja va2 s . c o m * Instances object to be converted into a Dataset. * @return * The dataset that is created from the provided Instances object. * @throws ToxOtisException * In case the conversion is not possible due to structural inconsistencies * of the provided Instances object. */ public Dataset createFromArff(Instances instances) throws ToxOtisException { if (instances.attribute("compound_uri") == null && instances.attribute("URI") == null) { throw new ToxOtisException("Cannot create an OpenTox dataset out of this dataset because " + "'compound_uri' was not found in it's attribute list"); } Dataset ds = new Dataset(); Enumeration instancesEnum = instances.enumerateInstances(); while (instancesEnum.hasMoreElements()) { Instance instance = (Instance) instancesEnum.nextElement(); ds.getDataEntries().add(createDataEntry(instance)); } try { ds.setUri(new VRI(instances.relationName())); } catch (URISyntaxException ex) { throw new ToxOtisException( "The relation name '" + instances.relationName() + "' is not" + "a valid dataset URI!", ex); } return ds; }
From source file:outlier.INNE.java
License:Open Source License
public INNE(int numSub, int numSet, Instances instances) { this.numSet = numSet; this.instances = instances; this.dataFileName = instances.relationName(); numAttributes = instances.numAttributes(); numInstances = instances.numInstances(); this.numSub = Math.min(numSub, numInstances); random = new Random(); ensemble = new ArrayList<NNSet>(); }
From source file:script.FileSaver.java
public String buildFile(Instances data) { String result = "@relation " + data.relationName() + newLine + newLine; sb = new StringBuilder(result); appendAttributes(data);/*from w w w . j a v a2 s .c om*/ appendRecords(data); return sb.toString(); }
From source file:sg.edu.nus.comp.nlp.ims.classifiers.CMultiClassesSVM.java
License:Open Source License
/** * get output format//from ww w. ja v a2 s . co m * * @param p_Instances * input format */ protected void getOutputFormat(Instances p_Instances) { FastVector newAtts, newVals; // Compute new attributes newAtts = new FastVector(p_Instances.numAttributes()); for (int j = 0; j < p_Instances.numAttributes(); j++) { Attribute att = p_Instances.attribute(j); if (j != p_Instances.classIndex()) { newAtts.addElement(att.copy()); } else { if (p_Instances.classAttribute().isNumeric()) { newAtts.addElement(new Attribute(att.name())); } else { newVals = new FastVector(2); newVals.addElement("negative"); newVals.addElement("positive"); newAtts.addElement(new Attribute(att.name(), newVals)); } } } // Construct new header this.m_OutputFormat = new Instances(p_Instances.relationName(), newAtts, 0); this.m_OutputFormat.setClassIndex(p_Instances.classIndex()); if (this.m_IndexOfID >= 0) { this.m_OutputFormat.deleteAttributeAt(this.m_IndexOfID); } }
From source file:tr.gov.ulakbim.jDenetX.experiments.wrappers.EvalActiveBoostingID.java
License:Open Source License
public static Instances clusterInstances(Instances data) { XMeans xmeans = new XMeans(); Remove filter = new Remove(); Instances dataClusterer = null;//from w w w. jav a 2 s .com if (data == null) { throw new NullPointerException("Data is null at clusteredInstances method"); } //Get the attributes from the data for creating the sampled_data object ArrayList<Attribute> attrList = new ArrayList<Attribute>(); Enumeration attributes = data.enumerateAttributes(); while (attributes.hasMoreElements()) { attrList.add((Attribute) attributes.nextElement()); } Instances sampled_data = new Instances(data.relationName(), attrList, 0); data.setClassIndex(data.numAttributes() - 1); sampled_data.setClassIndex(data.numAttributes() - 1); filter.setAttributeIndices("" + (data.classIndex() + 1)); data.remove(0);//In Wavelet Stream of MOA always the first element comes without class try { filter.setInputFormat(data); dataClusterer = Filter.useFilter(data, filter); String[] options = new String[4]; options[0] = "-L"; // max. iterations options[1] = Integer.toString(noOfClassesInPool - 1); if (noOfClassesInPool > 2) { options[1] = Integer.toString(noOfClassesInPool - 1); xmeans.setMinNumClusters(noOfClassesInPool - 1); } else { options[1] = Integer.toString(noOfClassesInPool); xmeans.setMinNumClusters(noOfClassesInPool); } xmeans.setMaxNumClusters(data.numClasses() + 1); System.out.println("No of classes in the pool: " + noOfClassesInPool); xmeans.setUseKDTree(true); //xmeans.setOptions(options); xmeans.buildClusterer(dataClusterer); System.out.println("Xmeans\n:" + xmeans); } catch (Exception e) { e.printStackTrace(); } //System.out.println("Assignments\n: " + assignments); ClusterEvaluation eval = new ClusterEvaluation(); eval.setClusterer(xmeans); try { eval.evaluateClusterer(data); int classesToClustersMap[] = eval.getClassesToClusters(); //check the classes to cluster map int clusterNo = 0; for (int i = 0; i < data.size(); i++) { clusterNo = xmeans.clusterInstance(dataClusterer.get(i)); //Check if the class value of instance and class value of cluster matches if ((int) data.get(i).classValue() == classesToClustersMap[clusterNo]) { sampled_data.add(data.get(i)); } } } catch (Exception e) { e.printStackTrace(); } return ((Instances) sampled_data); }