Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package net.recommenders.rival.split.splitter;
  
  import java.util.List;
  import java.util.Random;
  import java.util.Set;
Class that splits a dataset randomly.

Author(s):
Alejandro
 
 public class RandomSplitter implements Splitter<LongLong> {

    
The percentage of training to be used by the splitter.
 
     private float percentageTraining;
    
The flag that indicates if the split should be done in a per user basis.
 
     private boolean perUser;
    
The flag that indicates if the split should consider all the items independently.
 
     private boolean doSplitPerItems;
    
An instance of a Random class.
 
     private Random rnd;

    
Constructor

Parameters:
percentageTraining percentage of training data to be split
perUser flag to do the split in a per user basis
seed value to initialize a Random class
doSplitPerItems if true, every interaction between a user and a specific item is considered as one, and hence all of them will be either on the training or on the test split
 
     public RandomSplitter(float percentageTrainingboolean perUserlong seedboolean doSplitPerItems) {
         this. = percentageTraining;
         this. = perUser;
         this. = doSplitPerItems;
 
          = new Random(seed);
     }
 
    @Override
     public DataModel<LongLong>[] split(DataModel<LongLongdata) {
         final DataModel<LongLong>[] splits = new DataModel[2];
         splits[0] = new DataModel<LongLong>(); // training
         splits[1] = new DataModel<LongLong>(); // test
         if () {
             for (Long user : data.getUsers()) {
                 if () {
                     List<Longitems = new ArrayList<Long>(data.getUserItemPreferences().get(user).keySet());
                     Collections.shuffle(items);
                     int splitPoint = Math.round( * items.size());
                     for (int i = 0; i < items.size(); i++) {
                         Long item = items.get(i);
                         Double pref = data.getUserItemPreferences().get(user).get(item);
                         Set<Longtime = null;
                         if (data.getUserItemTimestamps().containsKey(user) && data.getUserItemTimestamps().get(user).containsKey(item)) {
                             time = data.getUserItemTimestamps().get(user).get(item);
                         }
                         DataModel<LongLongdatamodel = splits[0]; // training
                         if (i > splitPoint) {
                             datamodel = splits[1]; // test
                         }
                         if (pref != null) {
                             datamodel.addPreference(useritempref);
                         }
                         if (time != null) {
                             for (Long t : time) {
                                 datamodel.addTimestamp(useritemt);
                             }
                         }
                     }
                 } else {
                     if (!data.getUserItemTimestamps().containsKey(user)) {
                         continue;
                     }
                     List<StringitemsTime = new ArrayList<String>();
                     for (Entry<LongSet<Long>> e : data.getUserItemTimestamps().get(user).entrySet()) {
                         long i = e.getKey();
                         for (Long t : e.getValue()) {
                             itemsTime.add(i + "_" + t);
                         }
                     }
                     Collections.shuffle(itemsTime);
                     int splitPoint = Math.round( * itemsTime.size());
                     for (int i = 0; i < itemsTime.size(); i++) {
                         String it = itemsTime.get(i);
                        Long item = Long.parseLong(it.split("_")[0]);
                        Long time = Long.parseLong(it.split("_")[1]);
                        Double pref = data.getUserItemPreferences().get(user).get(item);
                        DataModel<LongLongdatamodel = splits[0]; // training
                        if (i > splitPoint) {
                            datamodel = splits[1]; // test
                        }
                        if (pref != null) {
                            datamodel.addPreference(useritempref);
                        }
                        if (time != null) {
                            datamodel.addTimestamp(useritemtime);
                        }
                    }
                }
            }
        } else {
            for (Long user : data.getUsers()) {
                for (Long item : data.getUserItemPreferences().get(user).keySet()) {
                    Double pref = data.getUserItemPreferences().get(user).get(item);
                    Set<Longtime = null;
                    if (data.getUserItemTimestamps().containsKey(user) && data.getUserItemTimestamps().get(user).containsKey(item)) {
                        time = data.getUserItemTimestamps().get(user).get(item);
                    }
                    if () {
                        DataModel<LongLongdatamodel = splits[0]; // training
                        if (.nextDouble() > ) {
                            datamodel = splits[1]; // test
                        }
                        if (pref != null) {
                            datamodel.addPreference(useritempref);
                        }
                        if (time != null) {
                            for (Long t : time) {
                                datamodel.addTimestamp(useritemt);
                            }
                        }
                    } else {
                        if (time != null) {
                            for (Long t : time) {
                                DataModel<LongLongdatamodel = splits[0]; // training
                                if (.nextDouble() > ) {
                                    datamodel = splits[1]; // test
                                }
                                if (pref != null) {
                                    datamodel.addPreference(useritempref);
                                }
                                datamodel.addTimestamp(useritemt);
                            }
                        } else {
                            DataModel<LongLongdatamodel = splits[0]; // training
                            if (.nextDouble() > ) {
                                datamodel = splits[1]; // test
                            }
                            if (pref != null) {
                                datamodel.addPreference(useritempref);
                            }
                        }
                    }
                }
            }
        }
        return splits;
    }
New to GrepCode? Check out our FAQ X