程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Association Rules -- Apriori algorithm case -- Python

編輯:Python

data mining

  • Input
  • Output
  • Code

Learn and write , Ongoing update
Used with catalog


Input

This is a excel form , Yes 1k Data
link
1000 A patient's condition

Output

Output strong association rules directly

More content , Set in the code
For example, look at Frequent itemsets , You can uncomment the code yourself and run it again

Code

# -*- codeing = utf-8 -*-
# @Time : 2021/11/26 22:41
# @Author : Tancy
# @File : Case analysis -- Apriori Algorithm .py
# @Software : PyCharm
# 1. data fetch 
import pandas as pd
df = pd.read_excel('D:\A_ Study \ Data warehouse and data mining \ experiment \ Patient symptoms .xlsx')
# print(df.head())
# 2. Data preprocessing 
symptoms = [] # Create an empty list Illness 
# segmentation Into a two-dimensional array 
for i in df[' Patient symptoms '].tolist():
symptoms.append(i.split(','))
# print(symptoms)
# Convert data to boolean type 
from mlxtend.preprocessing import TransactionEncoder
TE = TransactionEncoder() # Construct conversion type 
data = TE.fit_transform(symptoms) # Convert to a Boolean table 
# print(data)
# Store Boolean data as DataFrame Format 
import pandas as pd
df = pd.DataFrame(data, columns=TE.columns_)
# print(df.head())
# 3. Mining frequent itemsets 
from mlxtend.frequent_patterns import apriori
items = apriori(df, min_support=0.15, use_colnames=True)
# print(items)
# print(items[items['itemsets'].apply(lambda x:len(x))==1])
# print(items[items['itemsets'].apply(lambda x:len(x))==2])
# print(items[items['itemsets'].apply(lambda x:len(x))==3])
# print(items[items['itemsets'].apply(lambda x:len(x))==4])
# 4. According to the minimum confidence , Find strong association rules in frequent itemsets 
from mlxtend.frequent_patterns import association_rules
rules = association_rules(items, min_threshold=0.6)
# print(rules)
# 5. Extract association rules , beautify 
for i, j in rules.iterrows():
X = j['antecedents']
Y = j['consequents']
x = ', '.join([item for item in X])
y = ', '.join([item for item in Y])
print(x + ' → ' + y)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved