List of usage examples for weka.classifiers.functions MLPRegressor MLPRegressor
MLPRegressor
From source file:src.BestFirst.java
License:Open Source License
/** * Searches the attribute subset space by best first search * * @param data the training instances.//from w w w . j av a 2 s .c o m * @return an array (not necessarily ordered) of selected attribute indexes * @throws Exception if the search can't be completed */ public int[] search(Instances data, TSLagMaker tsLagMaker, List<String> overlayFields) throws Exception { long startTime = System.currentTimeMillis(), stopTime; TSWrapper tsWrapper = new TSWrapper(); tsWrapper.buildEvaluator(data); String m_EvaluationMeasure = "RMSE"; tsWrapper.setM_EvaluationMeasure(m_EvaluationMeasure); System.out.println("Using " + m_EvaluationMeasure + " as a evaluation Measure"); LinearRegression linearRegression = new LinearRegression(); linearRegression.setOptions(weka.core.Utils.splitOptions("-S 1 -R 1E-6")); MLPRegressor mlpRegressor = new MLPRegressor(); mlpRegressor.setOptions(weka.core.Utils.splitOptions("-P 5 -E 5 -N 2")); tsWrapper.setM_BaseClassifier(mlpRegressor); System.out.println("Using best First and MLPReg as classifier."); m_numAttribs = data.numAttributes(); SubsetHandler subsetHandler = new SubsetHandler(); subsetHandler.setM_numAttribs(m_numAttribs); m_totalEvals = 0; int i, j; int best_size = 0; int size = 0; int done; int searchDirection = m_searchDirection; BitSet best_group, temp_group; int stale; double best_merit; double merit; boolean z; boolean added; Double bias = 0.; Hashtable<String, Double> lookForExistingSubsets = new Hashtable<String, Double>(); int insertCount = 0; LinkedList2 prioQueueList = new LinkedList2(m_maxStale); best_merit = -Double.MAX_VALUE; stale = 0; int startSetPercentage = 0; best_group = subsetHandler.getStartSet(startSetPercentage); m_startRange.setUpper(m_numAttribs - 1); if (!(getStartSet().equals(""))) m_starting = m_startRange.getSelection(); // If a starting subset has been supplied, then initialise the bitset if (m_starting != null) { for (i = 0; i < m_starting.length; i++) if ((m_starting[i]) != m_classIndex) best_group.set(m_starting[i]); best_size = m_starting.length; m_totalEvals++; } else { if (m_searchDirection == SELECTION_BACKWARD) { //setStartSet("1-last"); //m_starting = new int[m_numAttribs]; // init initial subset to all attributes for (i = 11, j = 0; i < m_numAttribs; i++) { if (i != m_classIndex) { best_group.set(i); //m_starting[j++] = i; } } best_size = m_numAttribs - 1; m_totalEvals++; } } // evaluate the initial subset best_merit = -tsWrapper.evaluateSubset(best_group, tsLagMaker, overlayFields, false); //printGroup(best_group, m_numAttribs); System.out.println("Merit:" + best_merit); System.out.print("Group: "); subsetHandler.printGroup(best_group); System.out.println("\n"); m_totalEvals++; // add the initial group to the list and the hash table Object[] best = new Object[1]; best[0] = best_group.clone(); prioQueueList.addToList(best, best_merit); String hashedGroup = best_group.toString(); lookForExistingSubsets.put(hashedGroup, new Double(best_merit)); System.out.println("StartsetPercentage:" + startSetPercentage + ", maxStale:" + m_maxStale); while (stale < m_maxStale) { added = false; if (m_searchDirection == SELECTION_BIDIRECTIONAL) { // bi-directional search done = 2; searchDirection = SELECTION_FORWARD; } else { done = 1; } // finished search? if (prioQueueList.size() == 0) { stale = m_maxStale; break; } // copy the attribute set at the head of the list temp_group = (BitSet) (prioQueueList.getLinkAt(0).getData()[0]); temp_group = (BitSet) temp_group.clone(); // remove the head of the list prioQueueList.removeLinkAt(0); // count the number of bits set (attributes) int kk; for (kk = 0, size = 0; kk < m_numAttribs; kk++) if (temp_group.get(kk)) size++; do { for (i = 11; i < m_numAttribs - 2; i++) { //setting it to 11 to skip overlay fields, time stamps etc. if (searchDirection == SELECTION_FORWARD) z = ((i != m_classIndex) && (!temp_group.get(i))); else z = ((i != m_classIndex) && (temp_group.get(i))); if (z) { // set the bit (attribute to add/delete) if (searchDirection == SELECTION_FORWARD) { temp_group.set(i); size++; } else { temp_group.clear(i); size--; } /* * if this subset has been seen before, then it is already in the * list (or has been fully expanded) */ hashedGroup = temp_group.toString(); if (lookForExistingSubsets.containsKey(hashedGroup) == false) { //System.out.println("Before eval:" + temp_group); merit = -tsWrapper.evaluateSubset(temp_group, tsLagMaker, overlayFields, false); System.out.println("Merit: " + merit); System.out.print("Group: "); subsetHandler.printGroup(temp_group); System.out.println("\n"); m_totalEvals++; hashedGroup = temp_group.toString(); lookForExistingSubsets.put(hashedGroup, new Double(merit)); insertCount++; // insert this one in the list } else merit = lookForExistingSubsets.get(hashedGroup).doubleValue(); Object[] add = new Object[1]; add[0] = temp_group.clone(); prioQueueList.addToList(add, merit); if (m_debug) { System.out.print("Group: "); subsetHandler.printGroup(temp_group); System.out.println("Merit: " + merit); } // is this better than the best? if (searchDirection == SELECTION_FORWARD) { z = (merit - best_merit) > 0.01; //they are both negative numbers; actually we are looking for the smallest error } else { if (merit == best_merit) { z = (size < best_size); } else { z = (merit > best_merit); } } if (z) { added = true; stale = 0; System.out.println("Setting best merit to:" + merit); best_merit = merit; // best_size = (size + best_size); best_size = size; best_group = (BitSet) (temp_group.clone()); } // unset this addition(deletion) if (searchDirection == SELECTION_FORWARD) { temp_group.clear(i); size--; } else { temp_group.set(i); size++; } } } if (done == 2) searchDirection = SELECTION_BACKWARD; done--; } while (done > 0); /* if we haven't added a new attribute subset then full expansion of this * node hasen't resulted in anything better */ if (!added) { stale++; System.out.println("Stale:" + stale); } } subsetHandler.printGroup(best_group); System.out.println("Best merit: " + best_merit); System.out.println(m_totalEvals); stopTime = System.currentTimeMillis(); System.out.println("Time taken for wrapper part:" + ((double) stopTime - startTime) / 1000); m_bestMerit = best_merit; subsetHandler.includesMoreThanXPercentOfFeatures(best_group, true, 0); tsWrapper.evaluateSubset(best_group, tsLagMaker, overlayFields, true); return attributeList(best_group); }
From source file:src.RandomSearch.java
License:Open Source License
/** * Searches the attribute subset space by best first search * * @param data the training instances.//from w w w . jav a2 s.co m * @return an array (not necessarily ordered) of selected attribute indexes * @throws Exception if the search can't be completed */ public int[] search(Instances data, TSLagMaker tsLagMaker, List<String> overlayFields) throws Exception { long startTime = System.currentTimeMillis(), stopTime; m_totalEvals = 0; int m_maxEvals = 400; TSWrapper tsWrapper = new TSWrapper(); tsWrapper.buildEvaluator(data); String m_EvaluationMeasure = "RMSE"; tsWrapper.setM_EvaluationMeasure(m_EvaluationMeasure); System.out.println("Using " + m_EvaluationMeasure + " as a evaluation Measure"); /*LinearRegression linearRegression = new LinearRegression(); tsWrapper.setM_BaseClassifier(linearRegression);*/ MLPRegressor mlpRegressor = new MLPRegressor(); mlpRegressor.setOptions(weka.core.Utils.splitOptions("-P 4 -E 4 -N 2")); tsWrapper.setM_BaseClassifier(mlpRegressor); System.out.println("Using RA and MLPReg as classifier."); m_numAttribs = data.numAttributes(); SubsetHandler subsetHandler = new SubsetHandler(); subsetHandler.setM_numAttribs(m_numAttribs); BitSet best_group; best_group = subsetHandler.getStartSet(1); double best_merit; Hashtable<String, Double> lookForExistingSubsets = new Hashtable<String, Double>(); // evaluate the initial subset subsetHandler.printGroup(best_group); //mode = 0 for wrapper, 1 for returning bias and 2 for testing best model best_merit = tsWrapper.evaluateSubset(best_group, tsLagMaker, overlayFields, false); m_totalEvals++; String subset_string = best_group.toString(); lookForExistingSubsets.put(subset_string, best_merit); System.out.println("Initial group with numAttribs: " + m_numAttribs + "/n"); System.out.println("Merit: " + best_merit); /*RAContainer raContainer = new RAContainer(m_totalEvals, best_group, lookForExistingSubsets, subsetHandler, best_merit, tsLagMaker, overlayFields); //Threading ArrayList<Thread> threadList = new ArrayList<Thread>(); threadNumber = 1; int threadLagInterval = m_maxEvals/threadNumber; for (int i = 0; i < threadNumber; i++) threadList.add(i, new RAThread("Thread " + i, raContainer, m_maxEvals, subsetHandler, linearRegression, data)); for (int i = 0; i < threadList.size(); i++) threadList.get(i).start(); for (int i = 0; i < threadList.size(); i++) threadList.get(i).join();*/ while (m_totalEvals < m_maxEvals) { BitSet s_new = subsetHandler.changeBits((BitSet) best_group.clone(), 1); subset_string = s_new.toString(); if (!lookForExistingSubsets.containsKey(subset_string)) { double s_new_merit = tsWrapper.evaluateSubset(s_new, tsLagMaker, overlayFields, false); m_totalEvals++; System.out.println("New merit: " + s_new_merit); lookForExistingSubsets.put(subset_string, s_new_merit); if (decisionFunction(best_merit - s_new_merit)) { best_group = (BitSet) s_new.clone(); best_merit = s_new_merit; System.out.println("New best merit!"); } } } System.out.println("Best merit:" + best_merit); System.out.println(m_totalEvals); stopTime = System.currentTimeMillis(); System.out.println("Time taken for wrapper part:" + ((double) stopTime - startTime) / 1000); tsWrapper.evaluateSubset(best_group, tsLagMaker, overlayFields, true); return attributeList(best_group); }
From source file:src.SimmulatedAnnealing.java
License:Open Source License
/** * Searches the attribute subset space by best first search * * @param data the training instances./*from w w w . j a va 2 s .co m*/ * @return an array (not necessarily ordered) of selected attribute indexes * @throws Exception if the search can't be completed */ public int[] search(Instances data, TSLagMaker tsLagMaker, List<String> overlayFields) throws Exception { long startTime = System.currentTimeMillis(), stopTime; m_totalEvals = 0; int m_totalEvals = 0; TSWrapper tsWrapper = new TSWrapper(); tsWrapper.buildEvaluator(data); String m_EvaluationMeasure = "RMSE"; tsWrapper.setM_EvaluationMeasure(m_EvaluationMeasure); System.out.println("Using " + m_EvaluationMeasure + " as a evaluation Measure"); LinearRegression linearRegression = new LinearRegression(); linearRegression.setOptions(weka.core.Utils.splitOptions("-S 1 -R 1E-6")); MLPRegressor mlpRegressor = new MLPRegressor(); mlpRegressor.setOptions(weka.core.Utils.splitOptions("-P 4 -E 4 -N 2")); tsWrapper.setM_BaseClassifier(mlpRegressor); System.out.println("Using SA and MLPRegressor as classifier."); m_numAttribs = data.numAttributes(); SubsetHandler subsetHandler = new SubsetHandler(); subsetHandler.setM_numAttribs(m_numAttribs); BitSet best_group; best_group = subsetHandler.getStartSet(0); double temperature = 0.4, initialTemp = temperature, dropRate = 0.00012, limit = 0.0000001; double best_merit; int changedAltoughWorseCounter = 0; Hashtable<String, Double> lookForExistingSubsets = new Hashtable<String, Double>(); // evaluate the initial subset subsetHandler.printGroup(best_group); best_merit = -tsWrapper.evaluateSubset(best_group, tsLagMaker, overlayFields, false); m_totalEvals++; String subset_string = best_group.toString(); lookForExistingSubsets.put(subset_string, best_merit); System.out.println("Initial group w/ numAttribs: " + m_numAttribs + " temp: " + temperature + " drop rate:" + dropRate + " limit:" + limit); System.out.println("Merit: " + best_merit); TheVeryBest theVeryBest = new TheVeryBest((BitSet) best_group.clone(), best_merit); ArrayList<Boolean> changedAlthoughWorse = new ArrayList<Boolean>(); while (temperature > limit) { changedAltoughWorseCounter = 0; BitSet s_new = subsetHandler.changeBits((BitSet) best_group.clone(), 1); subset_string = s_new.toString(); if (!lookForExistingSubsets.containsKey(subset_string)) { double s_new_merit = -tsWrapper.evaluateSubset(s_new, tsLagMaker, overlayFields, false); m_totalEvals++; System.out.println("New merit: " + s_new_merit); lookForExistingSubsets.put(subset_string, s_new_merit); if (decisionFunction(s_new_merit - best_merit, temperature, best_merit, initialTemp)) { if (best_merit - s_new_merit > 0) //it means this is a worse set than the best set, and we still change the best set to it. changedAlthoughWorse.add(true); best_group = (BitSet) s_new.clone(); best_merit = s_new_merit; } else changedAlthoughWorse.add(false); for (int j = 0; j < changedAlthoughWorse.size(); j++) if (changedAlthoughWorse.get(j)) changedAltoughWorseCounter++; System.out.println("Percentage of worse sets accepted:" + (float) changedAltoughWorseCounter * 100 / changedAlthoughWorse.size() + " Arraylist size:" + changedAlthoughWorse.size() + " changedAlthoughworse counter:" + changedAltoughWorseCounter); if (best_merit > theVeryBest.getMerit()) //we have negative values for the scores, so bigger is better theVeryBest.setNewSet((BitSet) best_group.clone(), best_merit); temperature = temperature / (float) (1 + dropRate * (m_totalEvals - 1)); } } System.out.println("Best merit: " + theVeryBest.getMerit()); System.out.println(m_totalEvals); stopTime = System.currentTimeMillis(); System.out.println("Time taken for wrapper part:" + ((double) stopTime - startTime) / 1000); subsetHandler.printGroup(theVeryBest.getSubset()); subsetHandler.includesMoreThanXPercentOfFeatures(theVeryBest.getSubset(), true, 0); tsWrapper.evaluateSubset(theVeryBest.getSubset(), tsLagMaker, overlayFields, true); return attributeList(theVeryBest.getSubset()); }