简单商品推荐
来源:互联网 发布:上瘾2网络剧台湾版网盘 编辑:程序博客网 时间:2024/05/12 17:08
本次简单商品推荐只考虑购买两种商品的情况。即希望得到:如果一个人购买了商品A,那么他很有可能购买B。这样的规则。
首先导入我们的数据集,并查看用来训练的数据集类型。
import numpy as npfrom __future__ import divisiondataSet_fileName = "affinity_dataset.txt"X = np.loadtxt(dataSet_fileName)print X[:5]我们取前五个会得到如下数组。
[[ 0. 1. 0. 0. 0.] [ 1. 1. 0. 0. 0.] [ 0. 0. 1. 0. 1.] [ 1. 1. 0. 0. 0.] [ 0. 0. 1. 1. 1.]]
以下是我们使用的数据代表的含义。
features = ["bread", "milk", "cheese", "apples", "bananas"]
每一行代表一条记录,1代表买了该水果,0代表没买该水果,我们的目的就是根据已知的买水果搭配,推测当用户买水果时,他还可能买什么其他水果?
num_apple_purchases = 0for example in X: if example[3] ==1: num_apple_purchases +=1print '{0} people bought apple'.format(num_apple_purchases)
我们先初始化一个购买苹果数量的变量,然后打印有多少个人购买了苹果。
from collections import defaultdict as dfdvalid_rules = dfd(int)invalid_rules = dfd(int)num_same = dfd(int)features = ["bread", "milk", "cheese", "apples", "bananas"]for example in X: for premise in range(4): if example[premise] ==0: continue num_same[premise] += 1 n_example,n_features =X.shape for conclusion in range(n_features): if premise == conclusion: continue #跳过本次循环 if example[conclusion] == 1: valid_rules[(premise,conclusion)] +=1 else: invalid_rules[(premise,conclusion)] +=1
利用两个循环,分别循环每个个体和每个个体的特征,如果条件满足(为1),则该条件加1.。shape输出行数(交易记录)和列数(特征)。
为了提高准确度,需要跳过条件和结论相同的情况,比如顾客买了香蕉,他们也买了香蕉。
如果适用于个体,则规则应验加1.否则 不应验加1。
support = valid_rulesconfidence = dfd(float)for premise,conclusion in valid_rules.keys(): rule = (premise,conclusion) confidence[rule] = valid_rules[rule]/num_same[premise]for premise, conclusion in confidence: premise_name = features[premise] conclusion_name = features[conclusion] print("Rule: If a person buys {0} they will also buy {1}".format(premise_name, conclusion_name)) print(" - Confidence: {0:.3f}".format(confidence[(premise, conclusion)])) print(" - Support: {0}".format(support[(premise, conclusion)])) print("")
支持度即有效规则次数,初始化一个置信度,规则应验的键值是由条件和结论组成的,这里采用函数keys来循环条件和结论。他的置信度就是规则应验数除以总数。
#计算置信度的时候,需要导入from __future__ import division,代表了精确除法。当我们没有导入时,/执行的是截断除法。比如1/4,在没有导入时,会出现0,导入后,才会出现0.25.
最后所得结果如下:
Rule: If a person buys bread they will also buy milk - Confidence: 0.464 - Support: 13Rule: If a person buys milk they will also buy cheese - Confidence: 0.212 - Support: 11Rule: If a person buys apples they will also buy cheese - Confidence: 0.512 - Support: 22Rule: If a person buys milk they will also buy apples - Confidence: 0.346 - Support: 18Rule: If a person buys apples they will also buy bread - Confidence: 0.209 - Support: 9Rule: If a person buys apples they will also buy milk - Confidence: 0.419 - Support: 18Rule: If a person buys milk they will also buy bananas - Confidence: 0.519 - Support: 27Rule: If a person buys cheese they will also buy bananas - Confidence: 0.513 - Support: 20Rule: If a person buys cheese they will also buy bread - Confidence: 0.128 - Support: 5Rule: If a person buys cheese they will also buy apples - Confidence: 0.564 - Support: 22Rule: If a person buys cheese they will also buy milk - Confidence: 0.282 - Support: 11Rule: If a person buys bread they will also buy bananas - Confidence: 0.571 - Support: 16Rule: If a person buys milk they will also buy bread - Confidence: 0.250 - Support: 13Rule: If a person buys bread they will also buy apples - Confidence: 0.321 - Support: 9Rule: If a person buys apples they will also buy bananas - Confidence: 0.628 - Support: 27Rule: If a person buys bread they will also buy cheese - Confidence: 0.179 - Support: 5
阅读全文
1 0
- 简单商品推荐
- 简单商品管理
- Demo8:简单商品评星
- 简单商品展示案例(ListView)
- abgular简单商品的查删
- Jsp动态网站初步项目 --简单商品浏览界面(说明)
- Jsp动态网站初步项目 --简单商品浏览页面 (数据库itemdb的设计)
- Jsp动态网站初步项目 --简单商品浏览界面(工具包DBConnection类)
- jsp+servlet实现简单商品上传、商品浏览、与商品分页技术
- 简单商品搜索界面的实现,流失布局,历史记录,商品展示,商品详情
- 推荐
- 推荐
- 推荐~
- 推荐
- 推荐
- 推荐
- 推荐
- 推荐
- Softmax的理解与应用
- MySQL数据库的一些注意事项
- Linux基本指令
- 编程基础--八皇后问题
- 2.Elevator
- 简单商品推荐
- linux-smb
- QSL之创建与管理表
- Jlink下载不进去
- 使用Python和xlwt向Excel文件中写入中文
- numpy基本矩阵操作
- 一头坑进Redis之事务
- 《七大排序算法》(六)之归并排序(Java实现)
- 创建竖表节省空间