List of usage examples for weka.classifiers.functions LinearRegression buildClassifier
@Override public void buildClassifier(Instances data) throws Exception
From source file:org.opentox.qsar.processors.trainers.regression.MLRTrainer.java
License:Open Source License
/** * Trains the MLR model given an Instances object with the training data. The prediction * feature (class attributre) is specified in the constructor of the class. * @param data The training data as <code>weka.core.Instances</code> object. * @return The QSARModel corresponding to the trained model. * @throws QSARException In case the model cannot be trained * <p>/*from ww w . j a v a 2s . com*/ * <table> * <thead> * <tr> * <td><b>Code</b></td><td><b>Explanation</b></td> * </tr> * </thead> * <tbody> * <tr> * <td>XQReg1</td><td>Could not train the an model</td> * </tr> * <tr> * <td>XQReg2</td><td>Could not generate PMML representation for the model</td> * </tr> * <tr> * <td>XQReg202</td><td>The prediction feature you provided is not a valid numeric attribute of the dataset</td> * </tr> * </tbody> * </table> * </p> * @throws NullPointerException * In case the provided training data is null. */ public QSARModel train(Instances data) throws QSARException { // GET A UUID AND DEFINE THE TEMPORARY FILE WHERE THE TRAINING DATA // ARE STORED IN ARFF FORMAT PRIOR TO TRAINING. final String rand = java.util.UUID.randomUUID().toString(); final String temporaryFilePath = ServerFolders.temp + "/" + rand + ".arff"; final File tempFile = new File(temporaryFilePath); // SAVE THE DATA IN THE TEMPORARY FILE try { ArffSaver dataSaver = new ArffSaver(); dataSaver.setInstances(data); dataSaver.setDestination(new FileOutputStream(tempFile)); dataSaver.writeBatch(); } catch (final IOException ex) { tempFile.delete(); throw new RuntimeException( "Unexpected condition while trying to save the " + "dataset in a temporary ARFF file", ex); } LinearRegression linreg = new LinearRegression(); String[] linRegOptions = { "-S", "1", "-C" }; try { linreg.setOptions(linRegOptions); linreg.buildClassifier(data); } catch (final Exception ex) {// illegal options or could not build the classifier! String message = "MLR Model could not be trained"; YaqpLogger.LOG.log(new Trace(getClass(), message + " :: " + ex)); throw new QSARException(Cause.XQReg1, message, ex); } try { generatePMML(linreg, data); } catch (final YaqpIOException ex) { String message = "Could not generate PMML representation for MLR model :: " + ex; throw new QSARException(Cause.XQReg2, message, ex); } // PERFORM THE TRAINING String[] generalOptions = { "-c", Integer.toString(data.classIndex() + 1), "-t", temporaryFilePath, /// Save the model in the following directory "-d", ServerFolders.models_weka + "/" + uuid }; try { Evaluation.evaluateModel(linreg, generalOptions); } catch (final Exception ex) { tempFile.delete(); throw new QSARException(Cause.XQReg350, "Unexpected condition while trying to train " + "an SVM model. Possible explanation : {" + ex.getMessage() + "}", ex); } ArrayList<Feature> independentFeatures = new ArrayList<Feature>(); for (int i = 0; i < data.numAttributes(); i++) { Feature f = new Feature(data.attribute(i).name()); if (data.classIndex() != i) { independentFeatures.add(f); } } Feature dependentFeature = new Feature(data.classAttribute().name()); Feature predictedFeature = dependentFeature; QSARModel model = new QSARModel(uuid.toString(), predictedFeature, dependentFeature, independentFeatures, YaqpAlgorithms.MLR, new User(), null, datasetUri, ModelStatus.UNDER_DEVELOPMENT); model.setParams(new HashMap<String, AlgorithmParameter>()); return model; }