本文整理汇总了C++中GMatrix::relation方法的典型用法代码示例。如果您正苦于以下问题:C++ GMatrix::relation方法的具体用法?C++ GMatrix::relation怎么用?C++ GMatrix::relation使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GMatrix
的用法示例。
在下文中一共展示了GMatrix::relation方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: trainInner
// virtual
void GGaussianProcess::trainInner(const GMatrix& features, const GMatrix& labels)
{
if(!features.relation().areContinuous())
throw Ex("GGaussianProcess only supports continuous features. Perhaps you should wrap it in a GAutoFilter.");
if(!labels.relation().areContinuous())
throw Ex("GGaussianProcess only supports continuous labels. Perhaps you should wrap it in a GAutoFilter.");
if(features.rows() <= m_maxSamples)
{
trainInnerInner(features, labels);
return;
}
GMatrix f(features.relation().clone());
GReleaseDataHolder hF(&f);
GMatrix l(labels.relation().clone());
GReleaseDataHolder hL(&l);
for(size_t i = 0; i < features.rows(); i++)
{
f.takeRow((GVec*)&features[i]);
l.takeRow((GVec*)&labels[i]);
}
while(f.rows() > m_maxSamples)
{
size_t i = (size_t)m_rand.next(f.rows());
f.releaseRow(i);
l.releaseRow(i);
}
trainInnerInner(f, l);
}
示例2: 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);
}
示例3: trainInner
// virtual
void GLinearRegressor::trainInner(const GMatrix& features, const GMatrix& labels)
{
if(!features.relation().areContinuous())
throw Ex("GLinearRegressor only supports continuous features. Perhaps you should wrap it in a GAutoFilter.");
if(!labels.relation().areContinuous())
throw Ex("GLinearRegressor only supports continuous labels. Perhaps you should wrap it in a GAutoFilter.");
// Use a fast, but not-very-numerically-stable technique to compute an initial approximation for beta and epsilon
clear();
GMatrix* pAll = GMatrix::mergeHoriz(&features, &labels);
Holder<GMatrix> hAll(pAll);
GPCA pca(features.cols());
pca.train(*pAll);
size_t inputs = features.cols();
size_t outputs = labels.cols();
GMatrix f(inputs, inputs);
GMatrix l(inputs, outputs);
for(size_t i = 0; i < inputs; i++)
{
GVec::copy(f[i].data(), pca.basis()->row(i).data(), inputs);
double sqmag = f[i].squaredMagnitude();
if(sqmag > 1e-10)
f[i] *= 1.0 / sqmag;
l[i].set(pca.basis()->row(i).data() + inputs, outputs);
}
m_pBeta = GMatrix::multiply(l, f, true, false);
m_epsilon.resize(outputs);
GVecWrapper vw(pca.centroid().data(), m_pBeta->cols());
m_pBeta->multiply(vw.vec(), m_epsilon, false);
m_epsilon *= -1.0;
GVec::add(m_epsilon.data(), pca.centroid().data() + inputs, outputs);
// Refine the results using gradient descent
refine(features, labels, 0.06, 20, 0.75);
}
示例4: splitClass
void splitClass(GArgReader& args)
{
const char* filename = args.pop_string();
GMatrix* pData = loadData(filename);
Holder<GMatrix> hData(pData);
size_t classAttr = args.pop_uint();
bool dropClass = false;
while(args.size() > 0)
{
if(args.if_pop("-dropclass"))
dropClass = true;
else
ThrowError("Invalid option: ", args.peek());
}
for(size_t i = 0; i < pData->relation()->valueCount(classAttr); i++)
{
GMatrix tmp(pData->relation(), pData->heap());
pData->splitByNominalValue(&tmp, classAttr, i);
std::ostringstream oss;
PathData pd;
GFile::parsePath(filename, &pd);
string fn;
fn.assign(filename + pd.fileStart, pd.extStart - pd.fileStart);
oss << fn << "_";
pData->relation()->printAttrValue(oss, classAttr, (double)i);
oss << ".arff";
string s = oss.str();
if(dropClass)
tmp.deleteColumn(classAttr);
tmp.saveArff(s.c_str());
}
}
示例5: fillMissingValues
void fillMissingValues(GArgReader& args)
{
// Load
GMatrix* pData = loadData(args.pop_string());
Holder<GMatrix> hData(pData);
// Parse options
unsigned int nSeed = getpid() * (unsigned int)time(NULL);
bool random = false;
while(args.size() > 0)
{
if(args.if_pop("-seed"))
nSeed = args.pop_uint();
else if(args.if_pop("-random"))
random = true;
else
ThrowError("Invalid option: ", args.peek());
}
// Replace missing values and print
GRand prng(nSeed);
if(random)
{
for(size_t i = 0; i < pData->relation()->size(); i++)
pData->replaceMissingValuesRandomly(i, &prng);
}
else
{
for(size_t i = 0; i < pData->relation()->size(); i++)
pData->replaceMissingValuesWithBaseline(i);
}
pData->print(cout);
}
示例6: test_transform_mergevert
void test_transform_mergevert()
{
// Make some input files
TempFileMaker tempFile1("a.arff",
"@RELATION test\n"
"@ATTRIBUTE a1 continuous\n"
"@ATTRIBUTE a2 { alice, bob }\n"
"@ATTRIBUTE a3 { true, false }\n"
"@DATA\n"
"1.2, alice, true\n"
"2.3, bob, false\n"
);
TempFileMaker tempFile2("b.arff",
"@RELATION test\n"
"@ATTRIBUTE a1 continuous\n"
"@ATTRIBUTE a2 { charlie, bob }\n"
"@ATTRIBUTE a3 { false, true }\n"
"@DATA\n"
"3.4, bob, true\n"
"4.5, charlie, false\n"
);
// Execute the command
GPipe pipeStdOut;
if(sysExec("waffles_transform", "mergevert a.arff b.arff", &pipeStdOut) != 0)
throw Ex("exit status indicates failure");
char buf[512];
size_t len = pipeStdOut.read(buf, 512);
if(len == 512)
throw Ex("need a bigger buffer");
buf[len] = '\0';
// Check the results
GMatrix M;
M.parseArff(buf, strlen(buf));
if(M.rows() != 4 || M.cols() != 3)
throw Ex("failed");
if(M.relation().valueCount(0) != 0)
throw Ex("failed");
if(M.relation().valueCount(1) != 3)
throw Ex("failed");
if(M.relation().valueCount(2) != 2)
throw Ex("failed");
std::ostringstream oss;
const GArffRelation* pRel = (const GArffRelation*)&M.relation();
pRel->printAttrValue(oss, 1, 2.0);
string s = oss.str();
if(strcmp(s.c_str(), "charlie") != 0)
throw Ex("failed");
if(M[0][0] != 1.2 || M[1][0] != 2.3 || M[2][0] != 3.4 || M[3][0] != 4.5)
throw Ex("failed");
if(M[0][1] != 0 || M[1][1] != 1 || M[2][1] != 1 || M[3][1] != 2)
throw Ex("failed");
if(M[0][2] != 0 || M[1][2] != 1 || M[2][2] != 0 || M[3][2] != 1)
throw Ex("failed");
}
示例7: trainInner
// virtual
void GNaiveBayes::trainInner(const GMatrix& features, const GMatrix& labels)
{
if(!features.relation().areNominal())
throw Ex("GNaiveBayes only supports nominal features. Perhaps you should wrap it in a GAutoFilter.");
if(!labels.relation().areNominal())
throw Ex("GNaiveBayes only supports nominal labels. Perhaps you should wrap it in a GAutoFilter.");
beginIncrementalLearningInner(features.relation(), labels.relation());
for(size_t n = 0; n < features.rows(); n++)
trainIncremental(features[n], labels[n]);
}
示例8: trainInner
// virtual
void GNaiveInstance::trainInner(const GMatrix& features, const GMatrix& labels)
{
if(!features.relation().areContinuous())
throw Ex("GNaiveInstance only supports continuous features. Perhaps you should wrap it in a GAutoFilter.");
if(!labels.relation().areContinuous())
throw Ex("GNaiveInstance only supports continuous labels. Perhaps you should wrap it in a GAutoFilter.");
beginIncrementalLearningInner(features.relation(), labels.relation());
for(size_t i = 0; i < features.rows(); i++)
trainIncremental(features[i], labels[i]);
}
示例9: GWorkerThread
GBagTrainWorker(GMasterThread& master, GBag* pBag, const GMatrix& features, const GMatrix& labels, double trainSize, size_t seed)
: GWorkerThread(master),
m_pBag(pBag),
m_features(features),
m_labels(labels),
m_drawnFeatures(features.relation().clone()),
m_drawnLabels(labels.relation().clone()),
m_rand(seed)
{
GAssert(m_features.rows() > 0);
m_drawSize = size_t(trainSize * features.rows());
m_drawnFeatures.reserve(m_drawSize);
m_drawnLabels.reserve(m_drawSize);
}
示例10: 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);
}
示例11: split
void split(GArgReader& args)
{
// Load
GMatrix* pData = loadData(args.pop_string());
Holder<GMatrix> hData(pData);
int pats = (int)pData->rows() - args.pop_uint();
if(pats < 0)
ThrowError("out of range. The data only has ", to_str(pData->rows()), " rows.");
const char* szFilename1 = args.pop_string();
const char* szFilename2 = args.pop_string();
unsigned int nSeed = getpid() * (unsigned int)time(NULL);
bool shouldShuffle = false;
while(args.size() > 0){
if(args.if_pop("-shuffle")){
shouldShuffle = true;
}else if(args.if_pop("-seed")){
nSeed = args.pop_uint();
}else
ThrowError("Invalid option: ", args.peek());
}
// Shuffle if necessary
GRand rng(nSeed);
if(shouldShuffle){
pData->shuffle(rng);
}
// Split
GMatrix other(pData->relation());
pData->splitBySize(&other, pats);
pData->saveArff(szFilename1);
other.saveArff(szFilename2);
}
示例12: 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);
}
示例13: test_parsearff_quoting
void test_parsearff_quoting(){
const char* inputArff=
"@relation 'squares of numbers'\n"
"\n"
"@attribute 'the number' real\n"
"\n"
"@attribute 'the square of the number' real\n"
"\n"
"@attribute exact {'is exact', inexact,is\\\\\\ exact}\n"
"\n"
"@data\n"
"1,1,'is exact'\n"
"2,4,is\\ exact\n"
"1.414,2,inexact\n"
"3,9,\"is exact\"\n"
"4,16,\"is\\ exact\"\n"
;
GMatrix M;
M.parseArff(inputArff, strlen(inputArff));
double expected_data[5][3]={{1,1,0},{2,4,0},{1.414,2,1},{3,9,0},{4,16,2}};
const GArffRelation* pRel = (const GArffRelation*)&M.relation();
const GArffRelation& R = *pRel;
TestEqual(R.size(), (std::size_t)3, "Incorrect number of attributes");
for(unsigned row = 0; row < 5; ++row){
for(unsigned col = 0; col < 3; ++col){
std::stringstream errdescr;
errdescr << "Incorrect matrix entry [" << row << "][" << col << "]";
TestEqual(M[row][col], expected_data[row][col], errdescr.str());
}
}
TestEqual(true, R.areContinuous(0,2),
"First or second attribute is not continuous");
TestEqual(true, R.areNominal(2,1), "Third attribute is not nominal");
std::stringstream val0, val1, val2;
R.printAttrValue(val0, 2, 0);
R.printAttrValue(val1, 2, 1);
R.printAttrValue(val2, 2, 2);
TestEqual("'is exact'",val0.str(),
"First value of third attribute incorrect name");
TestEqual("inexact",val1.str(),
"Second value of third attribute incorrect name");
TestEqual("is\\ exact",val2.str(),
"Third value of third attribute incorrect name");
TestEqual("'the number'",R.attrName(0),"First attribute incorrect name");
TestEqual("'the square of the number'",R.attrName(1),
"Second attribute incorrect name");
TestEqual("exact",R.attrName(2),"Third attribute incorrect name");
}
示例14: 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());
}
示例15: SwapAttributes
void SwapAttributes(GArgReader& args)
{
GMatrix* pData = loadData(args.pop_string());
Holder<GMatrix> hData(pData);
size_t nAttr1 = args.pop_uint();
size_t nAttr2 = args.pop_uint();
size_t attrCount = pData->relation()->size();
if(nAttr1 >= attrCount)
ThrowError("Index out of range");
if(nAttr2 >= attrCount)
ThrowError("Index out of range");
pData->swapColumns(nAttr1, nAttr2);
pData->print(cout);
}