DS6


Q. 1) Write PHP script to read “book.xml” file into simpleXML object. Display attributes and elements .
( simple_xml_load_file() function ) [Marks 15]

<?php // Load XML file into SimpleXML object $xml = simplexml_load_file("book.xml"); // Display attributes and elements echo "Attributes:\n"; foreach ($xml->attributes() as $key => $value) { echo "$key: $value\n"; } echo "\nElements:\n"; foreach ($xml->children() as $child) { echo $child->getName() . ": " . $child . "\n"; } ?>

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
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori, association_rules
import pandas as pd

Create the dataset

dataset = [
['Apple', 'Mango', 'Banana'],
['Mango', 'Banana', 'Cabbage', 'Carrots'],
['Mango', 'Banana', 'Carrots'],
['Mango', 'Carrots']
]

Convert categorical values into numeric format

te = TransactionEncoder()
te_ary = te.fit(dataset).transform(dataset)
df = pd.DataFrame(te_ary, columns=te.columns_)

Apply Apriori algorithm to generate frequent itemsets

frequent_itemsets = apriori(df, min_support=0.2, use_colnames=True)

Generate association rules

rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.7)

Display frequent itemsets and association rules

print("Frequent Itemsets:")
print(frequent_itemsets)

print("\nAssociation Rules:")
print(rules)