Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.3 kB
6
Indexable
Never
using Microsoft.ML;
using Microsoft.ML.Data;
using Zadaca4Clustering;
using Zadaca4Clustering.Classes;

string _dataPath = Path.Combine(Environment.CurrentDirectory, "Data", "StudentDatasetBerun.csv");
string _modelPath = Path.Combine(Environment.CurrentDirectory, "Data", "StudentClusteringModel.zip");

var mlContext = new MLContext(seed: 0);

IDataView dataView = mlContext.Data.LoadFromTextFile<StudentData>(_dataPath, hasHeader: false, separatorChar: ',');

string featuresColumnName = "Features";
var pipeline = mlContext.Transforms
    .Concatenate(featuresColumnName, "P104", "P12", "P140", "P141", "P142", "P143")
    .Append(mlContext.Clustering.Trainers.KMeans(featuresColumnName, numberOfClusters: 3));

var model = pipeline.Fit(dataView);

using (var fileStream = new FileStream(_modelPath, FileMode.Create, FileAccess.Write, FileShare.Write))
{
    mlContext.Model.Save(model, dataView.Schema, fileStream);
}

var predictor = mlContext.Model.CreatePredictionEngine<StudentData, ClusterPrediciton>(model);

var prediction = predictor.Predict(TestStudentData.studentData);
Console.WriteLine($"Cluster: {prediction.ClusterID}");
Console.WriteLine($"Distances: {string.Join(" ", prediction.Distances ?? Array.Empty<float>())}");
Leave a Comment