Search code examples
pythonassociationsdata-miningrulesapriori

Association Rule Mining in python on Census Data


Hello everyone, I am working on a project. I need to perform Association Rule Mining on a Census Data Which Looks Like the image given below.Census Data

I am using the Apriori Algorithm from the mlxtend library. Here is the Code.

# Library Imports
import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules

# Reading the Data File
data = pd.read_csv("Census.csv")

# Reading Certain Columns of the Data File.
df = data[["Region","Residence Type","Sex","Student"]]

# Initializing the Transaction Encoder 
te = TransactionEncoder()

# Fitting the Data.
te_ary = te.fit(df).transform(df)

# Creating a Dataframe of Support and Element name
df2 = pd.DataFrame(te_ary, columns=te.columns_)

# Calling in the Apriori Algorithm.
fre = apriori(df2,min_support=0.6,use_colnames=True)

# Calling the Association Rule Function.
association_rules(fre, metric="confidence",min_threshold=0.7)

But the fre variable does not returns any rules, it is always empty. Can someone help me please.It is request.


Solution

  • I tried many ways and a friend of mine suggested me a way for solving my problem. It worked in a graceful way, but yes it requires your logic too. This is the link which helped me to implement my question in a better way, here it is. In my solution I used correlating parameters and created a basket.

    This is it. Thank you.