implementing parsing and plotting
This commit is contained in:
parent
e9520af750
commit
88ed2b32a4
@ -1,2 +1,52 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
Console.WriteLine("Hello, World!");
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Globalization;
|
||||
|
||||
namespace kMeans;
|
||||
|
||||
public static class Program
|
||||
{
|
||||
private const string Coordinates = "coordinates.csv";
|
||||
private const string PlotOut = "plot.png";
|
||||
private const int K = 3;
|
||||
|
||||
private static void Main()
|
||||
{
|
||||
Console.WriteLine("k-Means-Algorithmus");
|
||||
var points = ParseCsv(Coordinates);
|
||||
PlotDiagram(points);
|
||||
}
|
||||
|
||||
private static IReadOnlyList<Point> ParseCsv(string path)
|
||||
{
|
||||
var sw = Stopwatch.StartNew();
|
||||
var result = new List<Point>();
|
||||
var lines = File.ReadLines(path);
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var current = line.Split(',');
|
||||
if (!double.TryParse(current[0], NumberStyles.Any, CultureInfo.InvariantCulture, out var x)) continue;
|
||||
if (!double.TryParse(current[1], NumberStyles.Any, CultureInfo.InvariantCulture, out var y)) continue;
|
||||
var point = new Point(x, y);
|
||||
result.Add(point);
|
||||
}
|
||||
|
||||
sw.Stop();
|
||||
Console.WriteLine($"[{nameof(ParseCsv)}] Elapsed: {sw.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void PlotDiagram(IReadOnlyList<Point> points)
|
||||
{
|
||||
var sw = Stopwatch.StartNew();
|
||||
var plot = new ScottPlot.Plot();
|
||||
plot.AddScatterPoints(points.Select(p => p.X).ToArray(), points.Select(p => p.Y).ToArray());
|
||||
plot.SaveFig(PlotOut);
|
||||
sw.Stop();
|
||||
Process.Start(new ProcessStartInfo(PlotOut) { UseShellExecute = true });
|
||||
Console.WriteLine($"[{nameof(PlotDiagram)}] Elapsed: {sw.ElapsedMilliseconds}ms");
|
||||
}
|
||||
|
||||
|
||||
private record Point(double X, double Y, Color? Color = default, bool Mean = false);
|
||||
}
|
@ -7,4 +7,14 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ScottPlot" Version="4.1.27" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="coordinates.csv">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
Loading…
x
Reference in New Issue
Block a user