欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python亲和性分析法推荐电影论文_关于《Python数据挖掘入门与实战》读书笔记二(亲和性分析)...

发布时间:2023/12/20 python 45 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python亲和性分析法推荐电影论文_关于《Python数据挖掘入门与实战》读书笔记二(亲和性分析)... 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

#原文的代码比较零散,网上的代码大多数互抄,先基于个人的理解对代码进行了归纳整理,并添加了注释importnumpyasnpfromcollectionsimportdefaultdictfromoperatorimportitemgetter#生成关联规则defmake_relation_rule(X,n_features):

valid_rules=defaultdict(int)#定义规则有效的集合invalid_rules=defaultdict(int)#定义规则无效的集合num_occurances=defaultdict(int)#定义某商品支持度集合

#复杂度分析(Complexity Analysis) n_samples*n_features*n_featuresforsampleinX:#循环对样本的每个个体进行处理forpremiseinrange(n_features):#循环对样本的每个个体的每个特征值进行处理ifsample[premise]==0:continue#检测个体是否满足条件,如果不满足,继续检测下一个条件。num_occurances[premise]+=1#如果条件满足(即值为1),该条件的出现次数加1forconclusioninrange(n_features):#再次循环样本的每个个体的每个特征值进行处理ifpremise==conclusion:continue#在遍历过程中跳过条件和结论相同的情况ifsample[conclusion]==1:#如果个体的目标特征值为1valid_rules[(premise,conclusion)] +=1#存入规则有效的集合else:

invalid_rules[(premise,conclusion)] +=1#规则无效的集合support=valid_rules#规则有效的集合,即支持度集合confidence=defaultdict(float)#定义置信度集合

#print(valid_rules)             #打印规则有效的结果集

#print(invalid_rules)           #打印规则无效的结果集forpremise,conclusioninvalid_rules.keys():#valid_rules是个元祖集合rule=(premise,conclusion)#获取每个规则confidence[rule]=float(valid_rules[rule])/num_occurances[premise]#这里需要将valid_rules的规则条目数从int转成float,生成规则的置信度returnsupport,confidence#输出某两件商品的支持度和置信度defprint_especial_rule(premise,conclusion,support,confidence,features):

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("-Support:{0}".format(support[(premise,conclusion)]))#输出支持度print("-Confidence:{0:.3f}".format(confidence[(premise,conclusion)]))#输出支持度

#输出该结果集支持度topN最高的商品defprint_topN_suppor_rule(support,confidence,features,topN):

sorted_support =sorted(support.items(),key=itemgetter(1),reverse=True)print('支持度最高的前{0}条规则:'.format(topN))forindexinrange(topN):print("规则 #{0}".format(index +1))

premise,conclusion = sorted_support[index][0]

print_especial_rule(premise, conclusion, support, confidence, features)#输出该结果集置信度topN最高的商品defprint_topN_confidence_rule(support,confidence,features,topN):

sorted_confidence =sorted(confidence.items(),key=itemgetter(1),reverse=True)print('置信度最高的前{0}条规则:'.format(topN))forindexinrange(topN):print("规则 #{0}".format(index +1))

premise,conclusion = sorted_confidence[index][0]

print_especial_rule(premise, conclusion, support, confidence, features)if__name__ =='__main__':#使用numpy加载数据集X=np.loadtxt("affinity_dataset.txt")#定义物品的映射关系features = ["bread","milk","cheese","apples","bananas"]'''

#Version1购买苹果的支持度

num_app_purchases=0

for sameple in X:

if sameple[3]==1:

num_app_purchases+=1

print('{0}people bought Apples'.format(num_app_purchases))

'''#获取数据集的大小形状,np_sameple为样本数量,n_features为样本列数n_samples,n_features=X.shapeprint(n_samples,n_features)#生成支持度和置信度集合support,confidence=make_relation_rule(X,n_features)#定义待求关联结果的物品和物品premise=1conclusion=3#输出两件商品的置信度和支持度print_especial_rule(premise,conclusion,support,confidence,features)#输出支持度高的前5条规则print_topN_suppor_rule(support,confidence,features,5)#输出置信度高的前5条规则print_topN_confidence_rule(support, confidence, features,5)

总结

以上是生活随笔为你收集整理的python亲和性分析法推荐电影论文_关于《Python数据挖掘入门与实战》读书笔记二(亲和性分析)...的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。