<jupyter_start><jupyter_text>**Assignment 2**               ![Spark logo](https://spark.apache.org/images/spark-logo-trademark.png)                                    Association Rule Mining Assignment.      I will answer a few questions using FP growth on a Retail data set.          We will start by setting and grouping the dataset by  'InvoiceNo' and will concatenate 'StockCode' data and create a .txt file that we use as training data.<jupyter_code>import pandas as pd
df = pd.read_csv('Retail.csv', encoding = "ISO-8859-1")
s2 =df.groupby('InvoiceNo')['StockCode'].apply(','.join)
s2 = s2.reset_index()
s2[:5].to_csv('Retail.txt', columns = ["StockCode"], header=False, index=False)
raw_rdd = s2<jupyter_output><empty_output><jupyter_text>I will run a count on the dataset<jupyter_code>raw_rdd.count()<jupyter_output><empty_output><jupyter_text>We will take the first 5 .<jupyter_code>s2.head(5)<jupyter_output><empty_output><jupyter_text>1) How many frequent itemsets were found with minimum support of .15?<jupyter_code>from pyspark.mllib.fpm import FPGrowth
from pyspark import SparkContext
sc=SparkContext()
data=sc.textFile("Retail.txt")
transactions=data.map(lambda line: line.strip().split(','))
model=FPGrowth.train(transactions, minSupport=0.15, numPartitions=10)
freqItems=model.freqItemsets().collect()
len(freqItems)<jupyter_output><empty_output><jupyter_text>2) How many association rules were identified with a minimum confidence of .4?<jupyter_code>freqItemsetsDict={}
for i in range(0,len(freqItems)):
    freqItemsetsDict[str(freqItems[i].items)]=freqItems[i].freq
    
miniConfidence=.4
rules=sorted(model._java_model.generateAssociationRules(miniConfidence).collect(),key=lambda x:x.confidence(),reverse =True)
len (rules)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12485939257592801, 'fraction_numerical': 0.015185601799775027, 'mean_word_length': 5.976470588235294, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '96959', 'license': 'Apache-2.0', 'n_tokens_mistral': 620, 'n_tokens_neox': 587, 'n_words': 131, 'path': 'assignments/assignment2.ipynb', 'repo_name': 'himatisa/BigData', 'size': '1778'}