B_test = graph.construct_bi_graph_buyer_product(test) testBuyers, testProducts = nx.bipartite.sets(B_test) timer = MeasureTimer() def predict_random_buyers(testProducts, k): for product in testProducts: by = np.random.choice(list(buyers), int(all_c * k / 100), replace=False) yield (product, by) results = {} for k in map(int, np.linspace(100, 0, K)): dprint("Running for k: ", k) with timer: predicted = predict_random_buyers(testProducts, k) scores = validate_buyers_for_products(B_test, predicted, all_c) #results[k] = tuple(np.average(list(scores), axis=0)) results[k] = list(scores) if saveto: with open(saveto, 'wb') as f: pickle.dump(Result(results, timer), f) printResults(results) print("Average time %s" % timer.getAverage())
KNeighborsRegressor, LogisticRegression ]): with timer: all_predicted = predict_buyers_mining(testProducts, list(np.linspace(0, 1, K)), m) predicted = defaultdict(list) for p in all_predicted: predicted[p[0]].append(p[1:]) for k, p in predicted.items(): scores = validate_buyers_for_products(B_test, p, all_c) #results[idx][k] = tuple(np.average(list(scores), axis=0)) results[idx][k] = list(scores) if saveto: with open(saveto, 'wb') as f: pickle.dump(Result(results, timer), f) print("LINEAR REGRESSION") printResults(results[0]) print("RANDOM FOREST") printResults(results[1]) print("KNN") printResults(results[2]) print("LOGISTIC REGRESSION") printResults(results[3]) print("Average time %s" % timer.getAverage())
w4 = weights.weight_promotion(product_info) w5 = weights.weight_rating(product_info) #w6 = weights.combine_weights([w1, w4, w2], [0.7, 0.5, 0.3]) for wi, w in enumerate([w1, w2, w3]): def runForK(k): dprint("Running for k: ", k) with timer: predicted = predict_buyers_for_products(B, testProducts, weights.cutOffK(w, k), m) scores = validate_buyers_for_products(B_test, predicted, all_c) #return tuple(np.average(list(scores), axis=0)) return list(scores) results[mi][wi] = {k: runForK(k) for k in range(0, K)} if saveto: with open(saveto, 'wb') as f: pickle.dump(Result(results, timer),f) for i, r in results[0].items(): print("UNION W" + str(i+1)) printResults(r) print() for i, r in results[1].items(): print("INTERSECT W" + str(i+1)) printResults(r) print("Average time %s" % timer.getAverage())
for k in map(int, np.linspace(230, 240, K)): dprint("Running for k: ", k, k2) predictedBuyers = predict_buyers_for_products( B, testProducts, weights.cutOffK(weights.bipartite_products_weights(B), k)) product_buyers = { product: buyers for product, buyers in predictedBuyers } predicted = list(m(buyer_products, product_buyers)) #scores = validate_buyers_for_products(B_test, predicted, all_c) scores = validate_products_for_buyers(B_test, predicted, all_c) #results[(k, k2)] = tuple(np.average(list(scores), axis=0)) results[mi][(k, k2)] = list(scores) if saveto: with open(saveto, 'wb') as f: pickle.dump(Result(results, timer), f) print('Intersect:') printResults(results[0]) print('Union:') printResults(results[1]) print("Average time %s" % timer.getAverage())