Пример #1
0
static void
loadmult_datafile_sub(istream &f, bool binary, const char *fname, 
                  xvec_t &xp, yvec_t &yp, int &maxdim, int maxrows)
{
  cout << "# Reading file " << fname << endl;
  if (! f.good())
    assertfail("Cannot open " << fname);

  int pcount = 0;
  while (f.good() && maxrows--)
    {
	double y;
      	SVector x;
	y = (f.get());
        x.load(f);
      
      if (f.good())
        {          
          xp.push_back(x);
          yp.push_back(y);
          pcount += 1;
          if (x.size() > maxdim)
            maxdim = x.size();
        }
    }
  cout << "# Read " << pcount << " examples." << endl;
  

}
Пример #2
0
void
load(const char *fname, xvec_t &xp, yvec_t &yp)
{
  cout << "Loading " << fname << "." << endl;
  
  igzstream f;
  f.open(fname);
  if (! f.good())
    {
      cerr << "ERROR: cannot open " << fname << "." << endl;
      exit(10);
    }
  int pcount = 0;
  int ncount = 0;

  bool binary;
  string suffix = fname;
  if (suffix.size() >= 7)
    suffix = suffix.substr(suffix.size() - 7);
  if (suffix == ".dat.gz")
    binary = false;
  else if (suffix == ".bin.gz")
    binary = true;
  else
    {
      cerr << "ERROR: filename should end with .bin.gz or .dat.gz" << endl;
      exit(10);
    }

  while (f.good())
    {
      SVector x;
      double y;
      if (binary)
        {
          y = (f.get()) ? +1 : -1;
          x.load(f);
        }
      else
        {
          f >> y >> x;
        }
      if (f.good())
        {
          assert(y == +1 || y == -1);
          xp.push_back(x);
          yp.push_back(y);
          if (y > 0)
            pcount += 1;
          else
            ncount += 1;
          if (x.size() > dim)
            dim = x.size();
        }
      if (trainsize > 0 && xp.size() > (unsigned int)trainsize)
        break;
    }
  cout << "Read " << pcount << "+" << ncount 
       << "=" << pcount + ncount << " examples." << endl;
}
Пример #3
0
void SVSet::deleteVec(DLPSV* ps)
{
   /* delete last entries, in an SVECTOR and also in an DLPSV there is always the position -1 used for memorizing
    * the size; this is the reason why we need to delete max()+1 entries
    */
   if (list.last() == ps)
      removeLast(ps->max() + 1);
   /* merge space of predecessor with position which will be deleted, therefore we do not need to delete any
    * memory or do an expensive memmove
    *
    * @note an SVECTOR and also in an DLPSV memorize the size always on position -1; this is the reason why we
    *       need to set the new mem size to the old combined size + 2
    */
   else if (list.first() != ps)
   {
      SVector* prev = ps->prev();
      int sz = prev->size();
      prev->setMem(prev->max() + ps->max() + 2, prev->mem());
      prev->set_size(sz);
   }
   /* delete the front entries of the first list entry and correct the memory pointers in the vectors */
   /* @note we do this by merging the first both vectors, move the entries from the second vector up front, and
    *       correct the size
    */
   else
   {
      SVector* next = ps->next();
      int sz = next->size();
      int bothmax = next->max() + ps->max();
      int offset = 0;

      /* the first element does not need to start at the beginning of the data array; why ??? */
      while( &(this->SVSetBase::operator[](offset)) != ps->mem() )
      {
         ++offset;
         assert(offset < size());
      }

      /* move all entries of the second vector to the front */
      for(int j = 0; j <= sz; ++j)
      {
         this->SVSetBase::operator[](offset + j) = next->mem()[j];
      }

      /* correct the data memmory pointer and the maximal space */
      next->setMem(bothmax + 2, ps->mem());
      /* correct size */
      next->set_size(sz);
   }

   list.remove(ps);
}
Пример #4
0
DSVector::DSVector(const SVector& old)
   : theelem( 0 )
{
   allocMem(old.size() + 1);
   SVector::operator= ( old );

   assert(DSVector::isConsistent());
}
Пример #5
0
Real Vector::operator*(const SVector& v) const
{
   assert(dim() >= v.dim());
   int i;
   Real x = 0;
   for (i = v.size(); i-- > 0;)
      x += val[v.index(i)] * v.value(i);
   return x;
}
Пример #6
0
void
load_classes(const char *fname, int &nclass)
{ 
  string filename = fname;
  ifstream f;
  SVector x;
  f.open(fname);
  x.load(f);
  nclass = x.size();  
}
Пример #7
0
void SVSet::xtend(SVector& svec, int newmax)
{
   if (svec.max() < newmax)
   {
      if (possiblyUnusedMem * memFactor > memSize())
         memPack(); 

      assert(has(&svec));
      DLPSV* ps = static_cast<DLPSV*>( & svec );

      if (ps == list.last())
      {
         int sz = ps->size();
         ensureMem (newmax - ps->max() + 1);
         insert(memSize(), newmax - ps->max());
         ps->setMem (newmax + 1, ps->mem());
         ps->set_size( sz );
      }
      else
      {
         ensureMem(newmax + 1);
         SVector newps(newmax + 1, &last() + 1);
         int sz = ps->size();
         insert(memSize(), newmax + 1);
         newps = svec;

         if (ps != list.first())
         {
            SVector* prev = ps->prev();
            int prevsz = prev->size();
            prev->setMem (prev->max()
                           + ps->max() + 2, prev->mem());
            prev->set_size(prevsz);
            
            possiblyUnusedMem += ps->max();
         }
         list.remove(ps);
         list.append(ps);

         ps->setMem(newmax + 1, newps.mem());
         ps->set_size(sz);
      }
   }
}
Пример #8
0
void SVSet::add2(SVector &svec, int n, const int idx[], const Real val[])
{
   xtend(svec, svec.size() + n);
   svec.add(n, idx, val);
}
Пример #9
0
void SVSet::add2(SVector &svec, int idx, Real val)
{
   xtend(svec, svec.size() + 1);
   svec.add(idx, val);
}