Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 02e4102

Browse files
Update kmeans.cpp
1 parent b563811 commit 02e4102

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

‎kmeans.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Point
2121

2222
for (int i = 0; i < (int)line.length(); i++)
2323
{
24-
if ((48 <= int(line[i]) && int(line[i]) <= 57) || line[i] == '.')
24+
if ((48 <= int(line[i]) && int(line[i]) <= 57) || line[i] == '.' || line[i] == '+' || line[i] == '-' || line[i] == 'e')
2525
{
2626
tmp += line[i];
2727
}
@@ -118,6 +118,7 @@ class KMeans
118118
private:
119119
int K, iters, dimensions, total_points;
120120
vector<Cluster> clusters;
121+
string output_dir;
121122

122123
void clearClusters()
123124
{
@@ -135,9 +136,11 @@ class KMeans
135136
for (int i = 0; i < dimensions; i++)
136137
{
137138
sum += pow(clusters[0].getCentroidByPos(i) - point.getVal(i), 2.0);
139+
// sum += abs(clusters[0].getCentroidByPos(i) - point.getVal(i));
138140
}
139141

140142
min_dist = sqrt(sum);
143+
// min_dist = sum;
141144
NearestClusterId = clusters[0].getId();
142145

143146
for (int i = 1; i < K; i++)
@@ -148,9 +151,11 @@ class KMeans
148151
for (int j = 0; j < dimensions; j++)
149152
{
150153
sum += pow(clusters[i].getCentroidByPos(j) - point.getVal(j), 2.0);
154+
// sum += abs(clusters[i].getCentroidByPos(j) - point.getVal(j));
151155
}
152156

153157
dist = sqrt(sum);
158+
// dist = sum;
154159

155160
if (dist < min_dist)
156161
{
@@ -163,10 +168,11 @@ class KMeans
163168
}
164169

165170
public:
166-
KMeans(int K, int iterations)
171+
KMeans(int K, int iterations, string output_dir)
167172
{
168173
this->K = K;
169174
this->iters = iterations;
175+
this->output_dir = output_dir;
170176
}
171177

172178
void run(vector<Point> &all_points)
@@ -259,7 +265,7 @@ class KMeans
259265
}
260266

261267
ofstream pointsFile;
262-
pointsFile.open("points.txt", ios::out);
268+
pointsFile.open(output_dir + "/" + to_string(K) + "-points.txt", ios::out);
263269

264270
for (int i = 0; i < total_points; i++)
265271
{
@@ -270,7 +276,7 @@ class KMeans
270276

271277
// Write cluster centers to file
272278
ofstream outfile;
273-
outfile.open("clusters.txt");
279+
outfile.open(output_dir + "/" + to_string(K) + "-clusters.txt");
274280
if (outfile.is_open())
275281
{
276282
for (int i = 0; i < K; i++)
@@ -295,13 +301,15 @@ class KMeans
295301

296302
int main(int argc, char **argv)
297303
{
298-
// Need 2 arguments (except filename) to run, else exit
299-
if (argc != 3)
304+
// Need 3 arguments (except filename) to run, else exit
305+
if (argc != 4)
300306
{
301-
cout << "Error: command-line argument count mismatch.";
307+
cout << "Error: command-line argument count mismatch.\n ./kmeans <INPUT> <K> <OUT-DIR>" << endl;
302308
return 1;
303309
}
304310

311+
string output_dir = argv[3];
312+
305313
// Fetching number of clusters
306314
int K = atoi(argv[2]);
307315

@@ -325,7 +333,6 @@ int main(int argc, char **argv)
325333
Point point(pointId, line);
326334
all_points.push_back(point);
327335
pointId++;
328-
cout << pointId << endl;
329336
}
330337

331338
infile.close();
@@ -342,7 +349,7 @@ int main(int argc, char **argv)
342349
// Running K-Means Clustering
343350
int iters = 100;
344351

345-
KMeans kmeans(K, iters);
352+
KMeans kmeans(K, iters, output_dir);
346353
kmeans.run(all_points);
347354

348355
return 0;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /