コード例 #1
0
int main(int argc, char** argv) {
    
    // seed random generator
    srand(time(NULL));

    ifstream infile("../iris.data");
    string line;
    DataVec data;
    double fac = 4.0;
    while (std::getline(infile, line))
    {
        std::vector<std::string> fields;
        boost::split(fields,line, boost::is_any_of(","));
        assert(fields.size() == 5);

        double x1 = atof(fields[0].c_str())*fac;
        double x2 = atof(fields[1].c_str())*fac;
        double x3 = atof(fields[2].c_str())*fac;
        double x4 = atof(fields[3].c_str())*fac;

        Point p(x1,x2,x3,x4);
        //std::cout << p << std::endl;
        data.push_back(p);
    }
    std::cout << "Collected " << data.size() << " points. " << std::endl;
    assert(data.size()>K);
    
    // init centroids to random points
    DataVec centroids;
    //centroids.reserve(K);
    DataVec::iterator dataBegin = random_unique(data.begin(),data.end(),K);
    std::cout << K << " random points " << std::endl;
    for (size_t i=0;i<K;++i) {
        std::cout << data[i] << "\n";
        centroids.push_back(data[i]);
    }
    std::cout << centroids.size() << " centroids " << std::endl;

    // Lloyd's algorithm to iteratively fit the cluster centroids.
    bool done = fit(data,centroids);
    while(!done) {
        done = fit(data,centroids);
        std::cout << done << "\n";
    }

    double idx = dunnIndex(data,centroids);
    cout << "Dunn Index for this clustering " << idx << "\n";

    // write clustering to file
    ofstream of("clusters.dat");
    for (DataVec::iterator it = data.begin(); it!=data.end(); ++it) {
        of << *it << std::endl;
    }
    of.close();

    return EXIT_SUCCESS;
}
コード例 #2
0
ファイル: systest.cpp プロジェクト: Thekian/nepeta
/// Test casting functions
void testCast()
{
	typedef std::vector<int8_t> DataVec;
	DataVec dvec;
	
	Nepeta nep;
	nep.getRoot().createNode("Test").createArg("Hello!");
	nep.getRoot().getNode("Test").readArg(dvec, 0);
	
	dvec.push_back(0);
	std::cout << "Casted to: " << dvec.data() << "\n";
	
	assert(std::string((const char*)dvec.data()) == nep.getRoot().getNode("Test").getArg(0)
		&& "STRINGS MUST BE EQUAL!\n");
}