Association mining

A sample container for market-basket analysis of itemset-transaction

JavaScript

// function formAssociations() {
var association_set = ["antecedent", "consequent", "support", "Confidence", "lift"];
var products = ["A", "B", "C", "D", "E", "F", "G", "H", "I"];
var orders = [
  ["A", "B"],
  ["A", "B", "C"],
  ["A", "C", "D"],
  ["B", "D", "F"],
  ["F", "G", "H"],
  ["A", "C", "I"],
  ["G", "H", "I"]
];

var associations = [];
for (var productA of products) {
  for (var productB of products) {
    if (productA != productB) {
      associations.push([productA, productB, 0, 0, 0]);
    }
  }
}

var support_X = support(orders, products);

function support(orders, elements) {
  var counts = new Array(elements.length);
  for (var element of elements) {
    for (var order of orders) {
      if (order.indexOf(element) != -1) {
        counts[elements.indexOf(element)] += 1;
      }
    }
  }

  for (int i = 0; i < counts.length; i++) {
    counts[i] /= orders.length;
  }

  return counts;
}

function confidence(orders, supports, products, antecedent, consequent) {
  var result = support(orders, [antecedent, consequent]) / support_x[products.indexOf(antecedent)];
  return result;
}

function lift(orders, supports, products, antecedent, consequent) {
  var result = confidence(orders, supports, products, antecedent, consequent) / support_x[products.indexOf(consequent)];
  return result;
}

for (int i = 0; i < associations.length; i++) {
  for (var order of orders) {
    var combined = 0;
    if (order.indexOf(associations[i][0]) != -1 && order.indexOf(associations[i][1]) != -1) {
      combined += 1;
    }
    associations[i][2] = combined / orders.length;
    associations[i][3] = confidence(orders, supports, products, associations[i][0], associations[i][1]);
    associations[i][4] = lift(orders, supports, products, associations[i][0], associations[i][1]);
  }
}
// return associations;
// }
// x = formAssociations();
console.log(associations);