谁有基于用户的推荐系统或者协同过滤的算法和代码分析啊
槽槽 2014-08-08 01:57:20 求个大数据的大神给个 基于用户的推荐系统或者协同过滤的算法和代码分析啊
我有部分代码但是不知道怎么在Eclipse上实现 求解答啊
1.public class AggregateAndRecommendReducer extends Reducer<VarLongWritable,VectorWritable,VarLongWritable,RecommendedItemsWritable>{
...
public viod reduce (VarLongWritable key,Iterable<VectorWritable>values,Context context)throws IOException,InterruptedException{
Vector recommendationVector=null;
for(VectorWritable vectorWritable:values){
recommendationVector=recommendationVector==null?
vectorWritable.get();
recommendationVector.plus(bectorWritable.get());
}
Queue<RecommendedItem> topItems=new PriorityQueue<RecommendedItem>(recommendationsPerUser+1,Collections.reverseOrder(ByValueRecommendedItemComparator.getInstance()));
Iterator<Vector.Element> recommendationVectorIterator=recommendationVector.iterateNonZero();
while(recommendationVectorIterator.hasNext()){
vector.Element element=recommendationVectorIterator.next();
int index=element.index();
float value = (float) element.get();
if (topItems.size()<recommendationsPerUser){
topItems.add(new GenericRecommendedItem(indexItemIDMap.get(index),value));
}
else if (value>topItems.peek().getValue()){
topItems.add(new GenericRecommendedItem(indexItemIDMap.get(index),value));
topItems.poll();
}
}
List<RecommendedItem> recommendations=new ArrayList<RecommendedItem>(topItems.size());
recommendations.addAll(topItems);
Collections.sort(recommendations,ByValueRecommendedItemComparator.getInstance());
context.write(key,
new
recommendedItemsWritable(recommendations));
}
}
2.public class AggregateCombiner extends Reducer<VarLongWritable,VectorWritable,VarLongWritable,VectorWritable>{public void reduce(varLongWritable key,Iterable<VectorWritable>values,Context context)throws IOException, IniterruptedException{
Vector partial=null;
for (VectorWritable vectorWritable:values){
partial=partial==null?
vectorWritable.get():partial.plus(vectorWritable.get());
}
context.write(key,new VectorWritable(partial));
}
}
3.public class CooccurrenceColumnWrapperMapper extends Mapper<IntWritable,VectorWritable,IntWritable,VectorOrPrefWritable>{
public void map(IntWritable key,VectorWritable value,Context context) throws IOException, IniterruptedException{
context.write(key,new VectorOrPrefWritable(value.get()));
}
}
4.public class PartialMultiplyMapper extends Mapper<IntWritable,VectorAndPrefsWritable,VarLongWritable,VectorWritable>{
public void map(IntWritable key,VectorAndPrefsWritable vectorAndPrefsWritable,Context context)throws IOException, InterruptedException{
Vector cooccurrenceColumn=vectorAndPrefsWritable.getVector();
List<Long> userIDs=vectorAndPrefsWritable.getUserIDs();
List<Float> prefValues=vectorAndPrefsWritable.getValues();
for (int i=0;i<=userIDs.size();i++){
long userID=userIDs.get(i);
float prefValue=prefValues.get(i);
Vector partialProduct=cooccurrenceColumn.times(prefValue);
context.write(new VarLongWritable(userID),new VectorWritable(partialProduct));
}
}
}
5.public class UserVectorSplitterMapper extends Mapper<VarLongWritable,VectorWritable,IntWritable,VectorOrPrefWritable>{
public void map(VarLongWritable key,VectorWritable key,Context context)throws IOException, InterruptedException{
long userID=key.get();
Vector userVector = value.get();
Iterator<Vector.Element>it=userVector.iterateNonZero();
IntWritable itemIndexWritable=new IntWritable();
while(it.hasNext()){
vector.Element e = it,next();
int itemIndex=e.index();
float preferenceValue=(float)e.get();
itemIndexWritable.set(itemIndex);
context.write(itemIndexWritable,new VectorOrPrefWritable(userID,preferenceValue));
}
}
}
6.public UserVectorToCooccurrenceMapper extends Mapper<VarLongWritable,VarLongWritable,Intwritable,Intwritable>{
public void map(VarLongWritable userID,VectorWritable userVector,Context context) throws IOException, InterruptedException{Iterator<Vector.Element> it=userVector.get().iterateNonZero();
while(it.hasNext()){
int index1 = it.next().index();
Iterator<Vector,Element> it2 = userVector.get().iterateNonZero();
while(it2.hasNext()){
int index2 = it2.next(0.index();
context.write(new IntWritable(index1),new IntWritable(index2));
}
}
}
7.
public UserVectorToCooccurrenceReducer extends Reducer<Intwritable,Intwritable,Intwritable,VectorWritable>{
public void reduce(Intwritable itemIndex1,Iterable<IntWritable>itemIndex2s,Context context) throws IOException, InterruptedException{
Vector cooccurrenceRow=new RandomAccessSparseVector(Integer.MAX_VALUE,100);
FOR (Intwritable intwritable:itemIndex2s){
int itemIndex2 = intwritable.get();
cooccurrenceRow.set(itemIndex2,cooccurrenceRow.get(itemIndex2)+1.0);
}
context.write(itemIndex1,new VectorWritable(cooccurrenceRow));
}
}
8.public class WikipediaToItemPrefsMapper
extends Mapper<LongWritable,Text,VarLongWritable,VarLongWritable>{
pricate static final pattern NUMBERS=Pattern.compile("(\\d+)");
public void map(LongWritable key,Text value,Context context)throws TOException, InterruptedException{
String line = value.toString();
Matcher m = NIMBERS.matcher(line);
m.find();
VarLongWritable userID=new VarLongWritable(Long.parseLong(m.group()));
VarLongWritable itemID=new VarLongWritable()
while(m.find()){
itemID.set(Long.parseLong(m.group()));
context.write(userID,itemID);
}
}
}
9.public class WikipediaToUserPrefsReducer
extends Reducer<VarLongWritable,VarLongWritable,VarLongWritable,VectorWritable>{
public void reduce(VarLongWritable userID,Iterable<VarLongWritable> itemPrefs,Context context)throws TOException, InterruptedException{
Vector userVector=new RandomAccessSparseVector(Integer.MAX_VALUE,100);
//*循环遍历用户的物品偏好对*//
for (VarLongWritable itemPref:itemPrefs){
userVector.set((int)itemPref.get(),1.0f);
}
context.write(userID,new VectorWritable(userVector));
}
}