在C++中实现k-均值
如果有人能帮助我,我将不胜感激! 在C++中实现KMead算法。p> -我必须分组的要点是已知的。 -我想把它们分成3组。 -但是集群必须在第一时间被随机实例化 当我尝试时,出现以下消息,我无法解决它 menge.exe中0x00007FF92B484E20 MengeCore.dll处未处理的异常:0xC0000005:访问冲突写入位置0x0000000000000000 当我尝试实例化集群时,问题就出在行中 提前谢谢你在C++中实现k-均值,c++,cluster-analysis,k-means,C++,Cluster Analysis,K Means,如果有人能帮助我,我将不胜感激! 在C++中实现KMead算法。p> -我必须分组的要点是已知的。 -我想把它们分成3组。 -但是集群必须在第一时间被随机实例化 当我尝试时,出现以下消息,我无法解决它 menge.exe中0x00007FF92B484E20 MengeCore.dll处未处理的异常:0xC0000005:访问冲突写入位置0x0000000000000000 当我尝试实例化集群时,问题就出在行中 提前谢谢你 #include <cassert> #include &l
#include <cassert>
#include <limits>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <list>
#include <ostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <stdio.h>
using namespace std;
class Point{
private:
double x;
double y;
public:
Point::Point(double x, double y) : x(x), y(y) {}
double Point::getX() const {
return x;
}
double Point::getY() const {
return y;
}
Point::Point() {}
bool Point::operator==(const Point &rhs) const {
return x == rhs.x &&
y == rhs.y;
}
bool Point::operator!=(const Point &rhs) const {
return !(rhs == *this);
}
friend std::ostream &operator<<(std::ostream &os, const Point &point) {
os << "(" << point.x << "," << point.y << ")";
return os;
}
double getDistance(Point &p) {
return sqrt(pow(p.x - this->x, 2) + pow(p.y - this->y, 2));
}
};
class Cluster {
public:
Point centroid;
vector<int> points;
Cluster::Cluster(const Point ¢roid, const vector<int> &points) : centroid(centroid), points(points) {}
Cluster::Cluster() {
}
string getPoints() {
string s = "";
for (int p : points) {
s += to_string(p + 1) + " ";
}
return s;
}
Cluster::Cluster(const Point ¢roid) : centroid(centroid) {}
};
vector<Point> points{ { 9, 9 },
{ 1, 1 },
{ -1, -1 },
{ 3, 3 },
{ 10, 10 },
{ -2, -2 },
{ 7, 8 },
{ 0.2, 0 },
{-1, 0},
{ 6, 10 } };
vector<Cluster> clusters{};
int main() {
int K=2;
for (int i = 0; i < K; i++)
{
srand(time(NULL));
int RandIndex = rand() % 10; //generates a random number between 0 and 9
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//HERE IS THE PROBLEM
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!
clusters[i].centroid = points[RandIndex];
}
return 0;
}
}
您不应该初始化群集向量的大小
int main()
{
...
int K=2;
clusters.resize(K);
for (int i = 0; i < K; i++)
...
}
它可以工作,并且不会出现错误。但是我尝试了int I=0的命令;i