weka.core.WekaException Java Examples
The following examples show how to use
weka.core.WekaException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Dl4jMlpClassifier.java From wekaDeeplearning4j with GNU General Public License v3.0 | 6 votes |
/** * Validate a data split of train and validation data * * @param trainData Training data * @param valData Validation data * @throws WekaException Invalid validation split */ protected void validateSplit(Instances trainData, Instances valData) throws WekaException { if (earlyStopping.getValidationSetPercentage() < 10e-8) { // Use no validation set at all return; } int classIndex = trainData.classIndex(); int valDataNumDinstinctClassValues = valData.numDistinctValues(classIndex); int trainDataNumDistinctClassValues = trainData.numDistinctValues(classIndex); if (trainData.numClasses() > 1 && valDataNumDinstinctClassValues != trainDataNumDistinctClassValues) { throw new InvalidValidationPercentageException( "The validation data did not contain the same classes as the training data. " + "You should increase the validation percentage in the EarlyStopping configuration."); } }
Example #2
Source File: Dl4jMlpClassifier.java From wekaDeeplearning4j with GNU General Public License v3.0 | 6 votes |
/** * Validate a data split of train and validation data * * @param trainData Training data * @param valData Validation data * @throws WekaException Invalid validation split */ protected void validateSplit(Instances trainData, Instances valData) throws WekaException { if (earlyStopping.getValidationSetPercentage() < 10e-8) { // Use no validation set at all return; } int classIndex = trainData.classIndex(); int valDataNumDinstinctClassValues = valData.numDistinctValues(classIndex); int trainDataNumDistinctClassValues = trainData.numDistinctValues(classIndex); if (trainData.numClasses() > 1 && valDataNumDinstinctClassValues != trainDataNumDistinctClassValues) { throw new InvalidValidationPercentageException( "The validation data did not contain the same classes as the training data. " + "You should increase the validation percentage in the EarlyStopping configuration."); } }
Example #3
Source File: AbstractOutput.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Prints the classification to the buffer. * * @param classifier the classifier to use for printing the classification * @param inst the instance to print * @param index the index of the instance * @throws Exception if check fails or error occurs during printing of * classification */ public void printClassification(Classifier classifier, Instance inst, int index) throws Exception { String error; if ((error = checkBasic()) != null) throw new WekaException(error); doPrintClassification(classifier, inst, index); }
Example #4
Source File: AbstractOutput.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Prints the classification to the buffer. * * @param dist the distribution from classifier for the supplied instance * @param inst the instance to print * @param index the index of the instance * @throws Exception if check fails or error occurs during printing of * classification */ public void printClassification(double[] dist, Instance inst, int index) throws Exception { String error; if ((error = checkBasic()) != null) throw new WekaException(error); doPrintClassification(dist, inst, index); }
Example #5
Source File: MekaClassAttributes.java From meka with GNU General Public License v3.0 | 5 votes |
/** * Determines the output format based on the input format and returns * this. In case the output format cannot be returned immediately, i.e., * hasImmediateOutputFormat() returns false, then this method will called * from batchFinished() after the call of preprocess(Instances), in which, * e.g., statistics for the actual processing step can be gathered. * * @param inputFormat the input format to base the output format on * @return the output format * @throws Exception in case the determination goes wrong */ protected Instances determineOutputFormat(Instances inputFormat) throws Exception { int i; int[] indices; StringBuilder order; Instances output; m_AttributeIndices.setUpper(inputFormat.numAttributes() - 1); order = new StringBuilder(); indices = m_AttributeIndices.getSelection(); if (indices.length == 0) throw new WekaException("No attributes defined as class attributes!"); for (i = 0; i < indices.length; i++) { if (i > 0) order.append(","); order.append("" + (indices[i]+1)); } for (i = 0; i < inputFormat.numAttributes(); i++) { if (m_AttributeIndices.isInRange(i)) continue; order.append(","); order.append("" + (i+1)); } m_Reorder.setAttributeIndices(order.toString()); m_Reorder.setInputFormat(inputFormat); output = m_Reorder.getOutputFormat(); output.setClassIndex(indices.length); output.setRelationName("-C " + indices.length); return output; }
Example #6
Source File: OneR.java From tsml with GNU General Public License v3.0 | 4 votes |
/** * Generates the classifier. * * @param instances the instances to be used for building the classifier * @throws Exception if the classifier can't be built successfully */ public void buildClassifier(Instances instances) throws Exception { boolean noRule = true; // can classifier handle the data? getCapabilities().testWithFail(instances); // remove instances with missing class Instances data = new Instances(instances); data.deleteWithMissingClass(); // only class? -> build ZeroR model if (data.numAttributes() == 1) { System.err.println( "Cannot build model (only class attribute present in data!), " + "using ZeroR model instead!"); m_ZeroR = new weka.classifiers.rules.ZeroR(); m_ZeroR.buildClassifier(data); return; } else { m_ZeroR = null; } // for each attribute ... Enumeration enu = instances.enumerateAttributes(); while (enu.hasMoreElements()) { try { OneRRule r = newRule((Attribute) enu.nextElement(), data); // if this attribute is the best so far, replace the rule if (noRule || r.m_correct > m_rule.m_correct) { m_rule = r; } noRule = false; } catch (Exception ex) { } } if (noRule) throw new WekaException("No attributes found to work with!"); }
Example #7
Source File: LibLINEAR.java From tsml with GNU General Public License v3.0 | 4 votes |
/** * Computes the distribution for a given instance. * * @param instance the instance for which distribution is computed * @return the distribution * @throws Exception if the distribution can't be computed successfully */ public double[] distributionForInstance (Instance instance) throws Exception { if (!getDoNotReplaceMissingValues()) { m_ReplaceMissingValues.input(instance); m_ReplaceMissingValues.batchFinished(); instance = m_ReplaceMissingValues.output(); } if (getConvertNominalToBinary() && m_NominalToBinary != null) { m_NominalToBinary.input(instance); m_NominalToBinary.batchFinished(); instance = m_NominalToBinary.output(); } if (m_Filter != null) { m_Filter.input(instance); m_Filter.batchFinished(); instance = m_Filter.output(); } Object x = instanceToArray(instance); double v; double[] result = new double[instance.numClasses()]; if (m_ProbabilityEstimates) { if (m_SVMType != SVMTYPE_L2_LR) { throw new WekaException("probability estimation is currently only " + "supported for L2-regularized logistic regression"); } int[] labels = (int[])invokeMethod(m_Model, "getLabels", null, null); double[] prob_estimates = new double[instance.numClasses()]; v = ((Integer) invokeMethod( Class.forName(CLASS_LINEAR).newInstance(), "predictProbability", new Class[]{ Class.forName(CLASS_MODEL), Array.newInstance(Class.forName(CLASS_FEATURENODE), Array.getLength(x)).getClass(), Array.newInstance(Double.TYPE, prob_estimates.length).getClass()}, new Object[]{ m_Model, x, prob_estimates})).doubleValue(); // Return order of probabilities to canonical weka attribute order for (int k = 0; k < prob_estimates.length; k++) { result[labels[k]] = prob_estimates[k]; } } else { v = ((Integer) invokeMethod( Class.forName(CLASS_LINEAR).newInstance(), "predict", new Class[]{ Class.forName(CLASS_MODEL), Array.newInstance(Class.forName(CLASS_FEATURENODE), Array.getLength(x)).getClass()}, new Object[]{ m_Model, x})).doubleValue(); assert (instance.classAttribute().isNominal()); result[(int) v] = 1; } return result; }
Example #8
Source File: PaceRegression.java From tsml with GNU General Public License v3.0 | 4 votes |
/** * Parses a given list of options. <p/> * <!-- options-start --> * Valid options are: <p/> * * <pre> -D * Produce debugging output. * (default no debugging output)</pre> * * <pre> -E <estimator> * The estimator can be one of the following: * eb -- Empirical Bayes estimator for noraml mixture (default) * nested -- Optimal nested model selector for normal mixture * subset -- Optimal subset selector for normal mixture * pace2 -- PACE2 for Chi-square mixture * pace4 -- PACE4 for Chi-square mixture * pace6 -- PACE6 for Chi-square mixture * * ols -- Ordinary least squares estimator * aic -- AIC estimator * bic -- BIC estimator * ric -- RIC estimator * olsc -- Ordinary least squares subset selector with a threshold</pre> * * <pre> -S <threshold value> * Threshold value for the OLSC estimator</pre> * <!-- options-end --> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ public void setOptions(String[] options) throws Exception { setDebug(Utils.getFlag('D', options)); String estimator = Utils.getOption('E', options); if ( estimator.equals("ols") ) paceEstimator = olsEstimator; else if ( estimator.equals("olsc") ) paceEstimator = olscEstimator; else if( estimator.equals("eb") || estimator.equals("") ) paceEstimator = ebEstimator; else if ( estimator.equals("nested") ) paceEstimator = nestedEstimator; else if ( estimator.equals("subset") ) paceEstimator = subsetEstimator; else if ( estimator.equals("pace2") ) paceEstimator = pace2Estimator; else if ( estimator.equals("pace4") ) paceEstimator = pace4Estimator; else if ( estimator.equals("pace6") ) paceEstimator = pace6Estimator; else if ( estimator.equals("aic") ) paceEstimator = aicEstimator; else if ( estimator.equals("bic") ) paceEstimator = bicEstimator; else if ( estimator.equals("ric") ) paceEstimator = ricEstimator; else throw new WekaException("unknown estimator " + estimator + " for -E option" ); String string = Utils.getOption('S', options); if( ! string.equals("") ) olscThreshold = Double.parseDouble( string ); }
Example #9
Source File: Utils.java From wekaDeeplearning4j with GNU General Public License v3.0 | 4 votes |
/** * Convert an arbitrary NDArray to Weka instances * * @param ndArray Input array * @param inputFormat Format to use for the instances * @param attributesPerLayer Hashmap of layer names and how many attributes there are per layer * @return Instances object * @throws WekaException Invalid input */ public static Instances ndArrayToInstances(INDArray ndArray, Instances inputFormat, Map<String, Long> attributesPerLayer) throws WekaException { int numInstances = (int) ndArray.size(0); long[] shape = ndArray.shape(); int dims = shape.length; if (dims != 2) { throw new WekaException("Invalid input, NDArray shape needs to be two dimensional " + "but was " + Arrays.toString(shape)); } long numAttributes = shape[1]; int classI = -1; if (inputFormat != null) { classI = (int) (numAttributes - 1); } // Create the new attribute names ArrayList<Attribute> atts = new ArrayList<>(); for (int i = 0; i < numAttributes; i++) { if (i == classI && inputFormat != null) { if (inputFormat.classAttribute().isNominal()) atts.add(copyNominalAttribute(inputFormat.classAttribute())); else atts.add(new Attribute(inputFormat.classAttribute().name())); } else { atts.add(new Attribute(getAttributeName(attributesPerLayer, i))); } } // Actually create the instances from the values in the given NDArray Instances instances = new Instances("Transformed", atts, numInstances); instances.setClassIndex(classI); for (int i = 0; i < numInstances; i++) { INDArray row = ndArray.get(NDArrayIndex.point(i)); double[] instanceVals = row.toDoubleVector(); Instance inst = new DenseInstance(1.0, instanceVals); inst.setDataset(instances); instances.add(inst); } return instances; }
Example #10
Source File: Utils.java From wekaDeeplearning4j with GNU General Public License v3.0 | 4 votes |
/** * Convert an arbitrary NDArray to Weka instances * * @param ndArray Input array * @param inputFormat Format to use for the instances * @param attributesPerLayer Hashmap of layer names and how many attributes there are per layer * @return Instances object * @throws WekaException Invalid input */ public static Instances ndArrayToInstances(INDArray ndArray, Instances inputFormat, Map<String, Long> attributesPerLayer) throws WekaException { int numInstances = (int) ndArray.size(0); long[] shape = ndArray.shape(); int dims = shape.length; if (dims != 2) { throw new WekaException("Invalid input, NDArray shape needs to be two dimensional " + "but was " + Arrays.toString(shape)); } long numAttributes = shape[1]; int classI = -1; if (inputFormat != null) { classI = (int) (numAttributes - 1); } // Create the new attribute names ArrayList<Attribute> atts = new ArrayList<>(); for (int i = 0; i < numAttributes; i++) { if (i == classI && inputFormat != null) { if (inputFormat.classAttribute().isNominal()) atts.add(copyNominalAttribute(inputFormat.classAttribute())); else atts.add(new Attribute(inputFormat.classAttribute().name())); } else { atts.add(new Attribute(getAttributeName(attributesPerLayer, i))); } } // Actually create the instances from the values in the given NDArray Instances instances = new Instances("Transformed", atts, numInstances); instances.setClassIndex(classI); for (int i = 0; i < numInstances; i++) { INDArray row = ndArray.get(NDArrayIndex.point(i)); double[] instanceVals = row.toDoubleVector(); Instance inst = new DenseInstance(1.0, instanceVals); inst.setDataset(instances); instances.add(inst); } return instances; }