public static void recommend ( final int nFolds , final String inPath , final String outPath ) { for ( int i = 0 ; i < nFolds ; i ++ ) { org . apache . mahout . cf . taste . model . DataModel trainModel ; org . apache . mahout . cf . taste . model . DataModel testModel ; try { trainModel = new FileDataModel ( new File ( inPath + "train_" + i + ".csv" ) ) ; testModel = new FileDataModel ( new File ( inPath + "test_" + i + ".csv" ) ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; return ; } GenericRecommenderBuilder grb = new GenericRecommenderBuilder ( ) ; String recommenderClass = "org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender" ; String similarityClass = "org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity" ; int neighborhoodSize = NEIGH_SIZE ; Recommender recommender = null ; try { recommender = grb . buildRecommender ( trainModel , recommenderClass , similarityClass , neighborhoodSize ) ; } catch ( RecommenderException e ) { e . printStackTrace ( ) ; } String fileName = "recs_" + i + ".csv" ; LongPrimitiveIterator users ; try { users = testModel . getUserIDs ( ) ; boolean createFile = true ; while ( users . hasNext ( ) ) { long u = users . nextLong ( ) ; assert recommender != null ; List < RecommendedItem > items = recommender . recommend ( u , trainModel . getNumItems ( ) ) ; // List < RecommenderIO . Preference < Long , Long > > prefs = new ArrayList <> ( ) ; for ( RecommendedItem ri : items ) { prefs . add ( new RecommenderIO . Preference <> ( u , ri . getItemID ( ) , ri . getValue ( ) ) ) ; } // RecommenderIO . writeData ( u , prefs , outPath , fileName , ! createFile , null ) ; createFile = false ; } } catch ( TasteException e ) { e . printStackTrace ( ) ; } } }
Recommends using an UB algorithm .