📄 lwr.java
字号:
/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * LWR.java * Copyright (C) 1999, 2002 Len Trigg, Eibe Frank * */package weka.classifiers.lazy;import weka.classifiers.functions.LinearRegression;import weka.classifiers.Classifier;import weka.classifiers.Evaluation;import weka.classifiers.functions.LinearRegression;import weka.classifiers.UpdateableClassifier;import java.io.*;import java.util.*;import weka.core.*;/** * Locally-weighted regression. Uses an instance-based algorithm to assign * instance weights which are then used by a linear regression model. For * more information, see<p> * * Atkeson, C., A. Moore, and S. Schaal (1996) <i>Locally weighted * learning</i> * <a href="ftp://ftp.cc.gatech.edu/pub/people/cga/air1.ps.gz">download * postscript</a>. <p> * * Valid options are:<p> * * -D <br> * Produce debugging output. <p> * * -K num <br> * Set the number of neighbours used for setting kernel bandwidth. * (default all) <p> * * -W num <br> * Set the weighting kernel shape to use. 1 = Inverse, 2 = Gaussian. * (default 0 = Linear) <p> * * -S num <br> * Set the attriute selection method to use. 1 = None, 2 = Greedy * (default 0 = M5' method) <p> * * -C <br> * Do not try to eliminate colinear attributes <p> * * -R num <br> * The ridge parameter (default 1.0e-8) <p> * * @author Len Trigg (trigg@cs.waikato.ac.nz) * @version $Revision: 1.1.1.1 $ */public class LWR extends Classifier implements OptionHandler, UpdateableClassifier, WeightedInstancesHandler { /** The training instances used for classification. */ protected Instances m_Train; /** The minimum values for numeric attributes. */ protected double [] m_Min; /** The maximum values for numeric attributes. */ protected double [] m_Max; /** True if debugging output should be printed */ protected boolean m_Debug; /** The number of neighbours used to select the kernel bandwidth */ protected int m_kNN = -1; /** The weighting kernel method currently selected */ protected int m_WeightKernel = LINEAR; /** True if m_kNN should be set to all instances */ protected boolean m_UseAllK = true; /** The available kernel weighting methods */ protected static final int LINEAR = 0; protected static final int INVERSE = 1; protected static final int GAUSS = 2; /** The linear regression object */ protected LinearRegression lr = new LinearRegression(); /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ public Enumeration listOptions() { Vector newVector = new Vector(5); newVector.addElement(new Option("\tProduce debugging output.\n" + "\t(default no debugging output)", "D", 0, "-D")); newVector.addElement(new Option("\tSet the number of neighbors used to set" + " the kernel bandwidth.\n" + "\t(default all)", "K", 1, "-K <number of neighbours>")); newVector.addElement(new Option("\tSet the weighting kernel shape to use." + " 1 = Inverse, 2 = Gaussian.\n" + "\t(default 0 = Linear)", "W", 1,"-W <number of weighting method>")); newVector.addElement(new Option("\tSet the attribute selection method" + " to use. 1 = None, 2 = Greedy.\n" + "\t(default 0 = M5' method)", "S", 1, "-S <number of selection method>")); newVector.addElement(new Option("\tDo not try to eliminate colinear" + " attributes.\n", "C", 0, "-C")); newVector.addElement(new Option("\tSet ridge parameter (default 1.0e-8).\n", "R", 1, "-R <double>")); return newVector.elements(); } /** * Parses a given list of options. Valid options are:<p> * * -D <br> * Produce debugging output. <p> * * -K num <br> * Set the number of neighbours used for setting kernel bandwidth. * (default all) <p> * * -W num <br> * Set the weighting kernel shape to use. 1 = Inverse, 2 = Gaussian. * (default 0 = Linear) <p> * * -S num <br> * Set the attriute selection method to use. 1 = None, 2 = Greedy * (default 0 = M5' method) <p> * * -C <br> * Do not try to eliminate colinear attributes <p> * * -R num <br> * The ridge parameter (default 1.0e-8) <p> * * @param options the list of options as an array of strings * @exception Exception if an option is not supported */ public void setOptions(String[] options) throws Exception { String knnString = Utils.getOption('K', options); if (knnString.length() != 0) { setKNN(Integer.parseInt(knnString)); } else { setKNN(0); } String weightString = Utils.getOption('W', options); if (weightString.length() != 0) { setWeightingKernel(Integer.parseInt(weightString)); } else { setWeightingKernel(LINEAR); } String selectionString = Utils.getOption('S', options); if (selectionString.length() != 0) { setAttributeSelectionMethod(new SelectedTag(Integer .parseInt(selectionString), LinearRegression.TAGS_SELECTION)); } else { setAttributeSelectionMethod(new SelectedTag(LinearRegression.SELECTION_M5, LinearRegression.TAGS_SELECTION)); } setEliminateColinearAttributes(!Utils.getFlag('C', options)); String ridgeString = Utils.getOption('R', options); if (ridgeString.length() != 0) { lr.setRidge(new Double(ridgeString).doubleValue()); } else { lr.setRidge(1.0e-8); } setDebug(Utils.getFlag('D', options)); } /** * Gets the current settings of the classifier. * * @return an array of strings suitable for passing to setOptions */ public String [] getOptions() { String [] options = new String [9]; int current = 0; if (getDebug()) { options[current++] = "-D"; } options[current++] = "-W"; options[current++] = "" + getWeightingKernel(); if (!m_UseAllK) { options[current++] = "-K"; options[current++] = "" + getKNN(); } options[current++] = "-S"; options[current++] = "" + getAttributeSelectionMethod() .getSelectedTag().getID(); if (!getEliminateColinearAttributes()) { options[current++] = "-C"; } options[current++] = "-R"; options[current++] = "" + lr.getRidge(); while (current < options.length) { options[current++] = ""; } return options; } /** * Get the value of EliminateColinearAttributes. * * @return Value of EliminateColinearAttributes. */ public boolean getEliminateColinearAttributes() { return lr.getEliminateColinearAttributes(); } /** * Set the value of EliminateColinearAttributes. * * @param newEliminateColinearAttributes Value to assign to EliminateColinearAttributes. */ public void setEliminateColinearAttributes(boolean newEliminateColinearAttributes) { lr.setEliminateColinearAttributes(newEliminateColinearAttributes); } /** * Sets the method used to select attributes for use in the * linear regression. * * @param method the attribute selection method to use. */ public void setAttributeSelectionMethod(SelectedTag method) { lr.setAttributeSelectionMethod(method); } /** * Gets the method used to select attributes for use in the * linear regression. * * @return the method to use. */ public SelectedTag getAttributeSelectionMethod() { return lr.getAttributeSelectionMethod(); } /** * Sets whether debugging output should be produced * * @param debug true if debugging output should be printed */ public void setDebug(boolean debug) { m_Debug = debug; } /** * SGts whether debugging output should be produced * * @return true if debugging output should be printed */ public boolean getDebug() { return m_Debug; } /** * Sets the number of neighbours used for kernel bandwidth setting. * The bandwidth is taken as the distance to the kth neighbour. * * @param knn the number of neighbours included inside the kernel * bandwidth, or 0 to specify using all neighbors. */ public void setKNN(int knn) { m_kNN = knn; if (knn <= 0) { m_kNN = 0; m_UseAllK = true; } else { m_UseAllK = false; } } /** * Gets the number of neighbours used for kernel bandwidth setting. * The bandwidth is taken as the distance to the kth neighbour. * * @return the number of neighbours included inside the kernel * bandwidth, or 0 for all neighbours */ public int getKNN() { return m_kNN; } /** * Sets the kernel weighting method to use. Must be one of LINEAR, * INVERSE, or GAUSS, other values are ignored. * * @param kernel the new kernel method to use. Must be one of LINEAR, * INVERSE, or GAUSS */ public void setWeightingKernel(int kernel) { if ((kernel != LINEAR) && (kernel != INVERSE) && (kernel != GAUSS)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -