1. 程式人生 > >數據挖掘——親和性分析

數據挖掘——親和性分析

基因 conf log 支持度 用戶 continue 字典 rem 帶來

親和性分析根據樣本個體之間的相似度,確定它們關系的親疏。應用場景:

1.向網站用戶提供多樣化的服務或投放定向廣告。

2.為了向用戶推薦電影或商品

3.根據基因尋找有親緣關系的人

比如:統計顧客購買了商品1,然後再購買商品2的比率,算相似度。

import numpy as np
dataset_filename = "affinity_dataset.txt"
x = np.loadtxt(dataset_filename)
# print x[:5]
# 上述代碼的結果代表前5次交易中顧客購買了什麽。用“1”表示購買,“0”表示沒有購買。
# 這五種商品分別是:面包,牛奶,奶酪,蘋果和香蕉。
# 現在我們要找出“如果顧客購買了商品x,那麽他們可能願意購買商品y”的規則(一條規則有前提條件和結論兩部分組成)。衡量一個規則的優劣通常有:支持度(指數據集中規則應驗的次數)和置信度(指規則準確率如何,計算方法是:規則應驗次數除以滿足前提條件的所有次數)。 # 舉個例子計算有多少人購買了蘋果。 num_apples_purchases = 0 for sample in x: if sample[3] == 1: num_apples_purchases += 1 # print "{0} people bought Apples".format(num_apples_purchases)
# 接著我們計算有多少人購買了蘋果,後又購買了香蕉。同時計算支持度和置信度。 num_apples_bananas_purchases = 0 for sample in x: if sample[3] == 1 and sample[4] == 1: num_apples_bananas_purchases += 1 valid_rules = num_apples_bananas_purchases num_occurances = num_apples_purchases support = valid_rules confidence = valid_rules/float(num_occurances)
print "{0} people bought Apples, but {1} people also bought bananas".format(num_apples_purchases, num_apples_bananas_purchases) print "------" # 支持度 print support # 置信度 print "{0:.3f}".format(confidence) # 我們接著將所有規則下的可能性都統計出來,找出親和性最高的幾個。首先,分為兩種:一種是規則應驗,一種是規則無效。分別創建字典。字典的鍵是由條件和結論組成的元組,元組元素為特征在特征列表中的索引值,比如“如果顧客買了蘋果,他們也會買香蕉”就用(3,4)表示。這裏使用defaultdict,好處是如果查找的鍵不存在,返回一個默認值。 from collections import defaultdict features = ["bread", "milk", "cheese", "apple", "banana"] valib_rules = defaultdict(int) invalib_rules = defaultdict(int) num_occurances = defaultdict(int) # 依次對樣本的每個個體及個體的每個特征值進行處理。第一個特征為規則的前提條件。 for sample in x: for premise in xrange(4): if sample[premise] == 0: continue num_occurances[premise] += 1 # 比如“顧客買了蘋果,他們也買了蘋果”,這樣的規則是沒有意義的。 for conclusion in xrange(len(features)): if premise == conclusion: continue if sample[conclusion] == 1: valib_rules[(premise, conclusion)] += 1 else: invalib_rules[(premise, conclusion)] += 1 support = valib_rules confidence = defaultdict(float) ‘‘‘ for premise, conclusion in valib_rules.keys(): rule = (premise, conclusion) confidence[rule] = valib_rules[rule] / num_occurances[premise] ‘‘‘ # 這樣我們就得到了支持度字典和置信度字典。我們再來創建一個函數,以便更加方便查看結果。 def print_rule(premise, conclusion, support, confidence, features): premise_name = features[premise] conclusion_name = features[conclusion] confidence[(premise, conclusion)] = valib_rules[(premise, conclusion)] / float(num_occurances[premise]) 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)]) if __name__ == "__main__": premise = 1 conclusion = 3 # print print_rule(premise, conclusion, support, confidence, features) # 排序找出最佳的規則。對字典排序:首先字典的items()函數返回包含字典所有元素的列表,再使用itemgetter()類作為鍵,這樣就可以對嵌套列表進行排序了。 from operator import itemgetter sorted_support = sorted(support.items(), key=itemgetter(1), reverse=True) # 提取支持度最高的5條 for index in range(5): print "Rule #{0}".format(index + 1) premise, conclusion = sorted_support[index][0] print_rule(premise, conclusion, support, confidence, features) # 總結親和性分析,可以清楚的看出哪兩種商品一起購買的幾率要大些,經理就可以根據這些規則來調整商品擺放的位置,從而為商家帶來更大的經濟效益。
affinity_dataset.txt
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
0 1 0 0 0
0 0 1 1 1
0 0 1 1 0
0 1 0 1 0
0 1 0 0 1
0 0 0 1 0
1 0 1 0 0
1 0 0 0 1
0 1 1 0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