The style of recommendation in this sample is based upon the co-purchase scenario or products frequently bought together which means it will recommend customers a set of products based upon their purchase order history.
We build and train an ML model on own training data, evaluate how good it is (analyzing the obtained metrics), and lastly you can consume/test the model to predict the demand given input data variables.
ITransformer model = pipeline.Fit(partitions.TrainSet);
var predictionengine = _mlContext.Model.CreatePredictionEngine
//get product id from shoppong cart
var product_from_shoppingcart = _shoppingcart.ShoppingCarts.Select(s => s.ProductId).ToList();
//create empty list of product recommender, wich contain: Product, Score, Recommended for
List<Recommender >recommenderList = new List<Recommender>();
foreach (var x in product_from_shoppingcart) {
//find top 1 product for product from shopping cart
var top = (from m in Enumerable.Range(1, p_count)
//p_count - count of all products in DB
let p = predictionengine.Predict(
new ProductInfo(){
ProductID = (uint)x,//product ID from shopping cart
CombinedProductID = (uint)m //recommended product ID
})
orderby p.Score descending
//take one recommended product ID with higher score
select (ProductId: m, p.Score)).Take(1);
//get product from DB by product ID
foreach (var (ProductId, Score) in top){
var recommendFor = _productrepository.GetProducts.SingleOrDefault(obj => obj.ProductId == x)?.Name;
var recomm = new Recommender(){
Rec_product = _productrepository.GetProducts.FirstOrDefault(p => p.ProductId == ProductId),
P_score = Score,
RecommendFor = recommendFor
};
//add recommended product to list of product
recommenderList.Add(recomm);
}
}
The score produced by matrix factorization represents the likelihood of being a positive case. The larger the score value, the higher probability of being a positive case. However, the score doesn't carry any probability information. When making a prediction, you will have to compute multiple merchandises' scores and pick up the merchandise with the highest score.
Top