guoguibing / librec

LibRec: A Leading Java Library for Recommender Systems, see
https://www.librec.net/
Other
3.23k stars 1.03k forks source link

Need Help for using Sparse Matrix #263

Closed sososa1213 closed 6 years ago

sososa1213 commented 6 years ago

Here the code I use it and it works fine. However, I need to use sparse matrix for the dataset ml-100k->ratings.txt and store the result in new file. Really I get stuck in this point and I did not find any example for how use sparse matrix with new version of librec.

I really highly appreciate any help. Thank.

import net.librec.BaseTestCase;
import net.librec.common.LibrecException;
import net.librec.conf.Configuration;
import net.librec.conf.Configuration.Resource;
import net.librec.data.model.TextDataModel;
import net.librec.eval.RecommenderEvaluator;
import net.librec.eval.rating.MAEEvaluator;
import net.librec.eval.rating.RMSEEvaluator;
import net.librec.filter.GenericRecommendedFilter;
import net.librec.filter.RecommendedFilter;
import net.librec.recommender.Recommender;
import net.librec.recommender.RecommenderContext;
import net.librec.recommender.cf.ItemKNNRecommender;
import net.librec.recommender.cf.UserKNNRecommender;
import net.librec.recommender.item.RecommendedItem;
import net.librec.similarity.PCCSimilarity;
import net.librec.similarity.RecommenderSimilarity;
import java.awt.List;
import java.io.BufferedWriter;
import java.io.FileWriter;

import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runners.MethodSorters;
public class test1 {
    public static void main (String [] args)  throws Exception {
    System.out.print("xxx");
    Resource resource = new Resource("librec.properties");

    Configuration conf = new Configuration();
    conf.addResource(resource);
    conf.set("data.splitter.trainset.ratio", "0.8");

    conf.set("inputDataPath", conf.get("dfs.data.dir") + "/movielens/ml-100k/ratings.txt");
    conf.set("data.column.format","UIR");
    conf.set("data.convert.binarize.threshold","-1.0");

    TextDataModel dataModel = new TextDataModel(conf);
    dataModel.buildDataModel();

    RecommenderContext context = new RecommenderContext(conf,dataModel);

    RecommenderSimilarity similarity = new PCCSimilarity();
    similarity.buildSimilarityMatrix(dataModel);
    context.setSimilarity(similarity);

    Recommender recommender = new ItemKNNRecommender();
    recommender.setContext(context);

    recommender.recommend(context);

    RecommenderEvaluator evaluator = new MAEEvaluator();

    System.out.println("MAE:" + recommender.evaluate(evaluator));
    java.util.List<RecommendedItem> recommendedItemList = recommender.getRecommendedList();

//    print filter result
    System.out.println("len " + recommendedItemList.size());
    BufferedWriter writer = new BufferedWriter(new FileWriter("recommendations.txt"));
    for (RecommendedItem recommendedItem : recommendedItemList) {
        //System.out.println("user:" + recommendedItem.getUserId() + " " +
            //  "item:" + recommendedItem.getItemId() + " " +
            //  "value:" + recommendedItem.getValue() + "\n");
        writer.write(

                "user:" + recommendedItem.getUserId() + " " +
                "item:" + recommendedItem.getItemId() + " " +
                "value:" + recommendedItem.getValue() + "\n"
                );
        writer.flush();
    }
    writer.close();
    }
}
allenjack commented 6 years ago

Hi, Sorry, I did not get your point. Basically, the raw data is stored in the 'dataModel' object. We transform the raw data into the sparse matrix and store it in the 'recommender' object. There are trainMatrix and testMatrix in the 'recommender' object, which all are sparse matrices.

sososa1213 commented 6 years ago

Thanks for your comment, I get confused do you mean I have to do this:

We transform the raw data into the sparse matrix and store it in the 'recommender' object.

or librec did it already?

Can you pleas check issue #266

allenjack commented 6 years ago

Hi, You do not need to do that. It has been done default by LibRec.

On 17 July 2018 at 03:58, sososa1213 notifications@github.com wrote:

Thanks for your comment, I get confused do you mean I have to do this:

We transform the raw data into the sparse matrix and store it in the 'recommender' object.

or librec did it already?

Can you pleas check issue #266 https://github.com/guoguibing/librec/issues/266

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/guoguibing/librec/issues/263#issuecomment-405494496, or mute the thread https://github.com/notifications/unsubscribe-auth/AEN9vss7s9Yw0JDLnMNo1QJrGp5UUqLMks5uHZkQgaJpZM4VO9kT .

sososa1213 commented 6 years ago

Is that mean if I use the way I poste it in issue #266, that means I used sparse matrix?

Sorry but I wnat to make sure before finalizing my code, becasue I need to improve my code by using sparse matrix.

Thanks again @allenjack

allenjack commented 6 years ago

Yes, you are right. You are using the sparse matrix.

On 17 July 2018 at 10:44, sososa1213 notifications@github.com wrote:

Is that mean if I use the way I poste it in issue #266 https://github.com/guoguibing/librec/issues/266, that means I used sparse matrix?

Sorry but I wnat to make sure before finalizing my code, becasue I need to improve my code by using sparse matrix.

Thanks again @allenjack https://github.com/allenjack

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/guoguibing/librec/issues/263#issuecomment-405607756, or mute the thread https://github.com/notifications/unsubscribe-auth/AEN9vmFBcprwR4jyz69-1O02ZHUVhnyfks5uHfhVgaJpZM4VO9kT .

sososa1213 commented 6 years ago

Thank you I really appreciat your help