Mahout推荐1

Mahout主要包如下,其中的协同过滤推荐主要涉及cf.taste包

如下是包cf.taste的主要内容:

common包主要涉及一些Exception和Enum

eval package (重新运算求出参数的内容)包中是辅助构建推荐的interface,和推荐效果评估的interface

hadoop package中主要是一些与hadoop相关的,废话嘛,反正没怎么认真看过

neighborhood package中主要是推荐的最近邻的相关interface

similarity package中是相似度计算的interface

recommender package中是推荐过程的interface

model package之中主要是 各种数据源datamodel的相关的interface


Java中是按照interface来实现编程的,所以在阅读相关开源项目是,应该自顶向下的看,先看interface

制定的相关功能,同时代码一般按照 interface,interface extends ,abstractclass,class的层次结构传承

下来的,同时还会进行功能分块,与功能抽离(model类中的genericdatamodel,就是一个例子

他抽离出一个单纯的内存数据模型,方便其他函数调用,对应桥接,策略的设计模式),

有些功能在在本类中看起来比较简单或者白痴,比如genericdatamodel的构造参数, public GenericDataModel(FastByIDMap<PreferenceArray> userData, FastByIDMap<FastByIDMap<Long>> timestamps) {
    Preconditions.checkArgument(userData != null, "userData is null");

感觉第一个参数,从哪来,其实还是面向过程的习惯的问题,

面型对象就是功能的分配与抽离的抽象画,这也是单元测试,和分层设计的基础,

只有将功能抽离,分层,一个具体过程抽象为多个抽象的过程,才能实现代码复用,

将代码分块之后,通过各种设计模式的组合搭配来实现复杂的功能,有点像工程组件的拼接

 A simple {@link DataModel} which uses a given {@link List} of users as its data source. This implementation
 is mostly useful for small experiments and is not recommended for contexts where performance is important.

GenericDataModel

泛型匹配符类型转化

public static FastByIDMap<PreferenceArray> toDataMap(FastByIDMap<Collection<Preference>> data,
                                                       boolean byUser) {
    for (Map.Entry<Long,Object> entry : ((FastByIDMap<Object>) (FastByIDMap<?>) data).entrySet()) {
      List<Preference> prefList = (List<Preference>) entry.getValue();
      entry.setValue(byUser ? new GenericUserPreferenceArray(prefList) : new GenericItemPreferenceArray(
          prefList));
    }
    return (FastByIDMap<PreferenceArray>) (FastByIDMap<?>) data;
  }

泛型中子类是不能带进父类的,不知道接口是否有问题

public GenericDataModel(FastByIDMap<PreferenceArray> userData, FastByIDMap<FastByIDMap<Long>> timestamps) {
    Preconditions.checkArgument(userData != null, "userData is null");

    this.preferenceFromUsers = userData;
    FastByIDMap<Collection<Preference>> prefsForItems = new FastByIDMap<Collection<Preference>>();
    FastIDSet itemIDSet = new FastIDSet();
    int currentCount = 0;
    float maxPrefValue = Float.NEGATIVE_INFINITY;
    float minPrefValue = Float.POSITIVE_INFINITY;
    for (Map.Entry<Long, PreferenceArray> entry : preferenceFromUsers.entrySet()) {
      PreferenceArray prefs = entry.getValue();
      prefs.sortByItem();
      for (Preference preference : prefs) {
        long itemID = preference.getItemID();
        itemIDSet.add(itemID);
        Collection<Preference> prefsForItem = prefsForItems.get(itemID);
        if (prefsForItem == null) {
          prefsForItem = new ArrayList<Preference>(2);
          prefsForItems.put(itemID, prefsForItem);

        }
        prefsForItem.add(preference);
        float value = preference.getValue();
        if (value > maxPrefValue) {
          maxPrefValue = value;
        }
        if (value < minPrefValue) {
          minPrefValue = value;
        }
      }
      if (++currentCount % 10000 == 0) {
        log.info("Processed {} users", currentCount);
      }
    }
    log.info("Processed {} users", currentCount);

    setMinPreference(minPrefValue);
    setMaxPreference(maxPrefValue);

    this.itemIDs = itemIDSet.toArray();
    itemIDSet = null; // Might help GC -- this is big
    Arrays.sort(itemIDs);

    this.preferenceForItems = toDataMap(prefsForItems, false);

    for (Map.Entry<Long, PreferenceArray> entry : preferenceForItems.entrySet()) {
      entry.getValue().sortByUser();
    }

    this.userIDs = new long[userData.size()];
    int i = 0;
    LongPrimitiveIterator it = userData.keySetIterator();
    while (it.hasNext()) {
      userIDs[i++] = it.next();
    }
    Arrays.sort(userIDs);

    this.timestamps = timestamps;
  }



FileDataModel


对文件操作的文件锁

相关光信息,file文件的内容格式,其次对文件的更新,以文本文件形式保存

相关系统使用使用的日志记录文件。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值