Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

StrTableVMatrix.cc

Go to the documentation of this file.
00001 // PLearn ("A C++ Machine Learning Library") 00002 // Copyright (C) 2002 Pascal Vincent and Julien Keable 00003 // 00004 00005 // Redistribution and use in source and binary forms, with or without 00006 // modification, are permitted provided that the following conditions are met: 00007 // 00008 // 1. Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // 00011 // 2. Redistributions in binary form must reproduce the above copyright 00012 // notice, this list of conditions and the following disclaimer in the 00013 // documentation and/or other materials provided with the distribution. 00014 // 00015 // 3. The name of the authors may not be used to endorse or promote 00016 // products derived from this software without specific prior written 00017 // permission. 00018 // 00019 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00020 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00021 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00022 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00024 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00025 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00026 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00027 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00030 // This file is part of the PLearn library. For more information on the PLearn 00031 // library, go to the PLearn Web site at www.plearn.org 00032 00033 /* ******************************************************* 00034 * $Id: StrTableVMatrix.cc,v 1.5 2004/03/23 23:08:09 morinf Exp $ 00035 * This file is part of the PLearn library. 00036 ******************************************************* */ 00037 00038 #include "StrTableVMatrix.h" 00039 00040 namespace PLearn { 00041 using namespace std; 00042 00043 StrTableVMatrix::StrTableVMatrix() 00044 {} 00045 00046 00047 /* This contructor takes a StringTable (which is simply a matrix of string) and converts it to a matrix of reals 00048 using real->string and string->real maps 00049 */ 00050 StrTableVMatrix::StrTableVMatrix(const StringTable & st) 00051 : inherited(Mat(st.length(),st.width())) 00052 { 00053 map<string,real>::iterator it; 00054 double dbl; 00055 TVec<int> mapnum(st.width(),0); 00056 TVec<string> vec(st.width()); 00057 TVec<bool> hasreal; 00058 Vec colmax; 00059 hasreal.resize(st.width()); 00060 colmax.resize(st.width()); 00061 00062 for(int j=0;j<st.width();j++) 00063 { 00064 hasreal[j]=false; 00065 colmax[j]=0; 00066 } 00067 00068 map_sr.resize(st.width()); 00069 map_rs.resize(st.width()); 00070 00071 for(int j=0;j<st.width();j++) 00072 declareField(j,st.getFieldName(j), VMField::UnknownType); 00073 00074 // 1st pass to detect maximums 00075 for(int i=0;i<st.length();i++) 00076 { 00077 vec=st(i); 00078 for(int j=0;j<st.width();j++) 00079 if(pl_isnumber(vec[j],&dbl)) 00080 { 00081 hasreal[j]=true; 00082 if(!is_missing(dbl)) 00083 if(colmax[j]<dbl) 00084 colmax[j]=dbl; 00085 } 00086 } 00087 00088 for(int j=0;j<st.width();j++) 00089 if(hasreal[j]) 00090 mapnum[j]=(int)ceil((double)colmax[j])+1; 00091 00092 for(int i=0;i<st.length();i++) 00093 { 00094 vec=st(i); 00095 for(int j=0;j<st.width();j++) 00096 if(!pl_isnumber(vec[j],&dbl)) 00097 { 00098 if((it=map_sr[j].find(vec[j]))==map_sr[j].end()) 00099 { 00100 data(i,j)=mapnum[j]; 00101 map_sr[j][vec[j]]=mapnum[j]; 00102 map_rs[j][mapnum[j]]=vec[j]; 00103 mapnum[j]++; 00104 } 00105 else data(i,j)=it->second; 00106 } 00107 else data(i,j)=dbl; 00108 } 00109 00110 } 00111 00112 PLEARN_IMPLEMENT_OBJECT(StrTableVMatrix, "ONE LINE DESCR", "NO HELP"); 00113 00114 } // end of namespace PLearn

Generated on Tue Aug 17 16:07:31 2004 for PLearn by doxygen 1.3.7