Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ginomessmer/ed2d63c9d0b4aa0fd23ddb91e9bc0131 to your computer and use it in GitHub Desktop.
Save ginomessmer/ed2d63c9d0b4aa0fd23ddb91e9bc0131 to your computer and use it in GitHub Desktop.
SkillDatingMatrixFactorization
name interest rating
Gino .NET 5
Gino C# 5
Gino Docker 5
Gino DevOps 5
Gino Software Engineering 5
Gino Java 1
Gino Spring 1
Gino Unity 3
Gino Thymeleaf 1
Gino Cloud 5
Imke Webentwicklung 5
Imke Apps 5
Imke Datenbanken 5
Imke Essen 5
Imke SCRUM 5
Imke Cloud 1
Petra Java 5
Petra Cloud 5
Petra Spring 5
Petra Thymeleaf 5
Petra Computergrafik 5
Petra Unity 5
Petra SCRUM 5
Petra Projektmanagement 5
Petra Meerschweinchen 5
Petra Datenbanken 2
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Trainers;
using System;
using System.IO;
// In machine learning, the columns that are used to make a prediction are called Features,
// and the column with the returned prediction is called the Label.
// We want to predict interest ratings => Label
// (Name, Interest) => Features ==> Predict the label
// Load
var mlContext = new MLContext();
var trainingDataPath = Path.Combine(Environment.CurrentDirectory, "interests_train.csv");
var trainingDataView = mlContext.Data.LoadFromTextFile<StudentInterest>(trainingDataPath, hasHeader: true, separatorChar: ',');
// Build and train model
var estimator = mlContext.Transforms.Conversion.MapValueToKey("nameEncoded", "Name")
.Append(mlContext.Transforms.Conversion.MapValueToKey("interestEncoded", "Interest"));
var options = new MatrixFactorizationTrainer.Options
{
MatrixColumnIndexColumnName = "nameEncoded",
MatrixRowIndexColumnName = "interestEncoded",
LabelColumnName = "Label",
NumberOfIterations = 20,
ApproximationRank = 100
};
var trainerEstimator = estimator.Append(mlContext.Recommendation().Trainers.MatrixFactorization(options));
var model = trainerEstimator.Fit(trainingDataView);
// Predict
var predictionEngine = mlContext.Model.CreatePredictionEngine<StudentInterest, StudentInterestPrediction>(model);
var testInput = new StudentInterest { Name = "Gino", Interest = "Datenbanken" };
var prediction = predictionEngine.Predict(testInput);
Console.WriteLine(prediction.Score); // -> 2,2039702 just about right
Console.ReadLine();
public class StudentInterest
{
[LoadColumn(0)]
public string Name;
[LoadColumn(1)]
public string Interest;
[LoadColumn(2)]
public float Label;
}
public class StudentInterestPrediction
{
public float Label;
public float Score;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment