import sys import pickle import helpers.data as data import helpers.graph as graph import networkx as nx import numpy as np from helpers.helpers import dprint, readArgs, Result, MeasureTimer, printResults from predictor import validate_buyers_for_products (K, orderlim, saveto) = readArgs() # Load orders orders = data.cut_orders_by_repeated_buyers(data.load_orders(), orderlim) buyers = set([order['buyer'] for order in orders]) all_c = len(buyers) # Split orders into train and test sets train, test = data.split_train_set(orders) # Create test bipartite graph B_test = graph.construct_bi_graph_buyer_product(test) testBuyers, testProducts = nx.bipartite.sets(B_test) timer = MeasureTimer() def predict_random_buyers(testProducts, k):
if i1 in partitions and i2 in partitions: com_neig = list(nx.common_neighbors(B_test, i1, i2)) if len(com_neig) > 0: if partitions[i1] == partitions[i2]: # Score if they are from same partition yield len(com_neig) / ( len(list(nx.neighbors(B_test, i1))) + len(list(nx.neighbors(B_test, i2)))) else: yield 0 else: yield (partitions[i1] != partitions[i2]) scores = list(score(parts, sets[0])) return np.average(scores), len(set(parts.values())) # Get data with additional parameters orders = data.load_orders() # Split data into train and test train, test = data.split_train_set(orders) # Construct bipartite graph buyer <--> product B = graph.construct_bi_graph_buyer_product(train) # Create test bipartite graph B_test = graph.construct_bi_graph_buyer_product(test) print(segmentation(B, B_test))