using in c++
LJ191021 opened this issue · 1 comments
LJ191021 commented
what should i do if i need to using the k-means in c++ codes instead of python?
dstein64 commented
Hi @LJ191021, the C++ code is in _core.cpp.
The code in there can be included directly in your C++ project. The Python-specific code (#include <Python.h>
and the code at the bottom that defines module_methods
, _coremodule
, and PyInit__core
) should be omitted.
Here's the same type of example that's shown for Python, in C++.
#include <iostream>
// <insert code from _core.cpp; omit python-specific code>
int main(void) {
double x[] = {4.0, 4.1, 4.2, -50, 200.2, 200.4, 200.9, 80, 100, 102};
ulong n = 10; // number of elements in x
ulong k = 4; // number of clusters
ulong clusters[n];
double centroids[k];
cluster(x, n, k, clusters, centroids);
// print the clusters
for (int i = 0; i < n; ++i) {
std::cout << clusters[i];
if (i < n - 1)
std::cout << ", ";
}
std::cout << std::endl;
// print the centroids
for (int i = 0; i < k; ++i) {
std::cout << centroids[i];
if (i < k - 1)
std::cout << ", ";
}
std::cout << std::endl;
return 0;
}