#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Downloads the 20newsgroups dataset, trains and tests a classifier.
#
# To run: change into the mahout directory and type:
# examples/bin/classify-20newsgroups.sh
////支持--help以查看该脚本的功能,本脚本是对经典的20 News Groups运行CBayes分类算法
if [ "$1" = "--help" ] || [ "$1" = "--?" ]; then
echo "This script runs SGD and Bayes classifiers over the classic 20 News Groups."
exit
fi
SCRIPT_PATH=${0%/*}
if [ "$0" != "$SCRIPT_PATH" ] && [ "$SCRIPT_PATH" != "" ]; then
cd $SCRIPT_PATH
fi
START_PATH=`pwd`
# Set commands for dfs
source ${START_PATH}/set-dfs-commands.sh
////Mahout MapReduce 作业的本地工作目录,
WORK_DIR=/tmp/mahout-work-${USER}
////本脚本支持的分类算法,这里选择cnaivebayes-MapReduce算法
algorithm=( cnaivebayes-MapReduce naivebayes-MapReduce cnaivebayes-Spark naivebayes-Spark sgd clean)
/////选择分类算法
if [ -n "$1" ]; then
choice=$1
else
echo "Please select a number to choose the corresponding task to run"
echo "1. ${algorithm[0]}"
echo "2. ${algorithm[1]}"
echo "3. ${algorithm[2]}"
echo "4. ${algorithm[3]}"
echo "5. ${algorithm[4]}"
echo "6. ${algorithm[5]}-- cleans up the work area in $WORK_DIR"
read -p "Enter your choice : " choice
fi
echo "ok. You chose $choice and we'll use ${algorithm[$choice-1]}"
///alg中保存选择的算法
alg=${algorithm[$choice-1]}
# Spark specific check and work
if [ "x$alg" == "xnaivebayes-Spark" -o "x$alg" == "xcnaivebayes-Spark" ]; then
if [ "$MASTER" == "" ] ; then
echo "Plese set your MASTER env variable to point to your Spark Master URL. exiting..."
exit 1
fi
if [ "$MAHOUT_LOCAL" != "" ] ; then
echo "Options 3 and 4 can not run in MAHOUT_LOCAL mode. exiting..."
exit 1
fi
fi
////如果不是clean操作
if [ "x$alg" != "xclean" ]; then
echo "creating work directory at ${WORK_DIR}"
////创建工作目录
mkdir -p ${WORK_DIR}
////下载样本文件
if [ ! -e ${WORK_DIR}/20news-bayesinput ]; then
if [ ! -e ${WORK_DIR}/20news-bydate ]; then
if [ ! -f ${WORK_DIR}/20news-bydate.tar.gz ]; then
echo "Downloading 20news-bydate"
curl http://people.csail.mit.edu/jrennie/20Newsgroups/20news-bydate.tar.gz -o ${WORK_DIR}/20news-bydate.tar.gz
fi
mkdir -p ${WORK_DIR}/20news-bydate
echo "Extracting..."
cd ${WORK_DIR}/20news-bydate && tar xzf ../20news-bydate.tar.gz && cd .. && cd ..
fi
fi
fi
#echo $START_PATH
cd $START_PATH
cd ../..
set -e
////如果CBayes分类算法
if ( [ "x$alg" == "xnaivebayes-MapReduce" ] || [ "x$alg" == "xcnaivebayes-MapReduce" ] || [ "x$alg" == "xnaivebayes-Spark" ] || [ "x$alg" == "xcnaivebayes-Spark" ] ); then
c=""
if [ "x$alg" == "xcnaivebayes-MapReduce" -o "x$alg" == "xnaivebayes-Spark" ]; then
c=" -c"
fi
set -x
echo "Preparing 20newsgroups data"
rm -rf ${WORK_DIR}/20news-all
mkdir ${WORK_DIR}/20news-all
cp -R ${WORK_DIR}/20news-bydate/*/* ${WORK_DIR}/20news-all
if [ "$HADOOP_HOME" != "" ] && [ "$MAHOUT_LOCAL" == "" ] ; then
echo "Copying 20newsgroups data to HDFS"
set +e
$DFSRM ${WORK_DIR}/20news-all
$DFS -mkdir ${WORK_DIR}
$DFS -mkdir ${WORK_DIR}/20news-all
set -e
if [ $HVERSION -eq "1" ] ; then
echo "Copying 20newsgroups data to Hadoop 1 HDFS"
$DFS -put ${WORK_DIR}/20news-all ${WORK_DIR}/20news-all
elif [ $HVERSION -eq "2" ] ; then
echo "Copying 20newsgroups data to Hadoop 2 HDFS"
$DFS -put ${WORK_DIR}/20news-all ${WORK_DIR}/
fi
fi
////1. 对样本文件进行创建Sequence File
echo "Creating sequence files from 20newsgroups data"
./bin/mahout seqdirectory \
-i ${WORK_DIR}/20news-all \
-o ${WORK_DIR}/20news-seq -ow
////2. 将Sequence File转换为tfidf向量文件,存放于20news-vectors目录下
echo "Converting sequence files to vectors"
./bin/mahout seq2sparse \
-i ${WORK_DIR}/20news-seq \
-o ${WORK_DIR}/20news-vectors -lnorm -nv -wt tfidf
////3. 对生成的向量数据集(20news-vectors/tfidf-vectors)创建training向量文件和测试向量文件
echo "Creating training and holdout set with a random 80-20 split of the generated vector dataset"
./bin/mahout split \
-i ${WORK_DIR}/20news-vectors/tfidf-vectors \
--trainingOutput ${WORK_DIR}/20news-train-vectors \
--testOutput ${WORK_DIR}/20news-test-vectors \
--randomSelectionPct 40 --overwrite --sequenceFiles -xm sequential
if [ "x$alg" == "xnaivebayes-MapReduce" -o "x$alg" == "xcnaivebayes-MapReduce" ]; then
////4. 训练Naive Bayes模型,输入为训练向量,输出为训练模型
////labelindex指的是分类名称,共20个,以Sequence File格式保存
echo "Training Naive Bayes model"
./bin/mahout trainnb \
-i ${WORK_DIR}/20news-train-vectors \
-o ${WORK_DIR}/model \
-li ${WORK_DIR}/labelindex \
-ow $c
////5.1 基于训练向量文件进行测试
echo "Self testing on training set"
./bin/mahout testnb \
-i ${WORK_DIR}/20news-train-vectors\
-m ${WORK_DIR}/model \
-l ${WORK_DIR}/labelindex \
-ow -o ${WORK_DIR}/20news-testing $c
echo "Testing on holdout set"
////5.2 基于测试向量文件进行测试
./bin/mahout testnb \
-i ${WORK_DIR}/20news-test-vectors\
-m ${WORK_DIR}/model \
-l ${WORK_DIR}/labelindex \
-ow -o ${WORK_DIR}/20news-testing $c
elif [ "x$alg" == "xnaivebayes-Spark" -o "x$alg" == "xcnaivebayes-Spark" ]; then
echo "Training Naive Bayes model"
./bin/mahout spark-trainnb \
-i ${WORK_DIR}/20news-train-vectors \
-o ${WORK_DIR}/spark-model $c -ow -ma $MASTER
echo "Self testing on training set"
./bin/mahout spark-testnb \
-i ${WORK_DIR}/20news-train-vectors\
-m ${WORK_DIR}/spark-model $c -ma $MASTER
echo "Testing on holdout set"
./bin/mahout spark-testnb \
-i ${WORK_DIR}/20news-test-vectors\
-m ${WORK_DIR}/spark-model $c -ma $MASTER
fi
////如果是线性回归算法
elif [ "x$alg" == "xsgd" ]; then
if [ ! -e "/tmp/news-group.model" ]; then
echo "Training on ${WORK_DIR}/20news-bydate/20news-bydate-train/"
./bin/mahout org.apache.mahout.classifier.sgd.TrainNewsGroups ${WORK_DIR}/20news-bydate/20news-bydate-train/
fi
echo "Testing on ${WORK_DIR}/20news-bydate/20news-bydate-test/ with model: /tmp/news-group.model"
./bin/mahout org.apache.mahout.classifier.sgd.TestNewsGroups --input ${WORK_DIR}/20news-bydate/20news-bydate-test/ --model /tmp/news-group.model
////如果是clean操作,则删除工作目录
elif [ "x$alg" == "xclean" ]; then
rm -rf $WORK_DIR
rm -rf /tmp/news-group.model
$DFSRM $WORK_DIR
fi
# Remove the work directory
#
基于Mahout的CBayes算法执行流程如下:
1. 对样本文件进行创建Sequence File
2. 将Sequence File转换为tfidf向量文件,存放于20news-vectors目录下
3. 对生成的向量数据集(20news-vectors/tfidf-vectors)创建training向量文件和测试向量文件
4. 训练Naive Bayes模型,输入为训练向量,输出为训练模型
5.1 基于训练向量文件进行测试
5.2 基于测试向量文件进行测试
分享到:
相关推荐
本项目"基于Mahout实现协同过滤推荐算法的电影推荐系统"旨在利用Apache Mahout这一开源机器学习库,构建一个能够为用户推荐个性化电影的系统。以下将详细介绍该系统的相关知识点: 1. **协同过滤推荐算法**: 协同...
基于Java+Mahout的协同过滤推荐算法图书推荐系统源码+项目说明.zip 基于协同过滤的书籍推荐系统,图书推荐系统 最新版本,在原先手动计算皮尔逊相似度和评分矩阵的基础上添加了Mahout实现的协同过滤推荐算法。 ...
Mahout 聚类算法是数据挖掘和机器学习领域中的一种重要算法,它可以将相似的数据点聚集在一起,以便更好地理解和分析数据。Mahout 聚类算法可以分为多种类型,如 Canopy、KMeans、Fuzzy-KMeans、Spectral Clustering...
为了解决这一问题,Apache Mahout在Hadoop平台上实现了基于项目的分布式协同过滤推荐算法。Mahout是一个开源的机器学习框架,它利用Hadoop强大的存储和计算能力,将协同过滤推荐算法向分布式方向进行了扩展。该算法...
该资源是在Eclipse平台里,使用Mahout库的API,实现基于用户的协同过滤算法,从而进行商品推荐。 软件环境是:win7 64位 +Eclipse4.4 + jdk1.6, 用到了7个.jar包, 分别为:commons-logging-1.2.jar, commons-...
Apache Mahout是一个基于Hadoop的数据挖掘库,它提供了多种推荐算法的实现,如协同过滤、基于内容的推荐以及混合推荐方法。 Mahout的协同过滤算法是其核心功能之一,它通过分析用户的历史行为来预测他们可能感兴趣...
在这个“mahout的基于用户的推荐Demo”中,我们将深入探讨如何利用Mahout构建一个简单的用户基于的推荐系统。 ### 1. Mahout简介 Mahout源于Hadoop生态系统,它提供了可扩展的机器学习算法,支持大规模数据集的...
通过以上知识点的分析,可以看出基于Hadoop-Mahout的分布式课程推荐算法具有重要的研究价值和实际应用意义。随着在线教育的不断发展和用户个性化需求的提升,推荐系统将会在在线学习平台扮演越来越重要的角色。而...
Taste 包含了多种推荐算法的实现,如基于用户的、基于物品的,还有SlopeOne算法等,同时提供扩展接口,允许开发者自定义推荐算法。 DataModel 是 Taste 中的一个关键组件,它负责存储用户喜好信息,可以是从数据库...
《基于Mahout的协同过滤算法实现详解》 在大数据领域,推荐系统已成为不可或缺的一部分,它能够根据用户的历史行为和偏好,为用户提供个性化的产品或服务推荐。Mahout作为Apache的一个开源项目,提供了丰富的机器...
电影推荐系统_基于Mahout实现的协同过滤推荐算法的电影推荐系统_项目源码_优质项目实战
基于java+Mahout实现协同过滤推荐算法的电影推荐系统+源码,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~ 基于java+Mahout实现协同过滤推荐算法的电影推荐系统+...
mahout中的贝叶斯算法的拓展开发包,提供了相关接口可以供用户调用,直接即可跑出结果,相关运行方式参考blog《mahout贝叶斯算法开发思路(拓展篇)》
Mahout提供了丰富的推荐算法实现,包括单机内存算法和基于Hadoop的分布式算法。 - **单机内存算法实现**:适合处理中小规模数据(如1GB至10GB),算法配置灵活,易于实现。但受限于单机资源,难以应对大规模数据...
Apache Mahout是一个基于Hadoop的机器学习库,它提供了多种推荐算法,旨在帮助开发者构建大规模的数据挖掘和机器学习系统。这本书的目标是通过实际案例来指导读者理解并应用Mahout的推荐算法。 推荐算法是现代数据...
Mahout关联推荐算法是Apache Mahout项目中的一种推荐系统算法,它基于用户的历史行为数据,通过挖掘用户之间的关联性来生成个性化的推荐。在Mahout中,关联推荐算法主要应用于发现用户行为模式,例如用户购买商品A后...