Apriori in Python – Step 1.) Import Libraries and Import Data

with No Comments

Before we start, we need to install the Apyori library. The Apyori is super useful if you want to create an Apriori Model because it contains modules that help the users to analyze and create model instantly.

Installing the Apyori Library

Go to the terminal of Anaconda Spyder, and do pip install

pip install apyori

After apyori is installed, go import other libraries to python.

Import Libraries 

  • Numpy is the library that does the scientific calculation. We definitely need that for an Apriori model.
  • Pandas is the library for handling Data. We use Pandas for all the Regression Model or Machine Learning Model.
  • For example, we are going to import the CSV data into python using Pandas. 
  • MatplotLib is the library for visualizing the results. It will be very useful when we plot the data later on. 

# Import the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

Data Processing before Training Apriori Model

  • We are going to import the dataset using pandas library as usual.
  • The header parameter is going to be ‘None’ because we want python to read the header column as well.
  • Say the data set has 100 records, so including the headers, we have 101 number of rows.
  • For each row in the dataset, we are going to connect the data horizontally.
  • An array name = records is going to store all the behaviors of the customers.
  • records = []  ; means creating an empty array name ‘records’.
  • And the codes below is going to connect the data in data set for each row.

for i in range(0, 101):
records.append([str(dataset.values[i,j]) for j in range(0, 10)])

i is going to run all the rows in the data and j is going to run all the columns in the data

Examples of Importing Data to Apriori model

# Import the Data
dataset = pd.read_csv(‘apriori_data.csv’, header = None)
records = [] for i in range(0, 101):
records.append([str(dataset.values[i,j]) for j in range(0, 10)])

 

Other Sections on Apriori :

Step 1.) Import Libraries and Import Dataset

Step 2.) Train a Apriori Model 

Step 3.) Visualize Apriori Results 

Other Topics – Multivariate Analysis : 
Other Topics – Association Rule : 
  • Apriori
  • Eclat
Other Topics – Artifical Inteligent : 

 

Leave a Reply