New paste Repaste Download
#hits#
import numpy as np
# Load the dataset
ratings = np.loadtxt('ratings.csv', delimiter=',')
# Initialize the user and item vectors
num_users = np.max(ratings[:, 0]) + 1
num_items = np.max(ratings[:, 1]) + 1
user_vectors = np.random.rand(num_users, 10)
item_vectors = np.random.rand(num_items, 10)
# Set the learning rate and regularization parameter
learning_rate = 0.01
regularization = 0.001
# Iterate over the dataset multiple times
for epoch in range(10):
  # Iterate over each user-item rating
  for user, item, rating in ratings:
    # Compute the predicted rating
    prediction = np.dot(user_vectors[user], item_vectors[item])
    
    # Compute the error
    error = rating - prediction
    
    # Update the user and item vectors
    user_vectors[user] += learning_rate * (error * item_vectors[item] - regularization * user_vectors[user])
    item_vectors[item] += learning_rate * (error * user_vectors[user] - regularization * item_vectors[item])
# Print the final user and item vectors
print(user_vectors)
print(item_vectors)
#page rank#
import numpy as np
# Initialize the adjacency matrix
adjacency_matrix = np.array([[0, 1, 0, 0],
                              [1, 0, 1, 1],
                              [0, 1, 0, 0],
                              [1, 1, 0, 0]])
# Initialize the PageRank scores
num_nodes = adjacency_matrix.shape[0]
page_rank_scores = np.ones(num_nodes) / num_nodes
# Set the damping factor
damping_factor = 0.85
# Iterate until convergence
while True:
    # Perform the matrix-vector multiplication
    new_page_rank_scores = damping_factor * adjacency_matrix.dot(page_rank_scores)
    new_page_rank_scores += (1 - damping_factor) / num_nodes
    # Check for convergence
    if np.allclose(page_rank_scores, new_page_rank_scores):
        break
    page_rank_scores = new_page_rank_scores
# Print the final PageRank scores
print(page_rank_scores)
#aprioriapriori#
import numpy as np
# Load the dataset
transactions = np.loadtxt('transactions.csv', delimiter=',')
# Set the minimum support threshold
min_support = 0.5
# Initialize the candidate itemsets
candidate_itemsets = set()
for transaction in transactions:
    for item in transaction:
        candidate_itemsets.add(frozenset([item]))
# Iterate over the levels of the lattice
while candidate_itemsets:
    # Count the occurrences of each candidate itemset
    itemset_counts = {}
    for transaction in transactions:
        for itemset in candidate_itemsets:
            if itemset.issubset(transaction):
                if itemset in itemset_counts:
                    itemset_counts[itemset] += 1
                else:
                    itemset_counts[itemset] = 1
    # Filter the candidate itemsets based on the minimum support threshold
    frequent_itemsets = set()
    for itemset, count in itemset_counts.items():
        if count / len(transactions) >= min_support:
            frequent_itemsets.add(itemset)
    # Generate the candidate itemsets for the next level
    candidate_itemsets = set()
    for itemset1 in frequent_itemsets:
        for itemset2 in frequent_itemsets:
            if len(itemset1.union(itemset2)) == len(itemset1) + 1:
                candidate_itemsets.add(itemset1.union(itemset2))
# Print the frequent itemsets
print(frequent_itemsets)
#Association rule#
import numpy as np
# Load the dataset
transactions = np.loadtxt('transactions.csv', delimiter=',')
# Set the minimum support and confidence thresholds
min_support = 0.5
min_confidence = 0.5
# Calculate the frequent itemsets using the Apriori algorithm
frequent_itemsets = set()
candidate_itemsets = set()
for transaction in transactions:
    for item in transaction:
        candidate_itemsets.add(frozenset([item]))
while candidate_itemsets:
    itemset_counts = {}
    for transaction in transactions:
        for itemset in candidate_itemsets:
            if itemset.issubset(transaction):
                if itemset in itemset_counts:
                    itemset_counts[itemset] += 1
                else:
                    itemset_counts[itemset] = 1
    frequent_itemsets.update(itemset_counts.keys())
    candidate_itemsets = set()
    for itemset1 in frequent_itemsets:
        for itemset2 in frequent_itemsets:
            if len(itemset1.union(itemset2)) == len(itemset1) + 1:
                candidate_itemsets.add(itemset1.union(itemset2))
# Generate the association rules
association_rules = set()
for itemset in frequent_itemsets:
    for subset in frequent_itemsets:
        if len(itemset) > len(subset) and itemset.issuperset(subset):
            confidence = itemset_counts[itemset] / itemset_counts[subset]
            if confidence >= min_confidence:
                association_rules.add((subset, itemset.difference(subset), confidence))
# Print the association rules
for rule in association_rules:
    print(rule)
Filename: None. Size: 5kb. View raw, , hex, or download this file.

This paste expires on 2024-05-23 03:46:03.922573. Pasted through web.