本文整理汇总了Java中org.apache.mahout.cf.taste.impl.common.FastByIDMap类的典型用法代码示例。如果您正苦于以下问题:Java FastByIDMap类的具体用法?Java FastByIDMap怎么用?Java FastByIDMap使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FastByIDMap类属于org.apache.mahout.cf.taste.impl.common包,在下文中一共展示了FastByIDMap类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: IRState
import org.apache.mahout.cf.taste.impl.common.FastByIDMap; //导入依赖的package包/类
public void IRState(String taskName) {
String itemmodelsPath = RecommendConfig.class.getResource("/").getPath() + "itemmodels.csv";
HadoopUtil.download(taskName, itemmodelsPath, false);
try {
DataModel fileDataModel = new FileDataModel(new File(itemmodelsPath));
RecommenderIRStatsEvaluator irStatsEvaluator = new GenericRecommenderIRStatsEvaluator();
IRStatistics irStatistics = irStatsEvaluator.evaluate(new RecommenderBuilder() {
@Override
public org.apache.mahout.cf.taste.recommender.Recommender buildRecommender(final DataModel dataModel) throws TasteException {
UserSimilarity userSimilarity = new PearsonCorrelationSimilarity(dataModel);
UserNeighborhood userNeighborhood = new NearestNUserNeighborhood(5, userSimilarity, dataModel);
return new GenericUserBasedRecommender(dataModel, userNeighborhood, userSimilarity);
}
}, new DataModelBuilder() {
@Override
public DataModel buildDataModel(final FastByIDMap<PreferenceArray> fastByIDMap) {
return new GenericDataModel(fastByIDMap);
}
}, fileDataModel, null, 5, GenericRecommenderIRStatsEvaluator.CHOOSE_THRESHOLD, 1.0);
System.out.println("查准率:" + irStatistics.getPrecision());
System.out.println("查全率:" + irStatistics.getRecall());
} catch (TasteException | IOException e) {
e.printStackTrace();
}
}
示例2: testBuildKNNRecommender
import org.apache.mahout.cf.taste.impl.common.FastByIDMap; //导入依赖的package包/类
@Test
public void testBuildKNNRecommender() {
GenericRecommenderBuilder rb = new GenericRecommenderBuilder();
FastByIDMap<PreferenceArray> userData = new FastByIDMap<PreferenceArray>();
userData.put(1, new GenericUserPreferenceArray(Arrays.asList(new GenericPreference(1, 1, 1),
new GenericPreference(1, 2, 1), new GenericPreference(1, 3, 1))));
userData.put(2, new GenericUserPreferenceArray(Arrays.asList(new GenericPreference(2, 1, 1),
new GenericPreference(2, 2, 1), new GenericPreference(2, 4, 1))));
DataModel dm = new GenericDataModel(userData);
Recommender rec = null;
String recommenderType = "org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender";
String similarityType = "org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity";
try {
rec = rb.buildRecommender(dm, recommenderType, similarityType);
} catch (RecommenderException e) {
e.printStackTrace();
}
assertTrue(rec instanceof GenericUserBasedRecommender);
}
示例3: loadInMemory
import org.apache.mahout.cf.taste.impl.common.FastByIDMap; //导入依赖的package包/类
public DataModel loadInMemory() {
// In-memory DataModel - GenericDataModels
FastByIDMap<PreferenceArray> preferences = new FastByIDMap<PreferenceArray>();
PreferenceArray prefsForUser1 = new GenericUserPreferenceArray(10);
prefsForUser1.setUserID(0, 1L);
prefsForUser1.setItemID(0, 101L);
prefsForUser1.setValue(0, 3.0f);
prefsForUser1.setItemID(1, 102L);
prefsForUser1.setValue(1, 4.5F);
preferences.put(1L, prefsForUser1); // use userID as the key
//TODO: add others users
// Return preferences as new data model
DataModel dataModel = new GenericDataModel(preferences);
return dataModel;
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:22,代码来源:BookRecommender.java
示例4: extractRawStudentPreferenceData
import org.apache.mahout.cf.taste.impl.common.FastByIDMap; //导入依赖的package包/类
private FastByIDMap<Collection<Preference>> extractRawStudentPreferenceData(CloseableIterator<StudentSubjectPreference> iterator) {
FastByIDMap<Collection<Preference>> rawData = new FastByIDMap<>();
iterator.forEachRemaining(it -> {
Collection<Preference> studentPreferences;
if (rawData.containsKey(it.getStudentId())) {
studentPreferences = rawData.get(it.getStudentId());
} else {
studentPreferences = new ArrayList<>();
rawData.put(it.getStudentId(), studentPreferences);
}
studentPreferences.add(new GenericPreference(it.getStudentId(), it.getSubjectId(), it.getPreferenceValue().floatValue()));
});
return rawData;
}
示例5: getGenericDataModel
import org.apache.mahout.cf.taste.impl.common.FastByIDMap; //导入依赖的package包/类
private DataModel getGenericDataModel() {
FastByIDMap<PreferenceArray> preferences =
new FastByIDMap<PreferenceArray>();
List<User> users = userService.getAllUsers();
for (int j = 0; j < users.size(); j++) {
List<Preference2d> preference2ds =
preference2dService.getPreferencesByUserId(users.get(j).getId());
PreferenceArray prefsForUser =
new GenericUserPreferenceArray(preference2ds.size());
prefsForUser.setUserID(j, users.get(j).getId());
for (int i = 0; i < preference2ds.size(); i++) {
prefsForUser.setItemID(i, preference2ds.get(i).getRestaurantId());
prefsForUser.setValue(i, preference2ds.get(i).getScore().floatValue());
}
preferences.put(j, prefsForUser);
}
return new GenericDataModel(preferences);
}
示例6: intializeDelegate
import org.apache.mahout.cf.taste.impl.common.FastByIDMap; //导入依赖的package包/类
private void intializeDelegate() {
try {
//iterate over all user ids
LongPrimitiveIterator it = easyrecDataModelDelegate.getUserIDs();
FastByIDMap<PreferenceArray> preferences = new FastByIDMap<PreferenceArray>(easyrecDataModelDelegate.getNumUsers());
FastByIDMap<FastByIDMap<Long>> timestamps = new FastByIDMap<FastByIDMap<Long>>(easyrecDataModelDelegate.getNumUsers());
while (it.hasNext()) {
Long userId = it.next();
//get preferences for each user
PreferenceArray prefs = easyrecDataModelDelegate.getPreferencesFromUser(userId);
preferences.put(userId,prefs);
//get preference times for each user
FastByIDMap<Long> timestampsForUser = new FastByIDMap<Long>(prefs.getIDs().length);
for (Long itemId: prefs.getIDs()) {
timestampsForUser.put(itemId,easyrecDataModelDelegate.getPreferenceTime(userId, itemId));
}
timestamps.put(userId, timestampsForUser);
}
//generate GenericDataModel
setMaxPreference(easyrecDataModelDelegate.getMaxPreference());
setMinPreference(easyrecDataModelDelegate.getMinPreference());
this.inMemoryDelegate = new GenericDataModel(preferences,timestamps);
} catch (TasteException e) {
logger.warn("caught exception while reading preference data", e);
}
}
示例7: DataModelWrapper
import org.apache.mahout.cf.taste.impl.common.FastByIDMap; //导入依赖的package包/类
/**
* Constructs the wrapper using the provided model.
*
* @param model the model to be used to create the wrapped model
*/
public DataModelWrapper(final net.recommenders.rival.core.TemporalDataModelIF<Long, Long> model) {
FastByIDMap<Collection<Preference>> data = new FastByIDMap<Collection<Preference>>();
FastByIDMap<FastByIDMap<Long>> timestampData = new FastByIDMap<FastByIDMap<Long>>();
for (Long u : model.getUsers()) {
List<Preference> prefs = new ArrayList<Preference>();
FastByIDMap<Long> userTimestamps = new FastByIDMap<Long>();
timestampData.put(u, userTimestamps);
for (Long i : model.getUserItems(u)) {
Iterable<Long> timestamps = model.getUserItemTimestamps(u, i);
long t = -1;
if (timestamps != null) {
for (Long tt : timestamps) {
t = tt;
break;
}
}
userTimestamps.put(i, t);
prefs.add(new GenericPreference(u, i, model.getUserItemPreference(u, i).floatValue()));
}
data.put(u, prefs);
}
FastByIDMap<PreferenceArray> userData = GenericDataModel.toDataMap(data, true);
wrapper = new GenericDataModel(userData, timestampData);
}
示例8: testBuildDefaultRecommender
import org.apache.mahout.cf.taste.impl.common.FastByIDMap; //导入依赖的package包/类
@Test
public void testBuildDefaultRecommender() {
RecommenderBuilder rb = new GenericRecommenderBuilder();
FastByIDMap<PreferenceArray> userData = new FastByIDMap<PreferenceArray>();
userData.put(1, new GenericUserPreferenceArray(Arrays.asList(new GenericPreference(1, 1, 1),
new GenericPreference(1, 2, 1), new GenericPreference(1, 3, 1))));
userData.put(2, new GenericUserPreferenceArray(Arrays.asList(new GenericPreference(2, 1, 1),
new GenericPreference(2, 2, 1), new GenericPreference(2, 4, 1))));
DataModel dm = new GenericDataModel(userData);
Recommender rec = null;
try {
rec = rb.buildRecommender(dm);
} catch (TasteException e) {
e.printStackTrace();
}
assertTrue(rec instanceof RandomRecommender);
}
示例9: processLine
import org.apache.mahout.cf.taste.impl.common.FastByIDMap; //导入依赖的package包/类
@Override
protected void processLine(String line, FastByIDMap<?> data, FastByIDMap<FastByIDMap<Long>> timestamps, boolean fromPriorData) {
String[] fields = line.split(",");
if (fields[2].equals("-1")) {
fields[2] = "3";
}
line = fields[0] + "," + fields[1] + "," + fields[2];
super.processLine(line, data, timestamps, fromPriorData);
}
示例10: processLineWithoutID
import org.apache.mahout.cf.taste.impl.common.FastByIDMap; //导入依赖的package包/类
@Override
protected void processLineWithoutID(String line, FastByIDMap<FastIDSet> data, FastByIDMap<FastByIDMap<Long>> timestamps) {
String[] fields = line.split(",");
if (fields[2].equals("-1")) {
fields[2] = "3";
}
line = fields[0] + "," + fields[1] + "," + fields[2];
super.processLineWithoutID(line, data, timestamps);
}
示例11: MemoryUserClusterStore
import org.apache.mahout.cf.taste.impl.common.FastByIDMap; //导入依赖的package包/类
public MemoryUserClusterStore(String client,int entries)
{
logger.info("MemoryUserClusterStore for "+client+" of size "+entries);
this.store = new FastByIDMap<>(entries);
this.client = client;
this.clusterGroups = new ConcurrentHashMap<>();
this.transientClusters = new ConcurrentHashMap<>();
}
示例12: getPrefDataByFileFast
import org.apache.mahout.cf.taste.impl.common.FastByIDMap; //导入依赖的package包/类
public static DataModel getPrefDataByFileFast() throws Exception {
String data = EEG_INTEREST_LEVEL_DATASET_FILE;
FastByIDMap<PreferenceArray> preferences = new FastByIDMap<PreferenceArray>();
BufferedReader br = new BufferedReader(new FileReader(data));
List<Preference> list = new ArrayList<Preference>();
String line = br.readLine();
long uid_init = Long.parseLong(line.split(",")[0]);
while (line != null) {
String[] arr = line.split(",");
long uid = Long.parseLong(arr[0]);
if (uid_init == uid) {
list.add(new GenericPreference(uid, Long.parseLong(arr[1]),
Float.parseFloat(arr[2])));
} else {
preferences.put(uid_init, new GenericUserPreferenceArray(list));
list = new ArrayList<Preference>();
list.add(new GenericPreference(uid, Long.parseLong(arr[1]),
Float.parseFloat(arr[2])));
uid_init = uid;
}
line = br.readLine();
if (line == null) {
preferences.put(uid_init, new GenericUserPreferenceArray(list));
}
}
br.close();
return new GenericDataModel(preferences);
}
示例13: createNoPrefDataModelBuilder
import org.apache.mahout.cf.taste.impl.common.FastByIDMap; //导入依赖的package包/类
public static DataModelBuilder createNoPrefDataModelBuilder()
throws TasteException {
return new DataModelBuilder() {
public DataModel buildDataModel(
FastByIDMap<PreferenceArray> trainingData) {
return new GenericBooleanPrefDataModel(
GenericBooleanPrefDataModel.toDataMap(trainingData));
}
};
}
示例14: createPrefDataModelBuilder
import org.apache.mahout.cf.taste.impl.common.FastByIDMap; //导入依赖的package包/类
public static DataModelBuilder createPrefDataModelBuilder() throws TasteException {
return new DataModelBuilder() {
public DataModel buildDataModel(
FastByIDMap<PreferenceArray> trainingData) {
return new GenericDataModel(trainingData);
}
};
}
示例15: addTimestamp
import org.apache.mahout.cf.taste.impl.common.FastByIDMap; //导入依赖的package包/类
@Override
public void addTimestamp(Long u, Long i, Long t) {
if (model != null) {
throw new IllegalArgumentException("DataModel already generated. It is not possible to add more information.");
}
FastByIDMap<Long> prefs = null;
if (!timestampData.containsKey(u)) {
prefs = new FastByIDMap<Long>();
timestampData.put(u, prefs);
} else {
prefs = timestampData.get(u);
}
prefs.put(i, t);
}