paradiseo/trunk/paradiseo-eo/doc/html/_dataset_8cpp-source.html
legrand c3aec878e5 Paradiseo-eo sources added
git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@40 331e1502-861f-0410-8da2-ba01fb791d7f
2006-12-12 14:49:08 +00:00

148 lines
8.8 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>EO: Dataset.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.3.9.1 -->
<div class="qindex"> <form class="search" action="search.php" method="get">
<a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="namespacemembers.html">Namespace&nbsp;Members</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a> | <span class="search"><u>S</u>earch&nbsp;for&nbsp;<input class="search" type="text" name="query" value="" size="20" accesskey="s"/></span></form></div>
<div class="nav">
<a class="el" href="dir_000007.html">contrib</a>&nbsp;/&nbsp;<a class="el" href="dir_000008.html">mathsym</a>&nbsp;/&nbsp;<a class="el" href="dir_000012.html">regression</a></div>
<h1>Dataset.cpp</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">/* </span>
00002 <span class="comment"> * Copyright (C) 2005 Maarten Keijzer</span>
00003 <span class="comment"> *</span>
00004 <span class="comment"> * This program is free software; you can redistribute it and/or modify</span>
00005 <span class="comment"> * it under the terms of version 2 of the GNU General Public License as </span>
00006 <span class="comment"> * published by the Free Software Foundation. </span>
00007 <span class="comment"> *</span>
00008 <span class="comment"> * This program is distributed in the hope that it will be useful,</span>
00009 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
00010 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
00011 <span class="comment"> * GNU General Public License for more details.</span>
00012 <span class="comment"> *</span>
00013 <span class="comment"> * You should have received a copy of the GNU General Public License</span>
00014 <span class="comment"> * along with this program; if not, write to the Free Software</span>
00015 <span class="comment"> * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span>
00016 <span class="comment"> */</span>
00017
00018 <span class="preprocessor">#include "Dataset.h"</span>
00019 <span class="preprocessor">#include &lt;fstream&gt;</span>
00020 <span class="preprocessor">#include &lt;sstream&gt;</span>
00021
00022 <span class="preprocessor">#include &lt;iostream&gt;</span>
00023
00024 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
00025
00026 <span class="keyword">class </span>DataSetImpl {
00027 <span class="keyword">public</span>:
00028 vector&lt; vector&lt;double&gt; &gt; inputs;
00029 vector&lt;double&gt; targets;
00030
00031 <span class="keywordtype">void</span> read_data(vector&lt;string&gt; strings) {
00032 <span class="comment">// find the number of inputs</span>
00033
00034 istringstream cnt(strings[0]);
00035 <span class="keywordtype">unsigned</span> n = 0;
00036 <span class="keywordflow">for</span> (;;) {
00037 string s;
00038 cnt &gt;&gt; s;
00039 <span class="keywordflow">if</span> (!cnt) <span class="keywordflow">break</span>;
00040 ++n;
00041 }
00042
00043 inputs.resize(strings.size(), vector&lt;double&gt;(n-1));
00044 targets.resize(strings.size());
00045
00046 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i &lt; strings.size(); ++i) {
00047 istringstream is(strings[i]);
00048 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> j = 0; j &lt; n; ++j) {
00049
00050 <span class="keywordflow">if</span> (!is) {
00051 cerr &lt;&lt; <span class="stringliteral">"Too few targets in record "</span> &lt;&lt; i &lt;&lt; endl;
00052 exit(1);
00053 }
00054
00055 <span class="keywordflow">if</span> (j &lt; n-1) {
00056 is &gt;&gt; inputs[i][j];
00057 } <span class="keywordflow">else</span> {
00058 is &gt;&gt; targets[i];
00059 }
00060
00061 }
00062 }
00063
00064 }
00065
00066 };
00067
00068 Dataset::Dataset() { pimpl = <span class="keyword">new</span> DataSetImpl; }
00069 Dataset::~Dataset() { <span class="keyword">delete</span> pimpl; }
00070 Dataset::Dataset(<span class="keyword">const</span> Dataset&amp; that) { pimpl = <span class="keyword">new</span> DataSetImpl(*that.pimpl); }
00071 Dataset&amp; Dataset::operator=(<span class="keyword">const</span> Dataset&amp; that) { *pimpl = *that.pimpl; <span class="keywordflow">return</span> *<span class="keyword">this</span>; }
00072
00073 <span class="keywordtype">unsigned</span> Dataset::n_records()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> pimpl-&gt;targets.size(); }
00074 <span class="keywordtype">unsigned</span> Dataset::n_fields()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> pimpl-&gt;inputs[0].size(); }
00075 <span class="keyword">const</span> std::vector&lt;double&gt;&amp; Dataset::get_inputs(<span class="keywordtype">unsigned</span> record)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> pimpl-&gt;inputs[record]; }
00076 <span class="keywordtype">double</span> Dataset::get_target(<span class="keywordtype">unsigned</span> record)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> pimpl-&gt;targets[record]; }
00077
00078 <span class="keywordtype">double</span> error(string errstr);
00079
00080 <span class="keywordtype">void</span> Dataset::load_data(std::string filename) {
00081 vector&lt;string&gt; strings; <span class="comment">// first load it in strings</span>
00082
00083 ifstream is(filename.c_str());
00084
00085 <span class="keywordflow">for</span>(;;) {
00086 string s;
00087 getline(is, s);
00088 <span class="keywordflow">if</span> (!is) <span class="keywordflow">break</span>;
00089
00090 <span class="keywordflow">if</span> (s[0] == <span class="charliteral">'#'</span>) <span class="keywordflow">continue</span>; <span class="comment">// comment, skip</span>
00091
00092 strings.push_back(s);
00093 }
00094
00095 is.close();
00096
00097 <span class="keywordflow">if</span> (strings.size() == 0) {
00098 error(<span class="stringliteral">"No data could be loaded"</span>);
00099 }
00100
00101 pimpl-&gt;read_data(strings);
00102
00103 }
00104
00105 std::vector&lt;double&gt; Dataset::input_minima()<span class="keyword"> const </span>{
00106 vector&lt;vector&lt;double&gt; &gt;&amp; in = pimpl-&gt;inputs;
00107
00108 vector&lt;double&gt; mn(in[0].size(), 1e+50);
00109 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i &lt; in.size(); ++i) {
00110 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> j = 0; j &lt; in[i].size(); ++j) {
00111 mn[j] = std::min(mn[j], in[i][j]);
00112 }
00113 }
00114
00115 <span class="keywordflow">return</span> mn;
00116 }
00117
00118 vector&lt;double&gt; Dataset::input_maxima()<span class="keyword"> const </span>{
00119 vector&lt;vector&lt;double&gt; &gt;&amp; in = pimpl-&gt;inputs;
00120
00121 vector&lt;double&gt; mx(in[0].size(), -1e+50);
00122 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i &lt; in.size(); ++i) {
00123 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> j = 0; j &lt; in[i].size(); ++j) {
00124 mx[j] = std::max(mx[j], in[i][j]);
00125 }
00126 }
00127
00128 <span class="keywordflow">return</span> mx;
00129 }
00130
00131
00132
00133
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:34 2006 for EO by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.3.9.1 </small></address>
</body>
</html>