OneCompiler

DS15

195

Q. 1) Write Ajax program to fetch suggestions when is user is typing in a textbox. (eg like google
suggestions. Hint create array of suggestions and matching string will be displayed) [Marks 15]

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Suggestions</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("#searchBox").keyup(function(){ var inputText = $(this).val(); $.ajax({ type: "POST", url: "get_suggestions.php", data: { input: inputText }, success: function(response){ $("#suggestionBox").html(response); } }); }); }); </script> </head> <body> <input type="text" id="searchBox" placeholder="Type here"> <div id="suggestionBox"></div> </body> </html> <?php $suggestions = array("apple", "banana", "orange", "mango", "grapes", "pineapple", "strawberry", "blueberry", "raspberry", "watermelon"); $input = $_POST['input']; $matches = array(); foreach ($suggestions as $suggestion) { if (strpos($suggestion, $input) !== false) { $matches[] = $suggestion; } } foreach ($matches as $match) { echo "<div>" . $match . "</div>"; } ?>

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

Create the dataset

data = {'TID': [1, 2, 3, 4],
'Items': [['apple', 'mango', 'banana'],
['mango', 'banana', 'orange'],
['mango', 'banana', 'orange'],
['apple', 'orange']]}

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 with different min_sup values

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)