⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 decomposition.cpp

📁 orange源码 数据挖掘技术
💻 CPP
📖 第 1 页 / 共 3 页
字号:
  { const_ITERATE(TVarList, evi, afreeSet)
      free[gen->domain->getVarNum(*evi)] = true;
  }

  return operator()(gen, bound, aboundSet, free, weightID);
}


PIM TIMBySorting::operator()(PExampleGenerator gen, const vector<bool> &bound, const TVarList &aboundSet, const vector<bool> &free, const int &weightID)
{
  PIM im=mlnew TIM(gen->domain->classVar->varType);

  vector<bool>::const_iterator freee(free.end());

  // prepare free domain for rowExamples if recordRowExamples==true
  PDomain freeDomain;
  PExampleTable rowTable;
  if (recordRowExamples) {
    TVarList freeSet;
    { TVarList::const_iterator vi(gen->domain->attributes->begin());
      const_ITERATE(vector<bool>, bi, free) {
        if (*bi)
          freeSet.push_back(*vi);
        vi++;
      }
    }
    freeDomain = mlnew TDomain(PVariable(), freeSet);
    rowTable = mlnew TExampleTable(freeDomain);
    im->rowExamples = rowTable;
  }

  PDomain boundDomain = mlnew TDomain(PVariable(), aboundSet);

  // prepare a sorted table with examples and graph node indices
  TSortedExamples_nodeIndices sorted(gen, bound, free);
  if (sorted.empty())
    raiseError("no examples");

  im->columns = vector<T_ExampleIMColumnNode>(sorted.maxIndex+1);

  // pointers to last elements of the lists
  vector<TIMColumnNode *> lastEl(im->columns.size(), (TIMColumnNode *)NULL);

  int classes = (im->varType==TValue::INTVAR) ? gen->domain->classVar->noOfValues() : -1;

  // Extract the incompatibility matrix
  if (recordRowExamples)
    rowTable->addExample(sorted.front().example.getReference()); // no problem - this example will be converted anyway...

  int rowIndex = 0;
  for(TSortedExamples_nodeIndices::iterator ebegin(sorted.begin()), eend(sorted.end()), eprev(ebegin);
      ebegin!=eend;
      eprev = ebegin++) {

    // Check equality of free attributes and increase rowIndex, if needed
    // We check for equality without converting the example - this is much cheaper
    if (ebegin!=eprev) {
      TExample::iterator ti((*ebegin).example->begin()), pi((*eprev).example->begin());
      vector<bool>::const_iterator bi(free.begin());
	    for( ; (bi!=freee) && (!*bi || (*ti==*pi)); bi++, ti++, pi++);
      if (bi!=freee) {
        rowIndex++;
        if (recordRowExamples)
          rowTable->addExample((*ebegin).example.getReference());
      }
    }

    // Add the example to the matrix
    int colIndex = (*ebegin).nodeIndex;
    if (classes>=0) {
      TIMColumnNode *coli=lastEl[colIndex];
      if (!coli) {
        T_ExampleIMColumnNode &node = im->columns[colIndex];
        node.example = mlnew TExample(boundDomain, (*ebegin).example.getReference());
        coli = lastEl[colIndex] = node.column = mlnew TDIMColumnNode(rowIndex, classes);
      }
      else if (coli->index!=rowIndex) {
        // This element is complete; let us compute abs
        dynamic_cast<TDIMColumnNode *>(coli)->computeabs();

        // Create new element, link the previous last to it and state that this one's now the last
        coli = mlnew TDIMColumnNode(rowIndex, classes);
        lastEl[colIndex]->next = coli;
        lastEl[colIndex] = coli;
      }
      // no need to check whether the class is special -- checked when building TSortedExamples_nodeIndices
      (dynamic_cast<TDIMColumnNode *>(coli))->distribution[(*ebegin).example->getClass().intV] += WEIGHT((*ebegin).example.getReference());
    }
    else {
      TIMColumnNode *coli=lastEl[colIndex];
      if (!coli)
        coli = lastEl[colIndex] = im->columns[colIndex].column = mlnew TFIMColumnNode(rowIndex);
      else if (coli->index!=rowIndex) {
        coli = mlnew TFIMColumnNode(rowIndex);
        lastEl[colIndex]->next = coli;
        lastEl[colIndex] = coli;
	    }
      // no need to check whether the class is special -- checked when building TSortedExamples_nodeIndices
      (dynamic_cast<TFIMColumnNode *>(coli))->add((*ebegin).example->getClass().floatV, WEIGHT((*ebegin).example.getReference()));
    }
  }

  // Complete the uncompleted elements by computing abs
  if (classes>=0)
    ITERATE(vector<TIMColumnNode *>, li, lastEl)
      if (*li)
        dynamic_cast<TDIMColumnNode *>(*li)->computeabs();

  return im;
}


