OneCompiler

DS9

106

Q. 1) Write a JavaScript function to validate username and password for a membership form.
[Marks 15]
// Function to validate username and password
function validateForm() {
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;

// Check if username is empty
if (username === '') {
    alert('Please enter a username');
    return false;
}

// Check if password is empty or less than 6 characters
if (password === '' || password.length < 6) {
    alert('Please enter a password with at least 6 characters');
    return false;
}

// Validation successful
return true;

}

<form onsubmit="return validateForm()"> Username: <input type="text" id="username"><br> Password: <input type="password" id="password"><br> <input type="submit" value="Submit"> </form>

Q. 2)Create your own transactions dataset and apply the above process on your dataset
import pandas as pd
from mlxtend.frequent_patterns import apriori, association_rules

Load the dataset

data = pd.read_csv('transactions.csv')

Display information about the dataset

print("Dataset Information:")
print(data.info())

Preprocess the data (split items and remove leading/trailing whitespaces)

data['Items'] = data['Items'].str.split(',').apply(lambda x: [item.strip() for item in x])

Convert the data into the correct format for Apriori algorithm

data_encoded = pd.get_dummies(data['Items'].apply(pd.Series).stack()).sum(level=0)

Apply the Apriori algorithm

frequent_itemsets = apriori(data_encoded, min_support=0.2, use_colnames=True)
print("\nFrequent itemsets:")
print(frequent_itemsets)

Generate association rules

rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.5)
print("\nAssociation rules:")
print(rules)