use of weka.core.Instance in project iobserve-analysis by research-iobserve.
the class ExpectationMaximizationClustering method clusterInstances.
/*
* (non-Javadoc)
*
* @see
* org.iobserve.analysis.cdoruserbehavior.clustering.IClustering#clusterInstances(weka.core.
* Instances)
*/
@Override
public Map<Integer, List<Pair<Instance, Double>>> clusterInstances(final Instances instances) {
final EM emClustering = new EM();
if (ExpectationMaximizationClustering.LOGGER.isInfoEnabled()) {
ExpectationMaximizationClustering.LOGGER.info("Computing the EM-Clustering with following options: " + emClustering.getOptions());
}
// NOPMD
final Map<Integer, List<Pair<Instance, Double>>> resultMap = new HashMap<>();
try {
emClustering.buildClusterer(instances);
/**
* iterate through all instances and bucket sort them with their probabilities to their
* assigned cluster.
*/
for (int i = 0; i < instances.numInstances(); i++) {
final Instance currentInstance = instances.instance(i);
final int cluster = emClustering.clusterInstance(currentInstance);
final double probability = emClustering.distributionForInstance(currentInstance)[cluster];
if (resultMap.get(cluster) == null) {
resultMap.put(cluster, new LinkedList<Pair<Instance, Double>>());
}
resultMap.get(cluster).add(new Pair<>(currentInstance, probability));
}
} catch (final Exception e) {
// NOPMD NOCS api induced
ExpectationMaximizationClustering.LOGGER.error("Clustering failed.", e);
}
return resultMap;
}
use of weka.core.Instance in project iobserve-analysis by research-iobserve.
the class ClusterMerger method printInstances.
private void printInstances(final Instances instances) {
for (int i = 0; i < instances.numInstances(); i++) {
String logString = "";
logString += "***************************";
logString += "Cluster " + i;
logString += "***************************";
final Instance instance = instances.instance(i);
for (int a = 0; a < instance.numAttributes(); a++) {
logString += instances.attribute(a).name() + " : " + instance.value(a);
}
ClusterMerger.LOGGER.info(logString);
}
}
use of weka.core.Instance in project iobserve-analysis by research-iobserve.
the class TBehaviorModelCreation method execute.
@Override
protected void execute(final Instances instances) {
final int size = instances.numInstances();
for (int i = 0; i < size; i++) {
final Instance instance = instances.instance(i);
final Optional<BehaviorModel> behaviorModel = this.createBehaviorModel(instances, instance);
final String modelName = this.namePrefix + i;
behaviorModel.ifPresent(model -> model.setName(modelName));
behaviorModel.ifPresent(this.outputPort::send);
}
}
use of weka.core.Instance in project iobserve-analysis by research-iobserve.
the class AbstractClustering method createInstances.
/**
* It transforms the user sessions(userSessions in form of counts of their called operation
* signatures) to Weka instances that can be used for the clustering.
*
* @param countModel
* contains the userSessions in form of counts of called operation signatures
* @param listOfDistinctOperationSignatures
* contains the extracted distinct operation signatures of the input
* entryCallSequenceModel
* @return the Weka instances that hold the data that is used for the clustering
*/
protected Instances createInstances(final List<UserSessionAsCountsOfCalls> countModel, final List<String> listOfDistinctOperationSignatures) {
final int numberOfDistinctOperationSignatures = listOfDistinctOperationSignatures.size();
final FastVector fvWekaAttributes = new FastVector(numberOfDistinctOperationSignatures);
for (int i = 0; i < numberOfDistinctOperationSignatures; i++) {
final String attributeName = "Attribute" + i;
final Attribute attribute = new Attribute(attributeName);
fvWekaAttributes.addElement(attribute);
}
final Instances clusterSet = new Instances("CallCounts", fvWekaAttributes, countModel.size());
for (final UserSessionAsCountsOfCalls userSession : countModel) {
int indexOfAttribute = 0;
final Instance instance = new Instance(numberOfDistinctOperationSignatures);
for (int row = 0; row < listOfDistinctOperationSignatures.size(); row++) {
instance.setValue((Attribute) fvWekaAttributes.elementAt(indexOfAttribute), userSession.getAbsoluteCountOfCalls()[row]);
indexOfAttribute++;
}
clusterSet.add(instance);
}
return clusterSet;
}
use of weka.core.Instance in project iobserve-analysis by research-iobserve.
the class BehaviorModelTable method toInstances.
/**
* create an Instances object for clustering.
*
* @return instance
*/
public Instances toInstances() {
final FastVector fastVector = new FastVector();
// add transitions
for (int i = 0; i < this.signatures.size(); i++) {
for (int j = 0; j < this.signatures.size(); j++) {
if (this.transitions[i][j] > AbstractBehaviorModelTable.TRANSITION_THRESHOLD) {
final Attribute attribute = new Attribute(AbstractBehaviorModelTable.EDGE_INDICATOR + this.inverseSignatures[i] + AbstractBehaviorModelTable.EDGE_DIVIDER + this.inverseSignatures[j]);
fastVector.addElement(attribute);
} else {
continue;
}
}
}
// add informations
this.signatures.values().stream().forEach(pair -> Arrays.stream(pair.getSecond()).forEach(callInformation -> fastVector.addElement(new Attribute(AbstractBehaviorModelTable.INFORMATION_INDICATOR + this.inverseSignatures[pair.getFirst()] + AbstractBehaviorModelTable.INFORMATION_DIVIDER + callInformation.getSignature()))));
// TODO name
final Instances instances = new Instances("Test", fastVector, 0);
final Instance instance = this.toInstance();
instances.add(instance);
return instances;
}
Aggregations