PIM TIMConstructor::operator ()(PIMByRows imrows)
{
  PIM im = mlnew TIM(imrows->varType);
  im->columns = vector<T_ExampleIMColumnNode>();

  int column = 0;
  ITERATE(vector<PExample>, ei, imrows->columnExamples) 
    if (*ei) {
      im->columns.push_back(T_ExampleIMColumnNode(*ei));
      TIMColumnNode **lastnode = &im->columns.back().column;
      for(vector<TDIMRow>::const_iterator rbi(imrows->rows.begin()), ri(rbi), rei(imrows->rows.end()); ri!=rei; ri++) {
        const float *di = (*ri).nodes[column], *de = di+(*ri).noOfValues;
        while ((di!=de) && !*di);
        if (di!=de) { // column is not empty
          (*lastnode) = mlnew TDIMColumnNode(ri-rbi, (*ri).noOfValues, (*ri).nodes[column]);
          lastnode = &(*lastnode)->next;
        }
      }
      column++;
    }

  if (recordRowExamples) {
    im->rowExamples = mlnew TExampleTable(imrows->rows.front().example->domain);
    ITERATE(vector<TDIMRow>, ri, imrows->rows)
      im->rowExamples->addExample((*ri).example.getReference());
  }

  return im;
}


TDIMRow::TDIMRow(PExample ex, const int &n, const int &classes)
: example(ex)
{ nodes.reserve(n);
  for(int i=n; i--;) {
    float *ndist = mlnew float[classes];
    nodes.push_back(ndist);
    for(float *de = ndist + classes; ndist != de; *(ndist++) = 0.0);
  }
}


TDIMRow::~TDIMRow()
{ ITERATE(vector<float *>, di, nodes)
    mldelete *di;
}


PIMByRows TIMByRowsConstructor::operator()(PExampleGenerator gen, const TVarList &aboundSet, const int &weightID)
{
  // Identify bound attributes
  vector<bool> bound = vector<bool>(gen->domain->attributes->size(), false);
  vector<bool> free = vector<bool>(gen->domain->attributes->size(), true);
  const_ITERATE(TVarList, evi, aboundSet) {
    int vn = gen->domain->getVarNum(*evi);
    bound[vn] = true;
    free[vn] = false;
  }

  return operator()(gen, bound, aboundSet, free, weightID);
}


TIMByRows::TIMByRows(const int &avarType)
: varType(avarType)
{}



int TIMByRows::traverse(visitproc visit, void *arg) const
{ TRAVERSE(TOrange::traverse);
  const_ITERATE(vector<PExample>, ei, columnExamples)
    PVISIT(*ei);
  const_ITERATE(vector<TDIMRow>, ri, rows)
    PVISIT((*ri).example);
  return 0;
}


int TIMByRows::dropReferences()
{ DROPREFERENCES(TOrange::dropReferences);
  columnExamples.clear();
  rows.clear();
  return 0;
}



PIMByRows TIMByRowsConstructor::operator()(PExampleGenerator gen, const TVarList &aboundSet, const TVarList &afreeSet, const int &weightID)
{
  // Identify bound attributes
  vector<bool> bound = vector<bool>(gen->domain->attributes->size(), false);
  { const_ITERATE(TVarList, evi, aboundSet)
      bound[gen->domain->getVarNum(*evi)]=true;
  }

  vector<bool> free = vector<bool>(gen->domain->attributes->size(), false);
  { const_ITERATE(TVarList, evi, afreeSet)
      free[gen->domain->getVarNum(*evi)]=true;
  }

  return operator()(gen, bound, aboundSet, free, weightID);
}


