DS10
Q. 1) Create a HTML fileto insert text before and after a Paragraph using jQuery. [Hint : Use before( )
and after( )]
[Marks 15]
$("#insertAfterBtn").click(function(){
$("p").after("<span>This text is inserted after the paragraph.</span>");
});
});
</script>
Q. 2)Create the following dataset in python & Convert the categorical values into numeric format.Apply
the apriori algorithm on the above dataset to generate the frequent itemsets and association rules. Repeat
the process with different min_sup values. [Marks 15]
TID Items
1 'eggs', 'milk','bread'
2 'eggs', 'apple'
3 'milk', 'bread'
4 'apple', 'milk'
5 'milk', 'apple', 'bread'
import pandas as pd
from mlxtend.frequent_patterns import apriori, association_rules
Create the dataset
data = {'TID': [1, 2, 3, 4, 5],
'Items': [['eggs', 'milk', 'bread'],
['eggs', 'apple'],
['milk', 'bread'],
['apple', 'milk'],
['milk', 'apple', 'bread']]}
Convert the dataset into a DataFrame
df = pd.DataFrame(data)
Convert categorical values into numeric format using one-hot encoding
data_encoded = pd.get_dummies(df['Items'].apply(pd.Series).stack()).sum(level=0)
Apply the Apriori algorithm
min_support_values = [0.2, 0.3] # Different min_sup values
for min_support in min_support_values:
frequent_itemsets = apriori(data_encoded, min_support=min_support, use_colnames=True)
print(f"\nFrequent itemsets with min_sup={min_support}:\n")
print(frequent_itemsets)
# Generate association rules
rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.5)
print(f"\nAssociation rules with min_sup={min_support}:\n")
print(rules)