OneCompiler

DS10

153

Q. 1) Create a HTML fileto insert text before and after a Paragraph using jQuery. [Hint : Use before( )
and after( )]
[Marks 15]

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Insert Text Before and After Paragraph</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ $("#insertBeforeBtn").click(function(){ $("p").before("<span>This text is inserted before the paragraph.</span>"); });
$("#insertAfterBtn").click(function(){
    $("p").after("<span>This text is inserted after the paragraph.</span>");
});

});
</script>

</head> <body> <p>This is a paragraph.</p> <button id="insertBeforeBtn">Insert Before</button> <button id="insertAfterBtn">Insert After</button> </body> </html>

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)