PIMByRows TIMByRowsBySorting::operator()(PExampleGenerator gen, const vector<bool> &bound, const TVarList &aboundSet, const vector<bool> &free, const int &weightID)
{
  int classes =   (gen->domain->classVar->varType==TValue::INTVAR)
                ? gen->domain->classVar->noOfValues()
                : -1;

  if (classes==-1)
    raiseError("these is no class or it is not discrete.");

  PIMByRows im = mlnew TIMByRows(gen->domain->classVar->varType);

  vector<bool>::const_iterator freee(free.end());

  // prepare free domain for rowExamples if recordRowExamples==true
  PDomain freeDomain;
  {
    TVarList freeSet;
    { TVarList::const_iterator vi(gen->domain->attributes->begin());
      const_ITERATE(vector<bool>, fi, free) {
        if (*fi)
          freeSet.push_back(*vi);
        vi++;
      }
    }
    freeDomain = mlnew TDomain(PVariable(), freeSet);
  }

  PDomain boundDomain = mlnew TDomain(PVariable(), aboundSet);

  // prepare a sorted table with examples and graph node indices
  TSortedExamples_nodeIndices sorted(gen, bound, free);
  if (sorted.empty())
    raiseError("no examples");
  int columns = sorted.maxIndex+1;
  im->columnExamples = vector<PExample>(columns, PExample());

  // Extract the incompatibility matrix
  im->rows.push_back(TDIMRow(mlnew TExample(freeDomain, sorted.front().example.getReference()), columns, classes));
  for(TSortedExamples_nodeIndices::iterator ebegin(sorted.begin()), eend(sorted.end()), eprev(ebegin);
      ebegin!=eend;
      eprev=ebegin++) {

    // Check equality of free attributes and increase rowIndex, if needed
    // We check for equality without converting the example - this is much cheaper
    if (ebegin!=eprev) {
      TExample::iterator ti((*ebegin).example->begin()), pi((*eprev).example->begin());
      vector<bool>::const_iterator fi(free.begin());
	    for( ; (fi!=freee) && (!*fi || (*ti==*pi)); fi++, ti++, pi++);
      if (fi!=freee)
        im->rows.push_back(TDIMRow(mlnew TExample(freeDomain, (*ebegin).example.getReference()), columns, classes));
    }

    int &nodeIndex = (*ebegin).nodeIndex;
    if (!im->columnExamples[nodeIndex])
      im->columnExamples[nodeIndex] = PExample(mlnew TExample(boundDomain, (*ebegin).example.getReference()));

    // Add the example to the matrix
    // no need to check whether the class is special -- checked when building TSortedExamples_nodeIndices
    im->rows.back().nodes[nodeIndex][(*ebegin).example->getClass().intV] += WEIGHT((*ebegin).example.getReference());
  }

  return im;
}


PIM TIMByIMByRows::operator()(PExampleGenerator gen, const vector<bool> &bound, const TVarList &aboundSet, const vector<bool> &free, const int &weightID)
{ PIMByRows imrows=TIMByRowsBySorting()(gen, bound, aboundSet, free, weightID);
  return TIMConstructor::operator()(imrows); 
}


TIMByRelief::TIMByRelief()
: distance(PExamplesDistance_Relief()),
  k(10),
  m(50),
  kFromColumns(0.0),
  ignoreSameExample(false),
  convertToBinary(false),
  correctClassFirst(false),
  allExamples(false),
  allSameNeighbours(false)
{}


PIM TIMByRelief::operator()(PExampleGenerator gen, const vector<bool> &bound, const TVarList &aboundSet, const vector<bool> &free, const int &weightID)
{ TIMByRowsByRelief imrr;
  imrr.k = k;
  imrr.m = m;
  imrr.kFromColumns = kFromColumns;

  imrr.ignoreSameExample = ignoreSameExample;
  imrr.convertToBinary = convertToBinary;
  imrr.correctClassFirst = correctClassFirst;
  imrr.allExamples = allExamples;
  imrr.allSameNeighbours = allSameNeighbours;

  PIMByRows imrows = imrr(gen, bound, aboundSet, free, weightID);
  return TIMConstructor::operator()(imrows);
}



class TCI_w {
public:
  long columnIndex; // index of the column (product of bound values)
  long freeIndex;   // index to freeExamples table
  TCI_w(const long &ci, const long &fi)
  : columnIndex(ci),
  freeIndex(fi)
  {}
};


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -