Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

07/12/23, 20.

23 Tugas_9 - Jupyter Notebook

In [42]: !pip install pandas


!pip install numpy
!pip install apyori
Requirement already satisfied: pandas in c:\users\this-pc\anaconda3\lib\si
te-packages (2.0.3)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\this-pc
\anaconda3\lib\site-packages (from pandas) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in c:\users\this-pc\anaconda3
\lib\site-packages (from pandas) (2023.3.post1)
Requirement already satisfied: tzdata>=2022.1 in c:\users\this-pc\anaconda
3\lib\site-packages (from pandas) (2023.3)
Requirement already satisfied: numpy>=1.21.0 in c:\users\this-pc\anaconda3
\lib\site-packages (from pandas) (1.24.3)
Requirement already satisfied: six>=1.5 in c:\users\this-pc\anaconda3\lib
\site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)
Requirement already satisfied: numpy in c:\users\this-pc\anaconda3\lib\sit
e-packages (1.24.3)
Requirement already satisfied: apyori in c:\users\this-pc\anaconda3\lib\si
te-packages (1.1.2)

In [43]: # Memanggil library yang dibutuhkan


import pandas as pd
import numpy as np
from apyori import apriori

localhost:8888/notebooks/Tugas_9.ipynb 1/4
07/12/23, 20.23 Tugas_9 - Jupyter Notebook

In [44]: # Loading the Data


df= pd.read_excel('Tb_Transaksi.xlsx')
df
Out[44]:
Tanggal ID Transaksi Item Transaksi

0 2020-09-12 PJ0001 Kerudung, Tas, Gamis, Khimar, Rok

1 2020-09-13 PJ0002 Celana, Kemeja, Jaket, Khimar, Gamis

2 2020-09-14 PJ0003 Khimar, Kerudung, Kemeja, Dompet, Gamis

3 2020-09-15 PJ0004 Hand Sock, Khimar, Kemeja, Bros, Kerudung

4 2020-09-16 PJ0005 Dompet, Kemeja, Tas, Kaos Kaki, Hand Sock

5 2020-09-17 PJ0006 Sepatu, Celana, Bros, Kemeja

6 2020-09-18 PJ0007 Kemeja, Celana, Khimar, Jam Tangan, Rok

7 2020-09-19 PJ0008 Kaos Kaki, Khimar, Gamis, Jaket, Kerudung

8 2020-09-20 PJ0009 Gamis, Dompet, Bros, Rok, Kerudung, Khimar

9 2020-09-21 PJ0010 Jaket, Sepatu, Gamis, Khimar, Kerudung

10 2020-09-22 PJ0011 Gamis, Tas, Kemeja, Masker, Rok, Khimar

11 2020-09-23 PJ0012 Masker, Jaket, Celana, Jam Tangan, Gamis, Rok

12 2020-09-24 PJ0013 Rok, Khimar, Gamis, Masker, Kerudung, Dompet

13 2020-09-25 PJ0014 Rok, Hand Sock, Kerudung, Khimar, Jaket

14 2020-09-26 PJ0015 Kerudung, Dompet, Sepatu, Bros

15 2020-09-27 PJ0016 Kaos Kaki, Kerudung, Dompet, Kemeja, Gamis

16 2020-09-28 PJ0017 Jam Tangan, Kerudung, Celana, Khimar, Gamis

17 2020-09-29 PJ0018 Rok, Kemeja, Jam Tangan, Hand Sock, Kerudung

18 2020-09-30 PJ0019 Celana, Kerudung, Jaket, Gamis, Rok, Kemeja

19 2020-10-01 PJ0020 Dompet, Hand Sock, Gamis, Bros, Khimar, Kerudung

In [45]: # Membuang kolom Tanggal, ID Transaksi


data=df.drop(['Tanggal','ID Transaksi'],axis=1)

localhost:8888/notebooks/Tugas_9.ipynb 2/4
07/12/23, 20.23 Tugas_9 - Jupyter Notebook

In [46]: # Membuat list dalam list dari transaksi pembelian barang


