算法(4)购物篮分析

所谓购物篮分析主要是挖掘出用户感兴趣的数据组合,应用于电商,大型超市。比如京东推荐,购买了此用户的产品同时购买了XX产品, 浏览了此商品的用户同时也浏览了XX商品, 对于大型超市来说道理也一样,这样就可以把产品组合打包卖给有兴趣的人。 购物篮分析为推荐做好后台数据组合的工作,推荐系统根据这些组合来做推荐。购物篮分析当然远远不止于此,更多信息可以查看相关资料。


购物篮主要目标是形成对应的排列组合,比如我购买了A,B,C,D 4个产品,另外一个人购买了B,D,E,F, 那么如果按照两两组合,我买的产品有A,B,AC,AD,BC,BD,CD, 另外一个人购买了,BD,BE,BF,DE,DF,EF。 组合之后计算出现的频率,结果如下:


AB 1

AC 1

AD 1

BD 2

CD 1

BE 1

BF 1

DE 1

DF 1

EF 1


通过上面的结果,我们知道,B和D这2种产品的组合要比其他组合多,在做推荐的时候,我们就可以考虑推荐B和D一起卖给用户。 

为了实现购物篮分析,先要完成排列组合,通过递归把每个购物清单两两组合在一起,然后发送给reduce,再做统计,就能得到我们要的结果。 因此通过 Mapreduce来完成购物篮分析,难点不在于map和reduce,而在于排列组合。


排列组合代码如下:

package com.isesol.mapreduce;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;


public class findSortMBA {
	
	
	public static List<List<String>>  findsort(Collection<String> elements, int n) {
		
		List result = new ArrayList<List>();
		
		if(n == 0) {
			result.add(new ArrayList());
			return result;
		}
		
		
		List<List<String>> combinations = findsort(elements, n - 1);
		
		for(List<String> combination : combinations) {
					
			for(String element : elements) {
				
				if(combination.contains(element)) {
					continue;
				}
				
				List<String> list = new ArrayList<String>();
				list.addAll(combination);
				
				if(list.contains(element)) {
					continue;
				}
				
				list.add(element);
				Collections.sort(list);
				
				if(result.contains(list)){
					continue;
				}
				
				result.add(list);
				
			}
		}
				
		return result;
		
	}

}

Mapreduce代码统计如下:

package com.isesol.mapreduce;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import org.apache.hadoop.conf.Configurable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;
import org.apache.hadoop.mapred.join.TupleWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import com.isesol.mapreduce.findSortMBA;

public class MarketBasketAnalysis {

	public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {

		private Text data = new Text();

		public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

			String[] val = value.toString().split(",");
			List<String> list = convertToList(val);
			List<List<String>> result = findSortMBA.findsort(list, 2);

			// Tuple tuple2 = Tuple.<String, Integer>of("hello", 1);

			for (List<String> tuple : result) {

				context.write(new Text(tuple.toString()), new IntWritable(1));
			}

		}

	}

	public static List<String> convertToList(String[] val) {

		List<String> list = new ArrayList<String>();

		for (int i = 0; i < val.length; i++) {
			list.add(val[i]);
		}

		return list;

	}

	public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

		public void reduce(Text key, Iterable<IntWritable> value, Context context)
				throws IOException, InterruptedException {

			int sum = 0;
			for (IntWritable values : value) {

				sum += 1;

			}

			context.write(new Text(key), new IntWritable(sum));

		}
	}

	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf, "MarketBasketAnalysis");
		job.setJarByClass(MarketBasketAnalysis.class);
		job.setMapperClass(TokenizerMapper.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		job.setReducerClass(IntSumReducer.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		job.setNumReduceTasks(1);
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		System.exit(job.waitForCompletion(true) ? 0 : 1);

	}

}

测试数据如下:

a,b,c
b,d,e
a,d,e
b,e,f
c,d
a,f
结果:

[a, b]	1
[a, c]	1
[a, d]	1
[a, e]	1
[a, f]	1
[b, c]	1
[b, d]	1
[b, e]	2
[b, f]	1
[c, d]	1
[d, e]	2
[e, f]	1

关于对大型超市“购物篮”的分析问题,并根据分析的结果来设计出实际的实施方案以达到最大限度的赢利。由于本题中假设了题目中的数据真实有效,而且各个问题的解决都是以他们为准,所以对数据的处理方法很重要。 本题包含了四个题目,题目都是递进的,后一个问题接着上一个问题来思考的,直到最后两个问题就接近了实际的应用目的了。 问题一,我们把商品的组合情况作为了未知因素,并通过从一般情况出发,找出了商品的组合方式和商品间的关联密切程度的函数关系式,它是一个只与商品组合A(k,l)有关的函数。只要给出任何商品组合,就可以找出它的密切程度即概率。 问题二,我们利用穷举法,把已知的数据用Matlab编程进行筛选,把符合要求的数据筛选出来,经过求解我们得到表4结果。即当商品组合对为(217,283),买的人数最多,为23次,其他组合也分布在10次以上。 问题三:运用最优解的方法,求得了不同的商品组合时候的利润,并得到了最大利润为:商品组合为529,598时,利润为6717.84。再根据问题二中已经找出了各个商品的组合对组合情况及他们的利润,把购买次数大的和利润大的两个因素结合起来考虑,找出符合以下要求的组合:商品组合中有一个利润大一个利润小,同时满足表格5中购买次数比较大是商品组合。促销中,我们可以把上面组合中利润高的商品:354,529,752,661,829,打折f(i),其它商品价格不变。经过多次进行市场实践调查,得到当打折为f(i)的时候可以得到最大的利润,那么f(i)就是我们需要的打折数据。根据这个数据,我们进行促销,这就是我们的促销的初步方案。 问题四:就是根据我们对以上各个问题的解决思路找到使得超市赢利最大的方法,即是把商品间关联密切程度大的商品集中在一起,方便顾客的购买。同时达到赢利最大的目的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tom_fans

谢谢打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值