mahout基于用户推荐的简单例子(1)
mahout是机器学习的一个工具,里面封装了大量的机器学习的算法。
算法类 | 算法名 | 中文名 |
分类算法 | Logistic Regression | 逻辑回归 |
Bayesian | 贝叶斯 | |
SVM | 支持向量机 | |
Perceptron | 感知器算法 | |
Neural Network | 神经网络 | |
Random Forests | 随机森林 | |
Restricted Boltzmann Machines | 有限波尔兹曼机 | |
聚类算法 | Canopy Clustering | Canopy聚类 |
K-means Clustering | K均值算法 | |
Fuzzy K-means | 模糊K均值 | |
Expectation Maximization | EM聚类(期望最大化聚类) | |
Mean Shift Clustering | 均值漂移聚类 | |
Hierarchical Clustering | 层次聚类 | |
Dirichlet Process Clustering | 狄里克雷过程聚类 | |
Latent Dirichlet Allocation | LDA聚类 | |
Spectral Clustering | 谱聚类 | |
关联规则挖掘 | Parallel FP Growth Algorithm | 并行FP Growth算法 |
回归 | Locally Weighted Linear Regression | 局部加权线性回归 |
降维/维约简 | Singular Value Decomposition | 奇异值分解 |
Principal Components Analysis | 主成分分析 | |
Independent Component Analysis | 独立成分分析 | |
Gaussian Discriminative Analysis | 高斯判别分析 | |
进化算法 | 并行化了Watchmaker框架 |
|
推荐/协同过滤 | Non-distributed recommenders | Taste(UserCF, ItemCF, SlopeOne) |
Distributed Recommenders | ItemCF | |
向量相似度计算 | RowSimilarityJob | 计算列间相似度 |
VectorDistanceJob | 计算向量间距离 | |
非Map-Reduce算法 | Hidden Markov Models | 隐马尔科夫模型 |
集合方法扩展 | Collections | 扩展了java的Collections类 |
package mahout; import java.io.File; import java.util.List; import org.apache.mahout.cf.taste.impl.model.file.FileDataModel; import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood; import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender; import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; import org.apache.mahout.cf.taste.model.DataModel; import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood; import org.apache.mahout.cf.taste.recommender.RecommendedItem; import org.apache.mahout.cf.taste.recommender.Recommender; import org.apache.mahout.cf.taste.similarity.UserSimilarity; public class UserRecommer { public static void main(String[] args) throws Exception { DataModel model = new FileDataModel(new File("xxx/intro.csv")); // 皮尔逊相似度算法。其他的还有好多相似度算法 UserSimilarity similarity = new PearsonCorrelationSimilarity(model); UserNeighborhood neighborhood = new NearestNUserNeighborhood(2, similarity, model); // 生成推荐系统 Recommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity); // 为用户1推荐物品1 List<RecommendedItem> recommendations = recommender.recommend(1, 1); for (RecommendedItem recommendation : recommendations) { System.out.println(recommendation); } } }结果如下:RecommendedItem[item:104, value:4.257081]
intro.csv文件内容: 1,101,5.0 1,102,3.0 1,103,2.5 2,101,2.0 2,102,2.5 2,103,5.0 2,104,2.0 3,101,2.5 3,104,4.0 3,105,4.5 3,107,5.0 4,101,5.0 4,103,3.0 4,104,4.5 4,106,4.0 5,101,4.0 5,102,3.0 5,103,2.0 5,104,4.0 5,105,3.5 5,106,4.0