<span style="font-size:18px;">/***
* @author YangXin
* @date 2016/2/19
* @info Libimseti上的匿名用户推荐系统
*/
package unitFive;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.GenericUserPreferenceArray;
import org.apache.mahout.cf.taste.impl.model.PlusAnonymousUserDataModel;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.model.PreferenceArray;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
public class LibimsetiWithAnonymousRecommender extends LibimsetiRecommender {
private final PlusAnonymousUserDataModel plusAnonymousModel;
//注这里的readResouceToTempFile待实现
public LibimsetiWithAnonymousRecommender() throws TasteException, IOException{
this(new FileDataModel(readResourceToTempFile("E:\\mahout项目\\examples\\ratings.dat")));
}
/***
* 此方法封装了如何将资源转换为加载的文件,此处未实现
* @param string
* @return
*/
private static File readResourceToTempFile(String string) {
// TODO Auto-generated method stub
return null;
}
public LibimsetiWithAnonymousRecommender(DataModel model) throws TasteException, IOException{
super(new PlusAnonymousUserDataModel(model));
plusAnonymousModel = (PlusAnonymousUserDataModel)getDataModel();
}
public synchronized List<RecommendedItem> recommend(PreferenceArray anonymouseUserPrefs, int howMany) throws TasteException{
plusAnonymousModel.setTempPrefs(anonymouseUserPrefs);
List<RecommendedItem> recommendations = recommend(PlusAnonymousUserDataModel.TEMP_USER_ID, howMany, null);
plusAnonymousModel.clearTempPrefs();
return recommendations;
}
public static void main(String[] args) throws Exception{
PreferenceArray anonymousPrefs = new GenericUserPreferenceArray(3);
anonymousPrefs.setUserID(0, PlusAnonymousUserDataModel.TEMP_USER_ID);
anonymousPrefs.setItemID(0, 123L);
anonymousPrefs.setValue(0, 1.0f);
anonymousPrefs.setItemID(1, 123L);
anonymousPrefs.setValue(1, 3.0f);
anonymousPrefs.setItemID(2, 123L);
anonymousPrefs.setValue(2, 2.0f);
LibimsetiWithAnonymousRecommender recommender = new LibimsetiWithAnonymousRecommender();
List<RecommendedItem> recommendations = recommender.recommend(anonymousPrefs, 10);
System.out.println(recommendations);
}
}
</span>
相关文章
- C#委托(delegate)的常用方式- 委托的定义 // 委托的核心是跟委托的函数结构一样 public delegate string SayHello(string c); public delegate string SayHello(string c);:定义了一个公共委托类型 SayHello,该委托接受一个 string 类型的参数 c,并返回一个 string 类型的值。 Main 方法 static void Main(string args) { // 本质上其实就是把方法当作委托的参数 SayHello sayC = new SayHello(SayChinese); Console.WriteLine(sayC("欢迎大家")); SayHello sayE = new SayHello(SayEgnlish); Console.WriteLine(sayE("Welcome to")); // 简单的写法:必须类型一样 SayHello s1 = SayChinese; SayHello s2 = SayEgnlish; Console.WriteLine(s1("好好好")); Console.WriteLine(s2("Gooood")); // 最推荐 SayHello ss1 = con => con; Console.WriteLine(ss1("niiiice")); // 匿名委托:一次性委托 SayHello ss3 = delegate(string s) { return s; }; Console.WriteLine(ss3("说中国话")); } 常规实例化委托 SayHello sayC = new SayHello(SayChinese);:创建了一个 SayHello 委托的实例 sayC,并将 SayChinese 方法作为参数传递给委托的构造函数。 Console.WriteLine(sayC("欢迎大家"));:通过委托实例调用 SayChinese 方法,并输出结果。 同理,SayHello sayE = new SayHello(SayEgnlish); 和 Console.WriteLine(sayE("Welcome to")); 是对 SayEgnlish 方法的委托调用。 简化的委托赋值方式 SayHello s1 = SayChinese; 和 SayHello s2 = SayEgnlish;:当委托类型和方法签名一致时,可以直接将方法赋值给委托变量,无需使用 new 关键字。 Console.WriteLine(s1("好好好")); 和 Console.WriteLine(s2("Gooood"));:通过委托实例调用相应的方法。 使用 Lambda 表达式实例化委托 SayHello ss1 = con => con;:使用 Lambda 表达式创建委托实例 ss1,con => con 表示接受一个参数 con 并返回该参数本身。 Console.WriteLine(ss1("niiiice"));:通过委托实例调用 Lambda 表达式。 匿名委托 SayHello ss3 = delegate(string s) { return s; };:使用匿名委托创建委托实例 ss3,delegate(string s) { return s; } 是一个匿名方法,直接在委托实例化时定义了方法体。 Console.WriteLine(ss3("说中国话"));:通过委托实例调用匿名方法。 委托引用的方法定义 public static string SayChinese(string content) { return content; } public static string SayEgnlish(string content) { return content; } public static string SayChinese(string content) 和 public static string SayEgnlish(string content):定义了两个静态方法,分别接受一个 string 类型的参数 content,并返回该参数本身。这两个方法的签名与 SayHello 委托一致,可以被 SayHello 委托引用。 常规的委托实例化、简化的赋值方式、Lambda 表达式和匿名委托。委托在 C# 中是一种强大的机制,它允许将方法作为参数传递,实现了代码的灵活性和可扩展性。
- 推荐系统中的用户偏好
- iOS便签软件求推荐,有可以在iOS系统上使用的桌面便签软件吗
- Creating adaptive web recommendation system based on user behavior(设计基于用户行为数据的适应性网络推荐系统)
- 使用PHP和MySQL的用户推荐系统
- VS 2005,网站(文件系统)在本地测试可以正常登录,但发布到服务器上后,登录失败: 未知的用户名或错误密码。
- 实战智能推荐系统(7)-- 基于用户的协同过滤算法
- 推荐系统实践--基于用户的协同过滤算法
- 【Java EE 学习 76 上】【数据采集系统第八天】【角色授权】【用户授权】【权限的粗粒度控制】【权限的细粒度控制】
- 文章翻译:Recommending items to more than a billion people(面向十亿级用户的推荐系统)