records = []
for i in range(data.shape[0]):
records.append([str(data.values[i,j]).split(',') for j in range(data.sha

trx = [[] for trx in range(len(records))]
for i in range(len(records)):
for j in records[i][0]:
trx[i].append(j)
trx
Out[46]: [['Kerudung', ' Tas', ' Gamis', ' Khimar', ' Rok '],
['Celana', ' Kemeja', ' Jaket', ' Khimar', ' Gamis '],
['Khimar', ' Kerudung', ' Kemeja', ' Dompet', ' Gamis '],
['Hand Sock', ' Khimar', ' Kemeja', ' Bros', ' Kerudung '],
['Dompet', ' Kemeja', ' Tas', ' Kaos Kaki', ' Hand Sock'],
['Sepatu', ' Celana', ' Bros', ' Kemeja'],
['Kemeja', ' Celana', ' Khimar', ' Jam Tangan', ' Rok '],
['Kaos Kaki', ' Khimar', ' Gamis', ' Jaket', ' Kerudung '],
['Gamis', ' Dompet', ' Bros', ' Rok', ' Kerudung', ' Khimar'],
['Jaket', ' Sepatu', ' Gamis', ' Khimar', ' Kerudung '],
['Gamis', ' Tas', ' Kemeja', ' Masker', ' Rok', ' Khimar '],
['Masker', ' Jaket', ' Celana', ' Jam Tangan', ' Gamis', ' Rok'],
['Rok', ' Khimar', ' Gamis', ' Masker', ' Kerudung', ' Dompet '],
['Rok', ' Hand Sock', ' Kerudung', ' Khimar', ' Jaket '],
['Kerudung', ' Dompet', ' Sepatu', ' Bros'],
['Kaos Kaki', ' Kerudung', ' Dompet', ' Kemeja', ' Gamis '],
['Jam Tangan', ' Kerudung', ' Celana', ' Khimar', ' Gamis '],
['Rok', ' Kemeja', ' Jam Tangan', ' Hand Sock', ' Kerudung '],
['Celana', ' Kerudung', ' Jaket', ' Gamis', ' Rok', ' Kemeja '],
['Dompet', ' Hand Sock', ' Gamis', ' Bros', ' Khimar', ' Kerudung']]

In [47]: # Menggunakan fungsi apriori untuk membuat asosiasi


association_rules = apriori(trx, min_support=0.14, min_confidence=0.80,min_l
# Membuat list hasil dari algoritma apriori
association_results = association_rules

localhost:8888/notebooks/Tugas_9.ipynb 3/4
07/12/23, 20.23 Tugas_9 - Jupyter Notebook

In [51]: import pandas as pd



pd.set_option('max_colwidth', 100)
result = []

for item in association_result:
pair = item[1]
for i in pair:
items = str([x for x in i])
if item[3] != 1:
result.append({
'Rule': str([x for x in i[0]]) + " -> " + str([x for x in i[
'Support': str(round(item[0] * 100, 2)) + '%',
'Confidence': str(round(item[2] * 100, 2)) + '%'
})

result_df = pd.DataFrame(result, columns=['Rule', 'Support', 'Confidence'])

# Menyajikan hasil perhitungan
print("Hasil Perhitungan:")
print(result_df)

# Menambahkan penjelasan singkat
print("\nPenjelasan:")
print("Rule: Aturan asosiasi yang ditemukan.")
print("Support: Dukungan untuk aturan tersebut, dihitung sebagai persentase.
print("Confidence: Tingkat keyakinan pada aturan tersebut, dihitung sebagai

Hasil Perhitungan:
Rule Support Confidence
0 ['A'] -> ['B'] 50.0% 80.0%
1 ['C'] -> ['D'] 50.0% 80.0%

Penjelasan:
Rule: Aturan asosiasi yang ditemukan.
Support: Dukungan untuk aturan tersebut, dihitung sebagai persentase.
Confidence: Tingkat keyakinan pada aturan tersebut, dihitung sebagai perse
ntase.

In [ ]: ​

localhost:8888/notebooks/Tugas_9.ipynb 4/4

You might also like