public void getReport() { if (lm == null) { getMnlModel(); } Forms.RunningProcess.frmRunningProcessDialog rd = new Forms.RunningProcess.frmRunningProcessDialog(false); rd.Text = "Soft Max Nnet Results"; rd.TopLevel = true; rd.pgbProcess.Visible = false; rd.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable; rd.addMessage("Sample size = " + n.ToString()); rd.addMessage("Number of Classes = " + NumberOfClasses.ToString()); rd.addMessage("Number of Parameters = " + nvars.ToString()); rd.addMessage("RMSE = " + RMSE.ToString()); rd.addMessage("Average Error = " + AverageError.ToString()); rd.addMessage("Average Relative Error = " + AverageRelativeError.ToString()); rd.addMessage("Average Cross Entropy Error = " + AverageCrossEntropyError.ToString()); rd.addMessage("Classification Error = " + ClassificationError.ToString()); rd.addMessage("Relative Classification Error = " + RelativeClassificationError.ToString()); try { if (ModelHelper.chartingAvailable() && System.Windows.Forms.MessageBox.Show("Do you want to build probability graphs?", "Graphs", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { createRegChart(); } } catch { System.Windows.Forms.MessageBox.Show("Cannot create charts"); } rd.Show(); rd.enableClose(); }
public void getReport(double alpha) { Forms.RunningProcess.frmRunningProcessDialog rd = new Forms.RunningProcess.frmRunningProcessDialog(false); rd.Text = "Regression Results"; rd.TopLevel = true; rd.pgbProcess.Visible = false; rd.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable; rd.addMessage("Dependent field = " + DependentFieldNames[0]); rd.addMessage("Independent fields = " + String.Join(", ", IndependentFieldNames)); rd.addMessage("Sample size = " + SampleSize.ToString()); rd.addMessage("Intercept Through Origin = " + InterceptThroughOrigin.ToString()); rd.addMessage("F-statistic = " + FValue.ToString() + " p-value = " + PValue.ToString()); rd.addMessage("RMSE = " + RMSE.ToString()); rd.addMessage("R2 = " + Rsquared.ToString()); rd.addMessage("Adj-R2 = " + AdjustedRsquared.ToString() + "\n\nCoefficents and standard errors:\n"); rd.addMessage("Param: Intercept, " + String.Join(", ", IndependentFieldNames)); rd.addMessage("Coef: " + string.Join(", ", (from double d in Coefficients select d.ToString()).ToArray())); rd.addMessage("STE: " + string.Join(", ", (from double d in StandardErrors select d.ToString()).ToArray()) + "\n"); try { if (ModelHelper.chartingAvailable()) { regChart(); } } catch { System.Windows.Forms.MessageBox.Show("Cannot create charts"); } rd.Show(); rd.enableClose(); }
public string RunNMFbasedOMF(int maxEpoch, double learnRate, double regularization, int factorCount, List <double> quantizer, int topN = 0) { if (!ReadyForNumerical) { GetReadyForNumerical(); } StringBuilder log = new StringBuilder(); log.AppendLine(Utils.PrintHeading("NMF based OMF")); // NMF Prediction // Get ratings from scorer, for both train and test // R_all contains indexes of all ratings both train and test DataMatrix R_all = new DataMatrix(R_unknown.UserCount, R_unknown.ItemCount); R_all.MergeNonOverlap(R_unknown); R_all.MergeNonOverlap(R_train.IndexesOfNonZeroElements()); Utils.StartTimer(); DataMatrix R_predictedByNMF = NMF.PredictRatings(R_train, R_all, maxEpoch, learnRate, regularization, factorCount); log.AppendLine(Utils.StopTimer()); // OMF Prediction log.AppendLine(Utils.PrintHeading("Ordinal Matrix Factorization with NMF as scorer")); Utils.StartTimer(); Dictionary <Tuple <int, int>, List <double> > OMFDistributionByUserItem; DataMatrix R_predicted; log.AppendLine(OMF.PredictRatings(R_train.Matrix, R_unknown.Matrix, R_predictedByNMF.Matrix, quantizer, out R_predicted, out OMFDistributionByUserItem)); log.AppendLine(Utils.StopTimer()); // Numerical Evaluation log.AppendLine(Utils.PrintValue("RMSE", RMSE.Evaluate(R_test, R_predicted).ToString("0.0000"))); log.AppendLine(Utils.PrintValue("MAE", MAE.Evaluate(R_test, R_predicted).ToString("0.0000"))); // TopN Evaluation if (topN != 0) { var topNItemsByUser = ItemRecommendationCore.GetTopNItemsByUser(R_predicted, topN); for (int n = 1; n <= topN; n++) { log.AppendLine(Utils.PrintValue("NCDG@" + n, NCDG.Evaluate(RelevantItemsByUser, topNItemsByUser, n).ToString("0.0000"))); } for (int n = 1; n <= topN; n++) { log.AppendLine(Utils.PrintValue("MAP@" + n, MAP.Evaluate(RelevantItemsByUser, topNItemsByUser, n).ToString("0.0000"))); } } // Save OMFDistribution to file if (!File.Exists(GetDataFileName("RatingOMF_"))) { Utils.IO <Dictionary <Tuple <int, int>, List <double> > > .SaveObject(OMFDistributionByUserItem, GetDataFileName("RatingOMF_")); } return(log.ToString()); }
public void RMSETest() { /* * 5 3 0 1 * 4 0 0 1 * 1 1 0 5 * 1 0 0 4 * 0 1 5 4 */ DataMatrix R = GetSampleRatingMatrix(); /* * 3 3 0 1 * 4 0 0 5 * 1 2 0 5 * 1 0 0 4 * 0 1 5 4 */ DataMatrix R_predicted = GetSampleRatingMatrix(); R_predicted[0, 0] = 3; // was 5 R_predicted[2, 1] = 2; // was 1 R_predicted[1, 3] = 5; // was 1 double debug1 = RMSE.Evaluate(R, R_predicted); double debug2 = Math.Sqrt(21.0 / 13.0); Debug.Assert(debug1 == debug2); }
private ResultModel item_predict(string pathFile) { ResultModel result = new ResultModel(); // load data Matrix <double> data = DelimitedReader.Read <double>(pathFile, false, "\t", false, System.Globalization.CultureInfo.InvariantCulture.NumberFormat); result.raw_data = data; int n_users = data.Column(0).Distinct().Count(); int n_items = data.Column(1).Distinct().Count(); double test_split = 0.1; double train_split = 1 - test_split; int train_row = (int)(data.RowCount * train_split); int train_col = data.ColumnCount; int test_row = data.RowCount - train_row; int test_col = train_col; Matrix <double> train_data = DenseMatrix.Create(train_row, train_col, 0); Matrix <double> test_data = DenseMatrix.Create(test_row, test_col, 0); train_data = data.SubMatrix(0, train_row, 0, train_col); test_data = data.SubMatrix(train_row, test_row, 0, test_col); // Create two user-item matrices, one for training and another for testing Matrix <double> train_data_matrix = DenseMatrix.Create(n_users, n_items, 0); for (int line = 0; line < train_data.RowCount; line++) { int userId = (int)(train_data[line, 0]); int itemId = (int)(train_data[line, 1] - 1); train_data_matrix[userId, itemId] = train_data[line, 2]; } Matrix <double> test_data_matrix = DenseMatrix.Create(n_users, n_items, 0); for (int line = 0; line < test_data.RowCount; line++) { int userId = (int)(test_data[line, 0]); int itemId = (int)(test_data[line, 1] - 1); test_data_matrix[userId, itemId] = test_data[line, 2]; } result.train_data = train_data_matrix; result.test_data = test_data_matrix; // consine similarity calculate Matrix <double> item_similarity = Recommender_System.cosin(train_data_matrix.Transpose()); // predict Matrix <double> item_prediction = Recommender_System.predict(train_data_matrix, item_similarity, "item"); result.predic_data = item_prediction; // rmse error double item_rmse = RMSE.calRMSE(item_prediction, test_data_matrix); result.rmse = item_rmse; return(result); }
public string RunNMFbasedORF(double regularization, double learnRate, int maxEpoch, List <double> quantizer, int topN = 0) { // Load OMFDistribution from file Dictionary <Tuple <int, int>, List <double> > OMFDistributionByUserItem; if (File.Exists(GetDataFileName("RatingOMF_"))) { OMFDistributionByUserItem = Utils.IO <Dictionary <Tuple <int, int>, List <double> > > .LoadObject(GetDataFileName("RatingOMF_")); } else { return("Abort, Run OMF first."); } if (!ReadyForNumerical) { GetReadyForNumerical(); } StringBuilder log = new StringBuilder(); log.AppendLine(Utils.PrintHeading("NMF based ORF")); // Prediction Utils.StartTimer(); DataMatrix R_predicted_expectations; DataMatrix R_predicted_mostlikely; ORF orf = new ORF(); orf.PredictRatings(R_train, R_unknown, StrongSimilarityIndicatorsByItemRating, OMFDistributionByUserItem, regularization, learnRate, maxEpoch, quantizer.Count, out R_predicted_expectations, out R_predicted_mostlikely); log.AppendLine(Utils.StopTimer()); // Numerical Evaluation log.AppendLine(Utils.PrintValue("RMSE", RMSE.Evaluate(R_test, R_predicted_expectations).ToString("0.0000"))); log.AppendLine(Utils.PrintValue("MAE", RMSE.Evaluate(R_test, R_predicted_mostlikely).ToString("0.0000"))); // Top-N Evaluation if (topN != 0) { var topNItemsByUser_expectations = ItemRecommendationCore.GetTopNItemsByUser(R_predicted_expectations, topN); for (int n = 1; n <= topN; n++) { log.AppendLine(Utils.PrintValue("NCDG@" + n, NCDG.Evaluate(RelevantItemsByUser, topNItemsByUser_expectations, n).ToString("0.0000"))); } for (int n = 1; n <= topN; n++) { log.AppendLine(Utils.PrintValue("MAP@" + n, MAP.Evaluate(RelevantItemsByUser, topNItemsByUser_expectations, n).ToString("0.0000"))); } } return(log.ToString()); }
public void RMSETest() { var actual = new Matrix(100, 1); var expected = new Matrix(100, 1); actual.InRandomize(0.25, 0.75); expected.InRandomize(0.25, 0.75); var metric = new RMSE(); var e = metric.Evaluate(actual, expected); var val = Math.Sqrt(new MSE().Evaluate(actual, expected)); Assert.IsTrue(Math.Abs(e - val) < 0.01, metric.Type().ToString() + " Evaluate."); }
public string writeModel(string outModelPath) { if (lm == null) { getMnlModel(); } outPath = outModelPath; double[,] coef = null; alglib.mnlunpack(lm, out coef, out nvars, out nclasses); using (System.IO.StreamWriter sw = new System.IO.StreamWriter(outPath)) { sw.WriteLine(modelTypes.SoftMax.ToString()); sw.WriteLine(InTablePath); sw.WriteLine(SampleSize.ToString()); sw.WriteLine(String.Join(",", IndependentFieldNames)); sw.WriteLine(String.Join(",", DependentFieldNames)); sw.WriteLine(String.Join(",", ClassFieldNames)); sw.WriteLine(string.Join(",", Categories)); sw.WriteLine(NumberOfVariables.ToString()); sw.WriteLine(NumberOfClasses.ToString()); sw.WriteLine(RMSE.ToString()); sw.WriteLine(AverageCrossEntropyError.ToString()); sw.WriteLine(AverageError.ToString()); sw.WriteLine(AverageRelativeError.ToString()); sw.WriteLine(ClassificationError.ToString()); sw.WriteLine(RelativeClassificationError.ToString()); sw.WriteLine(String.Join(",", (from double d in minValues select d.ToString()).ToArray())); sw.WriteLine(String.Join(",", (from double d in maxValues select d.ToString()).ToArray())); sw.WriteLine(String.Join(",", (from double d in sumValues select d.ToString()).ToArray())); int rws = coef.GetUpperBound(1); int clms = coef.GetUpperBound(0); for (int r = 0; r <= rws; r++) { string[] ln = new string[clms + 1]; for (int c = 0; c <= clms; c++) { ln[c] = coef[c, r].ToString(); } sw.WriteLine(String.Join(" ", ln)); } sw.Close(); } return(outPath); }
/// <summary> /// Add to result /// </summary> /// <param name="weights">double[] - weights</param> /// <param name="sse">double[] - sum square errors</param> /// <param name="rmse">double[] - rmse's</param> public void Add(double[] weights, double[] sse, double[] rmse) { if (ResultWeights == null) { ResultWeights = new List <double[]>(); } ResultWeights.Add(weights); if (SSE == null) { SSE = new List <double[]>(); } SSE.Add(sse); if (RMSE == null) { RMSE = new List <double[]>(); } RMSE.Add(rmse); }
/// <summary> /// Rating based Non-negative Matrix Factorization /// </summary> public string RunNMF(int maxEpoch, double learnRate, double regularization, int factorCount, int topN = 0) { if (!ReadyForNumerical) { GetReadyForNumerical(); } StringBuilder log = new StringBuilder(); log.AppendLine(Utils.PrintHeading("NMF")); // Prediction Utils.StartTimer(); DataMatrix R_predicted = NMF.PredictRatings(R_train, R_unknown, maxEpoch, learnRate, regularization, factorCount); log.AppendLine(Utils.StopTimer()); // Numerical Evaluation log.AppendLine(Utils.PrintValue("RMSE", RMSE.Evaluate(R_test, R_predicted).ToString("0.0000"))); log.AppendLine(Utils.PrintValue("MAE", MAE.Evaluate(R_test, R_predicted).ToString("0.0000"))); // TopN Evaluation if (topN != 0) { var topNItemsByUser = ItemRecommendationCore.GetTopNItemsByUser(R_predicted, topN); for (int n = 1; n <= topN; n++) { log.AppendLine(Utils.PrintValue("NCDG@" + n, NCDG.Evaluate(RelevantItemsByUser, topNItemsByUser, n).ToString("0.0000"))); } for (int n = 1; n <= topN; n++) { log.AppendLine(Utils.PrintValue("MAP@" + n, MAP.Evaluate(RelevantItemsByUser, topNItemsByUser, n).ToString("0.0000"))); } } return(log.ToString()); }
/// <summary> /// Predict all unknown values as global mean rating. /// </summary> public string RunGlobalMean() { if (!ReadyForNumerical) { GetReadyForNumerical(); } StringBuilder log = new StringBuilder(); log.AppendLine(Utils.PrintHeading("Global Mean")); // Prediction Utils.StartTimer(); double globalMean = R_train.GetGlobalMean(); DataMatrix R_predicted = R_unknown.Multiply(globalMean); log.AppendLine(Utils.StopTimer()); // Numerical Evaluation log.AppendLine(Utils.PrintValue("RMSE", RMSE.Evaluate(R_test, R_predicted).ToString("0.0000"))); log.AppendLine(Utils.PrintValue("MAE", MAE.Evaluate(R_test, R_predicted).ToString("0.0000"))); return(log.ToString()); }
public string RunUserKNN(int topN = 0) { if (!ReadyForNumerical) { GetReadyForNumerical(); } StringBuilder log = new StringBuilder(); log.AppendLine(Utils.PrintHeading("UserKNN")); // Prediction Utils.StartTimer(); DataMatrix R_predicted = Numerical.UserKNN.PredictRatings(R_train, R_unknown, UserSimilaritiesOfRating, MaxCountOfNeighbors); log.AppendLine(Utils.StopTimer()); // Numerical Evaluation log.AppendLine(Utils.PrintValue("RMSE", RMSE.Evaluate(R_test, R_predicted).ToString("0.0000"))); log.AppendLine(Utils.PrintValue("MAE", MAE.Evaluate(R_test, R_predicted).ToString("0.0000"))); // TopN Evaluation if (topN != 0) { var topNItemsByUser = ItemRecommendationCore.GetTopNItemsByUser(R_predicted, topN); for (int n = 1; n <= topN; n++) { Utils.PrintValue("NCDG@" + n, NCDG.Evaluate(RelevantItemsByUser, topNItemsByUser, n).ToString("0.0000")); } for (int n = 1; n <= topN; n++) { log.AppendLine(Utils.PrintValue("MAP@" + n, MAP.Evaluate(RelevantItemsByUser, topNItemsByUser, n).ToString("0.0000"))); } } return(log.ToString()); }