📄 matlabnmf.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. *//* * MatlabNMF.java * Copyright (C) 2002 Sugato Basu, Mikhail Bilenko and Yuk Wah Wong * */package weka.attributeSelection;import java.io.*;import java.util.*;import weka.core.*;import weka.filters.unsupervised.attribute.ReplaceMissingValues;import weka.filters.unsupervised.attribute.Normalize;import weka.filters.unsupervised.attribute.NominalToBinary;import weka.filters.unsupervised.attribute.Remove;import weka.filters.Filter;/** * Class for performing non-negative matrix factorization/transformation. <p> * * Valid options are:<p> * -D <br> * Don't normalize the input data. <p> * * -R <rank> <br> * Set rank of the basis matrix. Default=5. <p> * * -n <iterations> <br> * Set number of iterations. Default=20. <p> * * -O <objective function> <br> * Set objective function. Default=1. <br> * [1: sum_{i,u}[V_{iu}log(WH)_{iu}-(WH)_{iu}]; 2: norm(V-WH); 3: D(V||WH)] <p> * * -E <evaluator spec> <br> * Set attribute evaluator. Evaluator spec should contain the full * class name of an attribute evaluator followed by any options. <p> * * * @author Misha Bilenko (mbilenko@cs.utexas.edu) * @author Sugato Basu (sugato@cs.utexas.edu) * @author Yuk Wah Wong (ywwong@cs.utexas.edu) * @version $Revision: 1.1.1.1 $ */public class MatlabNMF extends AttributeEvaluator implements AttributeTransformer, OptionHandler { /** The data to transform analyse/transform */ private Instances m_trainInstances; /** Keep a copy for the class attribute (if set) */ private Instances m_trainCopy; /** The header for the transformed data format */ private Instances m_transformedFormat; /** Data has a class set */ private boolean m_hasClass; /** Number of attributes */ private int m_numAttribs; /** Number of instances */ private int m_numInstances; /** Name of temp directory */ private String m_tmpDir = new String("/tmp/"); /** Name of the Matlab program file that computes NMF */ protected String m_mFile = new String(m_tmpDir+"MatlabNMF.m"); /** Will hold the orthogonal basis of the original data */ private double [][] m_basis; /** A timestamp suffix for matching vectors with attributes */ String m_timestamp = null; /** Name of the file where attribute names will be stored */ String m_nmfAttributeFilenameBase = new String(m_tmpDir+"NMFattributes"); /** Name of the file where original data V (or v) will be stored */ String m_dataFilename = new String(m_tmpDir+"NMFdata.txt"); /** Name of the file where other parameters will be stored */ String m_paramFilename = new String(m_tmpDir+"NMFparam.txt"); /** Name of the file where the basis W will be stored */ String m_basisFilename = new String(m_tmpDir+"NMFbasis.txt"); /** Name of the file where the basis vectors in decreasing order of ranking will be stored */ String m_rankedBasisFilenameBase = new String(m_tmpDir+"NMFrankedbasis"); /** Name of the file where the encoding H (or h) of V (or v) will be stored */ String m_encodingFilename = new String(m_tmpDir+"NMFencoding.txt"); /** Filters for original data */ private ReplaceMissingValues m_replaceMissingFilter; private Normalize m_normalizeFilter; private Remove m_attributeFilter; /** normalize the input data? */ private boolean m_normalize = false; /** The number of attributes in the NMF transformed data (i.e. rank of the basis matrix) */ private int m_rank = 5; /** The number of iterations for gradient descent */ private int m_iter = 20; /** The objective function */ private int m_obj = 1; /** The attribute evaluator to use */ private ASEvaluation m_eval = new weka.attributeSelection.ChiSquaredAttributeEval(); /** * Returns a string describing this attribute transformer * @return a description of the evaluator suitable for * displaying in the explorer/experimenter gui */ public String globalInfo() { return "Performs non-negative matrix factorization. " +"Use in conjunction with a Ranker search."; } /** * Returns an enumeration describing the available options. <p> * * @return an enumeration of all the available options. **/ public Enumeration listOptions () { Vector newVector = new Vector(5); newVector.addElement(new Option("\tDon't normalize input data." , "D", 0, "-D")); newVector.addElement (new Option("\tSet rank of the basis matrix. Default=5." , "R", 1, "-R <rank>")); newVector.addElement (new Option("\tSet number of iterations. Default=20." , "n", 1, "-n <iterations>")); newVector.addElement (new Option("\tSet objective function. Default=1.\n" +"\t[1: sum_{i,u}[V_{iu}log(WH)_{iu}-(WH)_{iu}]; " +"2: norm(V-WH); 3: D(V||WH)]" , "O", 1, "-O <objection function>")); newVector.addElement (new Option("\tSet attribute evaluator. Full class name of attribute\n" +"\tevaluator, followed by its options.\n" +"\teg: \"weka.attributeSelection.ChiSquaredAttributeEval\"", "E", 1, "-E <evaluator spec>")); return newVector.elements(); } /** * Parses a given list of options. * * Valid options are:<p> * -D <br> * Don't normalize the input data. <p> * * -R <rank> <br> * Set rank of the basis matrix. Default=5. <p> * * -n <iterations> <br> * Set number of iterations. Default=20. <p> * * -O <objective function> <br> * Set objective function. Default=1. <br> * [1: sum_{i,u}[V_{iu}log(WH)_{iu}-(WH)_{iu}]; 2: norm(V-WH); * 3: D(V||WH)] <p> * * -E <evaluator spec> <br> * Set attribute evaluator. Evaluator spec should contain the full * class name of an attribute evaluator followed by any options. <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 { resetOptions(); String optionString; setNormalize(!Utils.getFlag('D', options)); optionString = Utils.getOption('R', options); if (optionString.length() != 0) { Integer temp; temp = Integer.valueOf(optionString); setRank(temp.intValue()); } optionString = Utils.getOption('n', options); if (optionString.length() != 0) { Integer temp; temp = Integer.valueOf(optionString); setIterations(temp.intValue()); } optionString = Utils.getOption('O', options); if (optionString.length() != 0) { Integer temp; temp = Integer.valueOf(optionString); setObjectiveFunction(temp.intValue()); } optionString = Utils.getOption('E', options); if (optionString.length() == 0) { throw new Exception("An attribute evaluator must be specified" + " with the -E option."); } String[] evaluatorSpec = Utils.splitOptions(optionString); if (evaluatorSpec.length == 0) { throw new Exception("Invalid attribute evaluator specification string"); } String evaluatorName = evaluatorSpec[0]; evaluatorSpec[0] = ""; setEvaluator(ASEvaluation.forName(evaluatorName, evaluatorSpec)); } /** * Reset to defaults */ private void resetOptions() { m_normalize = false; m_rank = 5; m_iter = 20; m_obj = 1; m_eval = new weka.attributeSelection.ChiSquaredAttributeEval(); } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String normalizeTipText() { return "Normalize input data."; } /** * Set whether input data will be normalized. * @param n true if input data is to be normalized */ public void setNormalize(boolean n) { m_normalize = n; } /** * Gets whether or not input data is to be normalized * @return true if input data is to be normalized */ public boolean getNormalize() { return m_normalize; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String rankTipText() { return "Set rank of the basis matrix."; } /** * Sets the rank of the basis matrix W. * @param r the rank of the basis matrix */ public void setRank(int r) { m_rank = r; } /** * Gets the rank of the basis matrix W. * @return the rank of the basis matrix */ public int getRank() { return m_rank; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String iterationsTipText() { return "Set number of iterations."; } /** * Sets the number of iterations for gradient descent. * @param i the number of iterations */ public void setIterations(int i) { m_iter = i; } /** * Gets the number of iterations for gradient descent. * @return the nubmer of iterations */ public int getIterations() { return m_iter; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String objectiveFunctionTipText() { return "Set objective function. " +"[1:sum(V*log(WH)-WH); 2:norm; 3:divergence]"; } /** * Sets the objective function. * @param i the objective function */ public void setObjectiveFunction(int i) { m_obj = i; } /** * Gets the objective function. * @return the objective function */ public int getObjectiveFunction() { return m_obj; } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -