日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

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

發(fā)布時間:2023/12/20 python 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python亲和性分析法推荐电影论文_关于《Python数据挖掘入门与实战》读书笔记二(亲和性分析)... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

#原文的代碼比較零散,網(wǎng)上的代碼大多數(shù)互抄,先基于個人的理解對代碼進(jìn)行了歸納整理,并添加了注釋importnumpyasnpfromcollectionsimportdefaultdictfromoperatorimportitemgetter#生成關(guān)聯(lián)規(guī)則defmake_relation_rule(X,n_features):

valid_rules=defaultdict(int)#定義規(guī)則有效的集合invalid_rules=defaultdict(int)#定義規(guī)則無效的集合num_occurances=defaultdict(int)#定義某商品支持度集合

#復(fù)雜度分析(Complexity Analysis) n_samples*n_features*n_featuresforsampleinX:#循環(huán)對樣本的每個個體進(jìn)行處理forpremiseinrange(n_features):#循環(huán)對樣本的每個個體的每個特征值進(jìn)行處理ifsample[premise]==0:continue#檢測個體是否滿足條件,如果不滿足,繼續(xù)檢測下一個條件。num_occurances[premise]+=1#如果條件滿足(即值為1),該條件的出現(xiàn)次數(shù)加1forconclusioninrange(n_features):#再次循環(huán)樣本的每個個體的每個特征值進(jìn)行處理ifpremise==conclusion:continue#在遍歷過程中跳過條件和結(jié)論相同的情況ifsample[conclusion]==1:#如果個體的目標(biāo)特征值為1valid_rules[(premise,conclusion)] +=1#存入規(guī)則有效的集合else:

invalid_rules[(premise,conclusion)] +=1#規(guī)則無效的集合support=valid_rules#規(guī)則有效的集合,即支持度集合confidence=defaultdict(float)#定義置信度集合

#print(valid_rules)???????????? #打印規(guī)則有效的結(jié)果集

#print(invalid_rules)?????????? #打印規(guī)則無效的結(jié)果集forpremise,conclusioninvalid_rules.keys():#valid_rules是個元祖集合rule=(premise,conclusion)#獲取每個規(guī)則confidence[rule]=float(valid_rules[rule])/num_occurances[premise]#這里需要將valid_rules的規(guī)則條目數(shù)從int轉(zhuǎn)成float,生成規(guī)則的置信度returnsupport,confidence#輸出某兩件商品的支持度和置信度defprint_especial_rule(premise,conclusion,support,confidence,features):

premise_name=features[premise]#轉(zhuǎn)換為商品名稱conclusion_name=features[conclusion]#轉(zhuǎn)換為商品名稱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)]))#輸出支持度

#輸出該結(jié)果集支持度topN最高的商品defprint_topN_suppor_rule(support,confidence,features,topN):

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

premise,conclusion = sorted_support[index][0]

print_especial_rule(premise, conclusion, support, confidence, features)#輸出該結(jié)果集置信度topN最高的商品defprint_topN_confidence_rule(support,confidence,features,topN):

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

premise,conclusion = sorted_confidence[index][0]

print_especial_rule(premise, conclusion, support, confidence, features)if__name__ =='__main__':#使用numpy加載數(shù)據(jù)集X=np.loadtxt("affinity_dataset.txt")#定義物品的映射關(guān)系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))

'''#獲取數(shù)據(jù)集的大小形狀,np_sameple為樣本數(shù)量,n_features為樣本列數(shù)n_samples,n_features=X.shapeprint(n_samples,n_features)#生成支持度和置信度集合support,confidence=make_relation_rule(X,n_features)#定義待求關(guān)聯(lián)結(jié)果的物品和物品premise=1conclusion=3#輸出兩件商品的置信度和支持度print_especial_rule(premise,conclusion,support,confidence,features)#輸出支持度高的前5條規(guī)則print_topN_suppor_rule(support,confidence,features,5)#輸出置信度高的前5條規(guī)則print_topN_confidence_rule(support, confidence, features,5)

總結(jié)

以上是生活随笔為你收集整理的python亲和性分析法推荐电影论文_关于《Python数据挖掘入门与实战》读书笔记二(亲和性分析)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。