本文整理匯總了Java中net.recommenders.rival.core.SimpleParser類的典型用法代碼示例。如果您正苦於以下問題:Java SimpleParser類的具體用法?Java SimpleParser怎麽用?Java SimpleParser使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
SimpleParser類屬於net.recommenders.rival.core包,在下文中一共展示了SimpleParser類的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getIndexMap
import net.recommenders.rival.core.SimpleParser; //導入依賴的package包/類
/**
* Read a user/item mapping (user/item original value, user/item internal
* id) from a file and return the maximum index number in that file.
*
* @param in The file with id mapping.
* @param map The user/item mapping
* @return The largest id number.
* @throws IOException if file does not exist.
*/
public static long getIndexMap(final File in, final Map<String, Long> map) throws IOException {
long id = 0;
if (in.exists()) {
BufferedReader br = SimpleParser.getBufferedReader(in);
String line;
while ((line = br.readLine()) != null) {
String[] toks = line.split("\t");
long i = Long.parseLong(toks[1]);
map.put(toks[0], i);
id = Math.max(i, id);
}
br.close();
}
return id + 1;
}
示例2: evaluate
import net.recommenders.rival.core.SimpleParser; //導入依賴的package包/類
/**
* Evaluates the recommendations generated in previous steps.
*
* @param splitPath path where splits have been stored
* @param recPath path where recommendation files have been stored
*/
public static void evaluate(final String splitPath, final String recPath) {
double ndcgRes = 0.0;
double precisionRes = 0.0;
double rmseRes = 0.0;
int i = 0;
File testFile = new File(splitPath + "test_" + i + ".csv");
File recFile = new File(recPath + "recs_" + i + ".csv");
DataModel<Long, Long> testModel = null;
DataModel<Long, Long> recModel = null;
try {
testModel = new SimpleParser().parseData(testFile);
recModel = new SimpleParser().parseData(recFile);
} catch (IOException e) {
e.printStackTrace();
}
NDCG<Long, Long> ndcg = new NDCG<Long, Long>(recModel, testModel, new int[]{AT});
ndcg.compute();
ndcgRes += ndcg.getValueAt(AT);
RMSE<Long, Long> rmse = new RMSE<Long, Long>(recModel, testModel);
rmse.compute();
rmseRes += rmse.getValue();
Precision<Long, Long> precision = new Precision<Long, Long>(recModel, testModel, REL_TH, new int[]{AT});
precision.compute();
precisionRes += precision.getValueAt(AT);
System.out.println("[email protected]" + AT + ": " + ndcgRes);
System.out.println("RMSE: " + rmseRes);
System.out.println("[email protected]" + AT + ": " + precisionRes);
}
示例3: evaluate
import net.recommenders.rival.core.SimpleParser; //導入依賴的package包/類
/**
* Evaluates the recommendations generated in previous steps.
*
* @param nFolds number of folds
* @param splitPath path where splits have been stored
* @param recPath path where recommendation files have been stored
*/
public static void evaluate(final int nFolds, final String splitPath, final String recPath) {
double ndcgRes = 0.0;
double precisionRes = 0.0;
double rmseRes = 0.0;
for (int i = 0; i < nFolds; i++) {
File testFile = new File(splitPath + "test_" + i + ".csv");
File recFile = new File(recPath + "recs_" + i + ".csv");
DataModel<Long, Long> testModel = null;
DataModel<Long, Long> recModel = null;
try {
testModel = new SimpleParser().parseData(testFile);
recModel = new SimpleParser().parseData(recFile);
} catch (IOException e) {
e.printStackTrace();
}
NDCG ndcg = new NDCG(recModel, testModel, new int[]{AT});
ndcg.compute();
ndcgRes += ndcg.getValueAt(AT);
RMSE rmse = new RMSE(recModel, testModel);
rmse.compute();
rmseRes += rmse.getValue();
Precision precision = new Precision(recModel, testModel, REL_TH, new int[]{AT});
precision.compute();
precisionRes += precision.getValueAt(AT);
}
System.out.println("[email protected]" + AT + ": " + ndcgRes / nFolds);
System.out.println("RMSE: " + rmseRes / nFolds);
System.out.println("[email protected]" + AT + ": " + precisionRes / nFolds);
}
示例4: Evaluator
import net.recommenders.rival.core.SimpleParser; //導入依賴的package包/類
/**
* Constructor for the evaluator.
* @param testsetPath The path to the file containing the testset.
* @param predictionsetPath The path to the file containing the predictions.
* @throws IOException When the files at either @testsetPath or @predictionsetPath do not exist.
*/
public Evaluator(String testsetPath, String predictionsetPath) throws IOException{
File testFile = new File(testsetPath);
File predictionFile = new File(predictionsetPath);
SimpleParser testParser = new SimpleParser();
this.test = testParser.parseData(testFile, ",");
this.predictions = testParser.parseData(predictionFile, ",");
this.ndcg = new NDCG(predictions, test, new int[]{AT});
}
示例5: run
import net.recommenders.rival.core.SimpleParser; //導入依賴的package包/類
/**
* Runs a single evaluation metric.
*
* @param properties The properties of the strategy.
* @throws IOException if recommendation file is not found or output cannot
* be written (see {@link #generateOutput(net.recommenders.rival.core.DataModelIF, int[],
* net.recommenders.rival.evaluation.metric.EvaluationMetric, java.lang.String, java.lang.Boolean, java.io.File, java.lang.Boolean, java.lang.Boolean)})
* @throws ClassNotFoundException see {@link #instantiateEvaluationMetric(java.util.Properties, net.recommenders.rival.core.DataModelIF, net.recommenders.rival.core.DataModelIF)}
* @throws IllegalAccessException see {@link #instantiateEvaluationMetric(java.util.Properties, net.recommenders.rival.core.DataModelIF, net.recommenders.rival.core.DataModelIF)}
* @throws InstantiationException see {@link #instantiateEvaluationMetric(java.util.Properties, net.recommenders.rival.core.DataModelIF, net.recommenders.rival.core.DataModelIF)}
* @throws InvocationTargetException see {@link #instantiateEvaluationMetric(java.util.Properties, net.recommenders.rival.core.DataModelIF, net.recommenders.rival.core.DataModelIF)}
* @throws NoSuchMethodException see {@link #instantiateEvaluationMetric(java.util.Properties, net.recommenders.rival.core.DataModelIF, net.recommenders.rival.core.DataModelIF)}
*/
@SuppressWarnings("unchecked")
public static void run(final Properties properties)
throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
System.out.println("Parsing started: recommendation file");
File recommendationFile = new File(properties.getProperty(PREDICTION_FILE));
DataModelIF<Long, Long> predictions;
EvaluationStrategy.OUTPUT_FORMAT recFormat;
if (properties.getProperty(PREDICTION_FILE_FORMAT).equals(EvaluationStrategy.OUTPUT_FORMAT.TRECEVAL.toString())) {
recFormat = EvaluationStrategy.OUTPUT_FORMAT.TRECEVAL;
} else {
recFormat = EvaluationStrategy.OUTPUT_FORMAT.SIMPLE;
}
switch (recFormat) {
case SIMPLE:
predictions = new SimpleParser().parseData(recommendationFile);
break;
case TRECEVAL:
predictions = new TrecEvalParser().parseData(recommendationFile);
break;
default:
throw new AssertionError();
}
System.out.println("Parsing finished: recommendation file");
System.out.println("Parsing started: test file");
File testFile = new File(properties.getProperty(TEST_FILE));
DataModelIF<Long, Long> testModel = new SimpleParser().parseData(testFile);
System.out.println("Parsing finished: test file");
// read other parameters
Boolean overwrite = Boolean.parseBoolean(properties.getProperty(OUTPUT_OVERWRITE, "false"));
Boolean doAppend = Boolean.parseBoolean(properties.getProperty(OUTPUT_APPEND, "true"));
Boolean perUser = Boolean.parseBoolean(properties.getProperty(METRIC_PER_USER, "false"));
File resultsFile = new File(properties.getProperty(OUTPUT_FILE));
// get metric
EvaluationMetric<Long> metric = instantiateEvaluationMetric(properties, predictions, testModel);
// get ranking cutoffs
int[] rankingCutoffs = getRankingCutoffs(properties);
// generate output
generateOutput(testModel, rankingCutoffs, metric, metric.getClass().getSimpleName(), perUser, resultsFile, overwrite, doAppend);
}
示例6: parseTemporalData
import net.recommenders.rival.core.SimpleParser; //導入依賴的package包/類
/**
* {@inheritDoc}
*/
@Override
public TemporalDataModelIF<Long, Long> parseTemporalData(final File f) throws IOException {
TemporalDataModelIF<Long, Long> dataset = DataModelFactory.getDefaultTemporalModel();
BufferedReader br = SimpleParser.getBufferedReader(f);
String line;
while ((line = br.readLine()) != null) {
parseLine(line, dataset);
}
br.close();
return dataset;
}
示例7: evaluate
import net.recommenders.rival.core.SimpleParser; //導入依賴的package包/類
/**
* Evaluates the recommendations generated in previous steps.
*
* @param nFolds number of folds
* @param splitPath path where splits have been stored
* @param recPath path where recommendation files have been stored
*/
public static void evaluate(final int nFolds, final String splitPath, final String recPath) {
double ndcgRes = 0.0;
double precisionRes = 0.0;
double rmseRes = 0.0;
for (int i = 0; i < nFolds; i++) {
File testFile = new File(splitPath + "test_" + i + ".csv");
File recFile = new File(recPath + "recs_" + i + ".csv");
DataModelIF<Long, Long> testModel = null;
DataModelIF<Long, Long> recModel = null;
try {
testModel = new SimpleParser().parseData(testFile);
recModel = new SimpleParser().parseData(recFile);
} catch (IOException e) {
e.printStackTrace();
}
NDCG<Long, Long> ndcg = new NDCG<>(recModel, testModel, new int[]{AT});
ndcg.compute();
ndcgRes += ndcg.getValueAt(AT);
RMSE<Long, Long> rmse = new RMSE<>(recModel, testModel);
rmse.compute();
rmseRes += rmse.getValue();
Precision<Long, Long> precision = new Precision<>(recModel, testModel, REL_TH, new int[]{AT});
precision.compute();
precisionRes += precision.getValueAt(AT);
}
System.out.println("[email protected]" + AT + ": " + ndcgRes / nFolds);
System.out.println("RMSE: " + rmseRes / nFolds);
System.out.println("[email protected]" + AT + ": " + precisionRes / nFolds);
}
示例8: evaluate
import net.recommenders.rival.core.SimpleParser; //導入依賴的package包/類
/**
* Evaluates the recommendations generated in previous steps.
*
* @param splitPath path where splits have been stored
* @param recPath path where recommendation files have been stored
*/
public static void evaluate(final String splitPath, final String recPath) {
double ndcgRes = 0.0;
double precisionRes = 0.0;
double rmseRes = 0.0;
int i = 0;
File testFile = new File(splitPath + "test_" + i + ".csv");
File recFile = new File(recPath + "recs_" + i + ".csv");
DataModelIF<Long, Long> testModel = null;
DataModelIF<Long, Long> recModel = null;
try {
testModel = new SimpleParser().parseData(testFile);
recModel = new SimpleParser().parseData(recFile);
} catch (IOException e) {
e.printStackTrace();
}
NDCG<Long, Long> ndcg = new NDCG<Long, Long>(recModel, testModel, new int[]{AT});
ndcg.compute();
ndcgRes += ndcg.getValueAt(AT);
RMSE<Long, Long> rmse = new RMSE<Long, Long>(recModel, testModel);
rmse.compute();
rmseRes += rmse.getValue();
Precision<Long, Long> precision = new Precision<Long, Long>(recModel, testModel, REL_TH, new int[]{AT});
precision.compute();
precisionRes += precision.getValueAt(AT);
System.out.println("[email protected]" + AT + ": " + ndcgRes);
System.out.println("RMSE: " + rmseRes);
System.out.println("[email protected]" + AT + ": " + precisionRes);
}
示例9: evaluate
import net.recommenders.rival.core.SimpleParser; //導入依賴的package包/類
/**
* Evaluates the recommendations generated in previous steps.
*
* @param splitPath path where splits have been stored
* @param recPath path where recommendation files have been stored
*/
public static void evaluate(final String splitPath, final String recPath) {
double ndcgRes = 0.0;
double precisionRes = 0.0;
double rmseRes = 0.0;
int i = 0;
File testFile = new File(splitPath + "test_" + i + ".csv");
File recFile = new File(recPath + "recs_" + i + ".csv");
DataModelIF<Long, Long> testModel = null;
DataModelIF<Long, Long> recModel = null;
try {
testModel = new SimpleParser().parseData(testFile);
recModel = new SimpleParser().parseData(recFile);
} catch (IOException e) {
e.printStackTrace();
}
NDCG<Long, Long> ndcg = new NDCG<>(recModel, testModel, new int[]{AT});
ndcg.compute();
ndcgRes += ndcg.getValueAt(AT);
RMSE<Long, Long> rmse = new RMSE<>(recModel, testModel);
rmse.compute();
rmseRes += rmse.getValue();
Precision<Long, Long> precision = new Precision<>(recModel, testModel, REL_TH, new int[]{AT});
precision.compute();
precisionRes += precision.getValueAt(AT);
System.out.println("[email protected]" + AT + ": " + ndcgRes);
System.out.println("RMSE: " + rmseRes);
System.out.println("[email protected]" + AT + ": " + precisionRes);
}
示例10: evaluate
import net.recommenders.rival.core.SimpleParser; //導入依賴的package包/類
/**
* Evaluates the recommendations generated in previous steps.
*
* @param splitPath path where splits have been stored
* @param recPath path where recommendation files have been stored
*/
public static void evaluate(final String splitPath, final String recPath) {
double ndcgRes = 0.0;
double precisionRes = 0.0;
double rmseRes = 0.0;
int i = 0;
File testFile = new File(splitPath + "test_" + i + ".csv");
File recFile = new File(recPath + "recs_" + i + ".csv");
DataModelIF<Long, Long> testModel = null;
DataModelIF<Long, Long> recModel = null;
try {
testModel = new SimpleParser().parseData(testFile);
recModel = new SimpleParser().parseData(recFile);
} catch (IOException e) {
e.printStackTrace();
}
NDCG<Long, Long> ndcg = new NDCG<>(recModel, testModel, new int[]{AT});
ndcg.compute();
ndcgRes += ndcg.getValueAt(AT);
RMSE<Long, Long> rmse = new RMSE<>(recModel, testModel);
rmse.compute();
rmseRes += rmse.getValue();
Precision<Long, Long> precision = new Precision<>(recModel, testModel, REL_TH, new int[]{AT});
precision.compute();
precisionRes += precision.getValueAt(AT);
System.out.println("[email protected]" + AT + ": " + ndcgRes);
System.out.println("RMSE: " + rmseRes);
System.out.println("[email protected]" + AT + ": " + precisionRes);
}
示例11: run
import net.recommenders.rival.core.SimpleParser; //導入依賴的package包/類
/**
* Runs multiple evaluation metrics.
*
* @param properties The properties of the strategy.
* @throws IOException if test file or prediction file are not found or
* output cannot be generated (see {@link net.recommenders.rival.core.Parser#parseData(java.io.File)}
* and {@link EvaluationMetricRunner#generateOutput(net.recommenders.rival.core.DataModelIF, int[],
* net.recommenders.rival.evaluation.metric.EvaluationMetric, java.lang.String, java.lang.Boolean, java.io.File, java.lang.Boolean, java.lang.Boolean)}).
* @throws ClassNotFoundException see
* {@link EvaluationMetricRunner#instantiateEvaluationMetric(java.util.Properties, net.recommenders.rival.core.DataModelIF, net.recommenders.rival.core.DataModelIF)}
* @throws IllegalAccessException see
* {@link EvaluationMetricRunner#instantiateEvaluationMetric(java.util.Properties, net.recommenders.rival.core.DataModelIF, net.recommenders.rival.core.DataModelIF)}
* @throws InstantiationException see
* {@link EvaluationMetricRunner#instantiateEvaluationMetric(java.util.Properties, net.recommenders.rival.core.DataModelIF, net.recommenders.rival.core.DataModelIF)}
* @throws InvocationTargetException see
* {@link EvaluationMetricRunner#instantiateEvaluationMetric(java.util.Properties, net.recommenders.rival.core.DataModelIF, net.recommenders.rival.core.DataModelIF)}
* @throws NoSuchMethodException see
* {@link EvaluationMetricRunner#instantiateEvaluationMetric(java.util.Properties, net.recommenders.rival.core.DataModelIF, net.recommenders.rival.core.DataModelIF)}
*/
@SuppressWarnings("unchecked")
public static void run(final Properties properties)
throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
EvaluationStrategy.OUTPUT_FORMAT recFormat;
if (properties.getProperty(PREDICTION_FILE_FORMAT).equals(EvaluationStrategy.OUTPUT_FORMAT.TRECEVAL.toString())) {
recFormat = EvaluationStrategy.OUTPUT_FORMAT.TRECEVAL;
} else {
recFormat = EvaluationStrategy.OUTPUT_FORMAT.SIMPLE;
}
System.out.println("Parsing started: test file");
File testFile = new File(properties.getProperty(TEST_FILE));
DataModelIF<Long, Long> testModel = new SimpleParser().parseData(testFile);
System.out.println("Parsing finished: test file");
File predictionsFolder = new File(properties.getProperty(PREDICTION_FOLDER));
String predictionsPrefix = properties.getProperty(PREDICTION_PREFIX);
Set<String> predictionFiles = new HashSet<>();
getAllPredictionFiles(predictionFiles, predictionsFolder, predictionsPrefix);
// read other parameters
Boolean overwrite = Boolean.parseBoolean(properties.getProperty(OUTPUT_OVERWRITE, "false"));
Boolean doAppend = Boolean.parseBoolean(properties.getProperty(OUTPUT_APPEND, "true"));
Boolean perUser = Boolean.parseBoolean(properties.getProperty(METRIC_PER_USER, "false"));
int[] rankingCutoffs = EvaluationMetricRunner.getRankingCutoffs(properties);
// process info for each result file
File resultsFolder = new File(properties.getProperty(OUTPUT_FOLDER));
for (String file : predictionFiles) {
File predictionFile = new File(predictionsPrefix + file);
System.out.println("Parsing started: recommendation file");
DataModelIF<Long, Long> predictions;
switch (recFormat) {
case SIMPLE:
predictions = new SimpleParser().parseData(predictionFile);
break;
case TRECEVAL:
predictions = new TrecEvalParser().parseData(predictionFile);
break;
default:
throw new AssertionError();
}
System.out.println("Parsing finished: recommendation file");
File resultsFile = new File(resultsFolder, "eval" + "__" + predictionFile.getName());
// get metrics
for (EvaluationMetric<Long> metric : instantiateEvaluationMetrics(properties, predictions, testModel)) {
// generate output
EvaluationMetricRunner.generateOutput(testModel, rankingCutoffs, metric, metric.getClass().getSimpleName(), perUser, resultsFile, overwrite, doAppend);
}
}
}
示例12: run
import net.recommenders.rival.core.SimpleParser; //導入依賴的package包/類
/**
* Process the property file and runs the specified strategies on some data.
*
* @param properties The property file
* @throws IOException when a file cannot be parsed
* @throws ClassNotFoundException when {@link Class#forName(java.lang.String)}
* fails
* @throws IllegalAccessException when {@link java.lang.reflect.Constructor#newInstance(java.lang.Object[])}
* fails
* @throws InstantiationException when {@link java.lang.reflect.Constructor#newInstance(java.lang.Object[])}
* fails
* @throws InvocationTargetException when {@link java.lang.reflect.Constructor#newInstance(java.lang.Object[])}
* fails
* @throws NoSuchMethodException when {@link Class#getConstructor(java.lang.Class[])}
* fails
*/
public static void run(final Properties properties)
throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
// read splits
System.out.println("Parsing started: training file");
File trainingFile = new File(properties.getProperty(TRAINING_FILE));
DataModelIF<Long, Long> trainingModel = new SimpleParser().parseData(trainingFile);
System.out.println("Parsing finished: training file");
System.out.println("Parsing started: test file");
File testFile = new File(properties.getProperty(TEST_FILE));
DataModelIF<Long, Long> testModel = new SimpleParser().parseData(testFile);
System.out.println("Parsing finished: test file");
// read other parameters
File inputFile = new File(properties.getProperty(INPUT_FILE));
Boolean overwrite = Boolean.parseBoolean(properties.getProperty(OUTPUT_OVERWRITE, "false"));
File rankingFile = new File(properties.getProperty(OUTPUT_FILE));
File groundtruthFile = new File(properties.getProperty(GROUNDTRUTH_FILE));
EvaluationStrategy.OUTPUT_FORMAT format = null;
if (properties.getProperty(OUTPUT_FORMAT).equals(EvaluationStrategy.OUTPUT_FORMAT.TRECEVAL.toString())) {
format = EvaluationStrategy.OUTPUT_FORMAT.TRECEVAL;
} else {
format = EvaluationStrategy.OUTPUT_FORMAT.SIMPLE;
}
Double threshold = Double.parseDouble(properties.getProperty(RELEVANCE_THRESHOLD));
String strategyClassName = properties.getProperty(STRATEGY);
Class<?> strategyClass = Class.forName(strategyClassName);
// get strategy
EvaluationStrategy<Long, Long> strategy = null;
if (strategyClassName.contains("RelPlusN")) {
Integer number = Integer.parseInt(properties.getProperty(RELPLUSN_N));
Long seed = Long.parseLong(properties.getProperty(RELPLUSN_SEED));
strategy = new RelPlusN(trainingModel, testModel, number, threshold, seed);
} else {
Object strategyObj = strategyClass.getConstructor(DataModelIF.class, DataModelIF.class, double.class).newInstance(trainingModel, testModel, threshold);
if (strategyObj instanceof EvaluationStrategy) {
@SuppressWarnings("unchecked")
EvaluationStrategy<Long, Long> strategyTemp = (EvaluationStrategy<Long, Long>) strategyObj;
strategy = strategyTemp;
}
}
// generate output
generateOutput(testModel, inputFile, strategy, format, rankingFile, groundtruthFile, overwrite);
}
示例13: run
import net.recommenders.rival.core.SimpleParser; //導入依賴的package包/類
/**
* Runs a single evaluation strategy.
*
* @param properties The properties of the strategy.
* @throws IOException when a file cannot be parsed
* @throws ClassNotFoundException see {@link #instantiateStrategy(java.util.Properties, net.recommenders.rival.core.DataModelIF, net.recommenders.rival.core.DataModelIF)}
* @throws IllegalAccessException see {@link #instantiateStrategy(java.util.Properties, net.recommenders.rival.core.DataModelIF, net.recommenders.rival.core.DataModelIF)}
* @throws InstantiationException see {@link #instantiateStrategy(java.util.Properties, net.recommenders.rival.core.DataModelIF, net.recommenders.rival.core.DataModelIF)}
* @throws InvocationTargetException see {@link #instantiateStrategy(java.util.Properties, net.recommenders.rival.core.DataModelIF, net.recommenders.rival.core.DataModelIF)}
* @throws NoSuchMethodException see {@link #instantiateStrategy(java.util.Properties, net.recommenders.rival.core.DataModelIF, net.recommenders.rival.core.DataModelIF)}
*/
public static void run(final Properties properties)
throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
// read splits
System.out.println("Parsing started: training file");
File trainingFile = new File(properties.getProperty(TRAINING_FILE));
DataModelIF<Long, Long> trainingModel = new SimpleParser().parseData(trainingFile);
System.out.println("Parsing finished: training file");
System.out.println("Parsing started: test file");
File testFile = new File(properties.getProperty(TEST_FILE));
DataModelIF<Long, Long> testModel = new SimpleParser().parseData(testFile);
System.out.println("Parsing finished: test file");
// read other parameters
File inputFile = new File(properties.getProperty(INPUT_FILE));
Boolean overwrite = Boolean.parseBoolean(properties.getProperty(OUTPUT_OVERWRITE, "false"));
File rankingFile = new File(properties.getProperty(OUTPUT_FILE));
File groundtruthFile = new File(properties.getProperty(GROUNDTRUTH_FILE));
EvaluationStrategy.OUTPUT_FORMAT format = null;
if (properties.getProperty(OUTPUT_FORMAT).equals(EvaluationStrategy.OUTPUT_FORMAT.TRECEVAL.toString())) {
format = EvaluationStrategy.OUTPUT_FORMAT.TRECEVAL;
} else {
format = EvaluationStrategy.OUTPUT_FORMAT.SIMPLE;
}
// get strategy
EvaluationStrategy<Long, Long> strategy = instantiateStrategy(properties, trainingModel, testModel);
// read recommendations: user \t item \t score
final Map<Long, List<Pair<Long, Double>>> mapUserRecommendations = new HashMap<Long, List<Pair<Long, Double>>>();
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), "UTF-8"));
try {
String line = null;
while ((line = in.readLine()) != null) {
StrategyIO.readLine(line, mapUserRecommendations);
}
} finally {
in.close();
}
// generate output
generateOutput(testModel, mapUserRecommendations, strategy, format, rankingFile, groundtruthFile, overwrite);
}
示例14: evaluate
import net.recommenders.rival.core.SimpleParser; //導入依賴的package包/類
/**
* Evaluate predictions using an evaluation model against the test set.
*
* @param splitPath path where splits have been stored
* @param strategyModelPath path where strategy model files have been stored
* @return evaluation metrics
*/
public EvaluationMetrics evaluate(final String splitPath, final String strategyModelPath) throws IOException {
EvaluationMetrics results = new EvaluationMetrics();
double rmseResult = 0.0;
double maeResult = 0.0;
for (int cutoff : this.cutoffs) {
double ndcgRes = 0.0;
double precisionRes = 0.0;
double recallRes = 0.0;
for (int i = 0; i < this.numFolds; i++) {
File testFile = new File(Paths.get(splitPath, "test_" + i + FILE_EXT).toString());
File strategyFile = new File(Paths.get(strategyModelPath, "strategymodel_" + i + FILE_EXT).toString());
DataModelIF<Long, Long> testModel = new SimpleParser().parseData(testFile);
DataModelIF<Long, Long> strategyModel = new SimpleParser().parseData(strategyFile);
// Error metrics calculated only once per fold, using all predictions
if (cutoff == this.cutoffs[0]) {
RMSE<Long, Long> rmse = new RMSE<>(strategyModel, testModel);
rmse.compute();
rmseResult += rmse.getValue();
MAE<Long, Long> mae = new MAE<>(strategyModel, testModel);
mae.compute();
maeResult += mae.getValue();
}
// Ranking metrics
NDCG<Long, Long> ndcg = new NDCG<>(strategyModel, testModel, new int[]{cutoff});
ndcg.compute();
ndcgRes += ndcg.getValueAt(cutoff);
Precision<Long, Long> precision = new Precision<>(strategyModel, testModel, this.relevanceThreshold, new int[]{cutoff});
precision.compute();
precisionRes += precision.getValueAt(cutoff);
Recall<Long, Long> recall = new Recall<>(strategyModel, testModel, this.relevanceThreshold, new int[]{cutoff});
recall.compute();
recallRes += recall.getValueAt(cutoff);
}
results.setPrecisionAtK(cutoff, precisionRes / this.numFolds);
results.setRecallAtK(cutoff, recallRes / this.numFolds);
results.setNDCGAtK(cutoff, ndcgRes / this.numFolds);
log.info("Ranking metrics at {} computed", cutoff);
log.info("[email protected]" + cutoff + ": " + ndcgRes / this.numFolds + " / [email protected]" + cutoff + ": " + precisionRes / this.numFolds
+ " / [email protected]" + cutoff + ": " + recallRes / this.numFolds);
}
results.setMAE(maeResult / this.numFolds);
results.setRMSE(rmseResult / this.numFolds);
log.info("Error metrics computed");
log.info("RMSE: " + rmseResult / this.numFolds + " / MAE: " + maeResult / this.numFolds);
return results;
}