-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
267 lines (234 loc) · 8.56 KB
/
Form1.cs
File metadata and controls
267 lines (234 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using CsvHelper;
using CsvHelper.Configuration;
using System.Globalization;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Drawing;
namespace KMeansClusteringTest
{
public partial class Form1 : Form
{
private int k = 3;
private List<ClusterPoint> ClusterPoints;
private List<Centroid> Centroids;
private Random rand = new Random();
string filePath = "otherData.csv";
public Form1()
{
InitializeComponent();
ClusterPoints = new List<ClusterPoint>();
LoadData();
this.Paint += new PaintEventHandler(Form1_Paint);
}
private void LoadData()
{
using (var reader = new StreamReader(filePath))
using (var csv = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = true
}))
{
var records = csv.GetRecords<dynamic>();
foreach (var record in records)
{
// Adjust these values based on your CSV format
float x = float.Parse(record.X, CultureInfo.InvariantCulture);
float y = float.Parse(record.Y, CultureInfo.InvariantCulture);
ClusterPoints.Add(new ClusterPoint(x, y, 0));
}
}
}
private async Task RunKMeans()
{
ClusterPoints = await Task.Run(() => KMeans(ClusterPoints, k));
this.Invalidate();
}
// Paint-Event zum Zeichnen der Punkte
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if (ClusterPoints.Count == 0)
return;
// Calculate bounds to scale points accordingly
float minX = ClusterPoints.Min(p => p.x);
float maxX = ClusterPoints.Max(p => p.x);
float minY = ClusterPoints.Min(p => p.y);
float maxY = ClusterPoints.Max(p => p.y);
float rangeX = maxX - minX;
float rangeY = maxY - minY;
if (rangeX == 0) rangeX = 1;
if (rangeY == 0) rangeY = 1;
float scaleX = (ClientSize.Width - 20) / rangeX;
float scaleY = (ClientSize.Height - 20) / rangeY;
foreach (var data in ClusterPoints)
{
Brush PointBrush = GetClusterColor(data.ClusterID);
float normalizedX = (data.x - minX) * scaleX + 10;
float normalizedY = (data.y - minY) * scaleY + 10;
g.FillEllipse(PointBrush, normalizedX - 5, normalizedY - 5, 10, 10);
}
}
private Brush GetClusterColor(int clusterId)
{
// Assign different colors to different clusters
switch (clusterId)
{
case 1:
return Brushes.Red;
case 2:
return Brushes.Green;
case 3:
return Brushes.Blue;
case 4:
return Brushes.Yellow;
case 5:
return Brushes.Purple;
case 6:
return Brushes.Orange;
case 7:
return Brushes.Cyan;
case 8:
return Brushes.Magenta;
case 9:
return Brushes.Brown;
case 10:
return Brushes.Pink;
default:
return GetRandomColor(clusterId); // Default to a random color for higher cluster IDs
}
}
private Brush GetRandomColor(int clusterId)
{
Random random = new Random(clusterId);
return new SolidBrush(Color.FromArgb(random.Next(256), random.Next(256), random.Next(256)));
}
private List<ClusterPoint> KMeans(List<ClusterPoint> dataset, int k)
{
foreach (var data in ClusterPoints)
{
data.ClusterID = 0;
}
Console.WriteLine("KMeans started");
Centroids = new List<Centroid>();
for (int i = 0; i < k; i++)
{
int p = rand.Next(0, dataset.Count);
while (dataset[p].ClusterID != 0)
{
p = rand.Next(0, dataset.Count);
}
dataset[p].ClusterID = i + 1;
Centroids.Add(new Centroid(dataset[p], i + 1));
}
bool centroidsChanged = true;
while (centroidsChanged)
{
centroidsChanged = false;
foreach (var data in dataset)
{
int nearestCentroidIndex = -1;
float minDistance = float.MaxValue;
for (int i = 0; i < Centroids.Count; i++)
{
var dist = getDistance(data, Centroids[i].p);
if (dist < minDistance)
{
minDistance = dist;
nearestCentroidIndex = i;
}
}
if (data.ClusterID != Centroids[nearestCentroidIndex].ClusterID)
{
data.ClusterID = Centroids[nearestCentroidIndex].ClusterID;
centroidsChanged = true;
}
}
// Recalculate centroids
foreach (var centroid in Centroids)
{
centroid.recalculate(dataset);
}
}
Console.WriteLine("KMeans finished");
return dataset;
}
private float getDistance(ClusterPoint a, ClusterPoint b)
{
float dx = b.x - a.x;
float dy = b.y - a.y;
return (float)Math.Sqrt(dx * dx + dy * dy);
}
private async void buttonRunKMeans_Click(object sender, EventArgs e)
{
if (int.TryParse(textBox1.Text, out int newK) && newK > 0)
{
k = newK; // Get k value from TextBox
await RunKMeans();
}
}
private async Task buttonRunElbowMethod_Click(object sender, EventArgs e)
{
int maxK = 10;
int optimalK = await Task.Run(() => FindOptimalK(ClusterPoints, maxK));
textBox1.Text = optimalK.ToString();
}
private int FindOptimalK(List<ClusterPoint> dataPoints, int maxK)
{
List<double> distortions = new List<double>();
for (int k = 1; k <= maxK; k++)
{
var clusters = KMeans(dataPoints, k);
distortions.Add(CalculateDistortion(dataPoints, clusters));
}
double minDistortion = distortions[0];
int optimalK = 1;
for (int i = 1; i < distortions.Count - 1; i++)
{
double slope1 = distortions[i] - distortions[i - 1];
double slope2 = distortions[i + 1] - distortions[i];
if (slope1 > 0 && slope2 < 0)
{
optimalK = i + 1;
break;
}
}
return optimalK;
}
private double CalculateDistortion(List<ClusterPoint> dataPoints, List<ClusterPoint> clusters)
{
double distortion = 0;
foreach (var point in dataPoints)
{
distortion += getDistance(point, clusters[point.ClusterID - 1]);
}
return distortion;
}
private async void button1_Click(object sender, EventArgs e)
{
if (int.TryParse(textBox1.Text, out int newK) && newK > 0)
{
k = newK;
await RunKMeans();
this.BeginInvoke(new Action(() => this.Invalidate()));
}
}
private async void button2_Click(object sender, EventArgs e)
{
await buttonRunElbowMethod_Click(sender, e);
k = int.Parse(textBox1.Text);
await RunKMeans();
this.BeginInvoke(new Action(() => this.Invalidate()));
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}