本文整理汇总了C++中GMatrix::row方法的典型用法代码示例。如果您正苦于以下问题:C++ GMatrix::row方法的具体用法?C++ GMatrix::row怎么用?C++ GMatrix::row使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GMatrix
的用法示例。
在下文中一共展示了GMatrix::row方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: autoCorrelation
void autoCorrelation(GArgReader& args)
{
GMatrix* pData = loadData(args.pop_string());
Holder<GMatrix> hData(pData);
size_t lag = std::min((size_t)256, pData->rows() / 2);
size_t dims = pData->cols();
GTEMPBUF(double, mean, dims);
pData->centroid(mean);
GMatrix ac(0, dims + 1);
for(size_t i = 1; i <= lag; i++)
{
double* pRow = ac.newRow();
*(pRow++) = (double)i;
for(size_t j = 0; j < dims; j++)
{
*pRow = 0;
size_t k;
for(k = 0; k + i < pData->rows(); k++)
{
double* pA = pData->row(k);
double* pB = pData->row(k + i);
*pRow += (pA[j] - mean[j]) * (pB[j] - mean[j]);
}
*pRow /= k;
pRow++;
}
}
ac.print(cout);
}
示例2: TransformData
void TransformData(const double* pVector)
{
m_transform.fromVector(pVector + m_attrs, m_attrs);
for(size_t i = 0; i < m_pData2->rows(); i++)
{
double* pPatIn = m_pData2->row(i);
double* pPatOut = m_transformed.row(i);
m_transform.multiply(pPatIn, pPatOut);
GVec::add(pPatOut, pVector, m_attrs);
}
}
示例3: center
///TODO: this command should be documented
void center(GArgReader& args)
{
GMatrix* pData = loadData(args.pop_string());
Holder<GMatrix> hData(pData);
unsigned int r = args.pop_uint();
size_t cols = pData->cols();
double* pRow = pData->row(r);
for(size_t i = 0; i < r; ++i)
GVec::subtract(pData->row(i), pRow, cols);
for(size_t i = r + 1; i < pData->rows(); ++i)
GVec::subtract(pData->row(i), pRow, cols);
GVec::setAll(pRow, 0.0, cols);
pData->print(cout);
}
示例4: dropRandomValues
void dropRandomValues(GArgReader& args)
{
GMatrix* pData = loadData(args.pop_string());
double portion = args.pop_double();
// Parse the options
unsigned int seed = getpid() * (unsigned int)time(NULL);
while(args.next_is_flag())
{
if(args.if_pop("-seed"))
seed = args.pop_uint();
else
ThrowError("Invalid option: ", args.peek());
}
GRand rand(seed);
size_t n = pData->rows() * pData->cols();
size_t k = size_t(portion * n);
for(size_t i = 0; i < pData->cols(); i++)
{
size_t vals = pData->relation()->valueCount(i);
if(vals == 0)
{
for(size_t j = 0; j < pData->rows(); j++)
{
if(rand.next(n) < k)
{
pData->row(j)[i] = UNKNOWN_REAL_VALUE;
k--;
}
n--;
}
}
else
{
for(size_t j = 0; j < pData->rows(); j++)
{
if(rand.next(n) < k)
{
pData->row(j)[i] = UNKNOWN_DISCRETE_VALUE;
k--;
}
n--;
}
}
}
pData->print(cout);
}
示例5: cumulativeColumns
void cumulativeColumns(GArgReader& args)
{
GMatrix* pA = loadData(args.pop_string());
Holder<GMatrix> hA(pA);
vector<size_t> cols;
parseAttributeList(cols, args, pA->cols());
double* pPrevRow = pA->row(0);
for(size_t i = 1; i < pA->rows(); i++)
{
double* pRow = pA->row(i);
for(vector<size_t>::iterator it = cols.begin(); it != cols.end(); it++)
pRow[*it] += pPrevRow[*it];
pPrevRow = pRow;
}
pA->print(cout);
}
示例6: addNoise
void addNoise(GArgReader& args)
{
GMatrix* pData = loadData(args.pop_string());
Holder<GMatrix> hData(pData);
double dev = args.pop_double();
// Parse the options
unsigned int seed = getpid() * (unsigned int)time(NULL);
int excludeLast = 0;
while(args.next_is_flag())
{
if(args.if_pop("-seed"))
seed = args.pop_uint();
else if(args.if_pop("-excludelast"))
excludeLast = args.pop_uint();
else
ThrowError("Invalid neighbor finder option: ", args.peek());
}
GRand prng(seed);
size_t cols = pData->cols() - excludeLast;
for(size_t r = 0; r < pData->rows(); r++)
{
double* pRow = pData->row(r);
for(size_t c = 0; c < cols; c++)
*(pRow++) += dev * prng.normal();
}
pData->print(cout);
}
示例7: enumerateValues
void enumerateValues(GArgReader& args)
{
GMatrix* pData = loadData(args.pop_string());
Holder<GMatrix> hData(pData);
size_t col = args.pop_uint();
if(pData->relation()->valueCount(col) > 0)
((GArffRelation*)pData->relation().get())->setAttrValueCount(col, 0);
else
{
size_t n = 0;
map<double,size_t> themap;
for(size_t i = 0; i < pData->rows(); i++)
{
double* pRow = pData->row(i);
map<double,size_t>::iterator it = themap.find(pRow[col]);
if(it == themap.end())
{
themap[pRow[col]] = n;
pRow[col] = (double)n;
n++;
}
else
pRow[col] = (double)it->second;
}
}
pData->print(cout);
}
示例8: DropMissingValues
void DropMissingValues(GArgReader& args)
{
GMatrix* pData = loadData(args.pop_string());
Holder<GMatrix> hData(pData);
GRelation* pRelation = pData->relation().get();
size_t dims = pRelation->size();
for(size_t i = pData->rows() - 1; i < pData->rows(); i--)
{
double* pPat = pData->row(i);
bool drop = false;
for(size_t j = 0; j < dims; j++)
{
if(pRelation->valueCount(j) == 0)
{
if(pPat[j] == UNKNOWN_REAL_VALUE)
{
drop = true;
break;
}
}
else
{
if(pPat[j] == UNKNOWN_DISCRETE_VALUE)
{
drop = true;
break;
}
}
}
if(drop)
pData->deleteRow(i);
}
pData->print(cout);
}
示例9:
GMatrix::GMatrix(GMatrix& m)
{
m_row = m.row();
m_col = m.col();
int size = m_row*m_col;
m_data = new double[size];
for(int i = 0; i < size; ++i)
{
m_data[i] = m[i];
}
}
示例10: transition
void transition(GArgReader& args)
{
// Load the input data
GMatrix* pActions = loadData(args.pop_string());
Holder<GMatrix> hActions(pActions);
GMatrix* pState = loadData(args.pop_string());
Holder<GMatrix> hState(pState);
if(pState->rows() != pActions->rows())
ThrowError("Expected the same number of rows in both datasets");
// Parse options
bool delta = false;
while(args.size() > 0)
{
if(args.if_pop("-delta"))
delta = true;
else
ThrowError("Invalid option: ", args.peek());
}
// Make the output data
size_t actionDims = pActions->cols();
size_t stateDims = pState->cols();
GMixedRelation* pRelation = new GMixedRelation();
sp_relation pRel = pRelation;
pRelation->addAttrs(pActions->relation().get());
pRelation->addAttrs(stateDims + stateDims, 0);
GMatrix* pTransition = new GMatrix(pRel);
pTransition->newRows(pActions->rows() - 1);
for(size_t i = 0; i < pActions->rows() - 1; i++)
{
double* pOut = pTransition->row(i);
GVec::copy(pOut, pActions->row(i), actionDims);
GVec::copy(pOut + actionDims, pState->row(i), stateDims);
GVec::copy(pOut + actionDims + stateDims, pState->row(i + 1), stateDims);
if(delta)
GVec::subtract(pOut + actionDims + stateDims, pState->row(i), stateDims);
}
pTransition->print(cout);
}
示例11: splitFold
void splitFold(GArgReader& args)
{
// Load
GMatrix* pData = loadData(args.pop_string());
Holder<GMatrix> hData(pData);
size_t fold = args.pop_uint();
size_t folds = args.pop_uint();
if(fold >= folds)
ThrowError("fold index out of range. It must be less than the total number of folds.");
// Options
string filenameTrain = "train.arff";
string filenameTest = "test.arff";
while(args.size() > 0)
{
if(args.if_pop("-out"))
{
filenameTrain = args.pop_string();
filenameTest = args.pop_string();
}
else
ThrowError("Invalid option: ", args.peek());
}
// Copy relevant portions of the data
GMatrix train(pData->relation());
GMatrix test(pData->relation());
size_t begin = pData->rows() * fold / folds;
size_t end = pData->rows() * (fold + 1) / folds;
for(size_t i = 0; i < begin; i++)
train.copyRow(pData->row(i));
for(size_t i = begin; i < end; i++)
test.copyRow(pData->row(i));
for(size_t i = end; i < pData->rows(); i++)
train.copyRow(pData->row(i));
train.saveArff(filenameTrain.c_str());
test.saveArff(filenameTest.c_str());
}
示例12: shiftColumns
void shiftColumns(GArgReader& args)
{
GMatrix* pA = loadData(args.pop_string());
Holder<GMatrix> hA(pA);
vector<size_t> cols;
parseAttributeList(cols, args, pA->cols());
double offset = args.pop_double();
for(size_t i = 0; i < pA->rows(); i++)
{
double* pRow = pA->row(i);
for(vector<size_t>::iterator it = cols.begin(); it != cols.end(); it++)
pRow[*it] += offset;
}
pA->print(cout);
}
示例13: Discretize
void Discretize(GArgReader& args)
{
// Load the file
GMatrix* pData = loadData(args.pop_string());
Holder<GMatrix> hData(pData);
// Parse Options
size_t nFirst = 0;
size_t nLast = pData->relation()->size() - 1;
size_t nBuckets = std::max(2, (int)floor(sqrt((double)pData->rows() + 0.5)));
while(args.size() > 0)
{
if(args.if_pop("-buckets"))
nBuckets = args.pop_uint();
else if(args.if_pop("-colrange"))
{
nFirst = args.pop_uint();
nLast = args.pop_uint();
}
else
ThrowError("Invalid option: ", args.peek());
}
if(nFirst < 0 || nLast >= pData->relation()->size() || nLast < nFirst)
ThrowError("column index out of range");
// Discretize the continuous attributes in the specified range
for(size_t i = nFirst; i <= nLast; i++)
{
if(pData->relation()->valueCount(i) != 0)
continue;
double min, range;
pData->minAndRange(i, &min, &range);
for(size_t j = 0; j < pData->rows(); j++)
{
double* pPat = pData->row(j);
pPat[i] = (double)std::max((size_t)0, std::min(nBuckets - 1, (size_t)floor(((pPat[i] - min) * nBuckets) / range)));
}
((GArffRelation*)pData->relation().get())->setAttrValueCount(i, nBuckets);
}
// Print results
pData->print(cout);
}
示例14: Export
void Export(GArgReader& args)
{
// Load
GMatrix* pData = loadData(args.pop_string());
Holder<GMatrix> hData(pData);
// Parse options
const char* separator = ",";
while(args.size() > 0)
{
if(args.if_pop("-tab"))
separator = " ";
else if(args.if_pop("-space"))
separator = " ";
else
ThrowError("Invalid option: ", args.peek());
}
// Print
for(size_t i = 0; i < pData->rows(); i++)
pData->relation()->printRow(cout, pData->row(i), separator);
}
示例15: loadSparseData
GSparseMatrix* GRecommenderLib::loadSparseData(const char* szFilename)
{
// Load the dataset by extension
PathData pd;
GFile::parsePath(szFilename, &pd);
if(_stricmp(szFilename + pd.extStart, ".arff") == 0)
{
// Convert a 3-column dense ARFF file to a sparse matrix
GMatrix data;
data.loadArff(szFilename);
if(data.cols() != 3)
throw Ex("Expected 3 columns: 0) user or row-index, 1) item or col-index, 2) value or rating");
double m0 = data.columnMin(0);
double r0 = data.columnMax(0) - m0;
double m1 = data.columnMin(1);
double r1 = data.columnMax(1) - m1;
if(m0 < 0 || m0 > 1e10 || r0 < 2 || r0 > 1e10)
throw Ex("Invalid row indexes");
if(m1 < 0 || m1 > 1e10 || r1 < 2 || r1 > 1e10)
throw Ex("Invalid col indexes");
GSparseMatrix* pMatrix = new GSparseMatrix(size_t(m0 + r0) + 1, size_t(m1 + r1) + 1, UNKNOWN_REAL_VALUE);
std::unique_ptr<GSparseMatrix> hMatrix(pMatrix);
for(size_t i = 0; i < data.rows(); i++)
{
GVec& row = data.row(i);
pMatrix->set(size_t(row[0]), size_t(row[1]), row[2]);
}
return hMatrix.release();
}
else if(_stricmp(szFilename + pd.extStart, ".sparse") == 0)
{
GDom doc;
doc.loadJson(szFilename);
return new GSparseMatrix(doc.root());
}
throw Ex("Unsupported file format: ", szFilename + pd.extStart);
return NULL;
}