List of usage examples for weka.core Instance classAttribute
public Attribute classAttribute();
From source file:moa.classifiers.featureselection.OFSP.java
License:Open Source License
@Override public void trainOnInstanceImpl(Instance inst) { double y_t, f_t, denom, m_bias; int[] indices = new int[this.numSelectOption.getValue()]; double[] m_weights; if (this.weights == null) { this.weights = new double[inst.numValues()]; for (int i = 0; i < this.weights.length; i++) this.weights[i] = this.rand.nextGaussian(); this.bias = 0.0; this.weights = truncate(this.weights, this.numSelectOption.getValue()); }//from w w w . jav a 2 s . co m if (inst.classAttribute().isNominal()) { y_t = (inst.classValue() == 0) ? -1 : 1; } else { y_t = inst.classValue(); } double[] x_t = inst.toDoubleArray(); double[] x_hat = inst.toDoubleArray(); if (this.rand.nextDouble() < this.searchOption.getValue()) { int[] indices_perm = perm(inst.numAttributes()); for (int i = 0; i < this.numSelectOption.getValue(); i++) indices[i] = indices_perm[i]; } else { int[] sorted_indices = bubblesort_index(abs_vector(this.weights)); for (int i = 0; i < inst.numAttributes() - this.numSelectOption.getValue(); i++) x_hat[sorted_indices[i]] = 0.0; for (int i = 0; i < this.numSelectOption.getValue(); i++) indices[i] = sorted_indices[sorted_indices.length - i - 1]; } f_t = 0; for (int i = 0; i < this.numSelectOption.getValue(); i++) f_t += this.weights[indices[i]] * x_t[indices[i]]; f_t += this.bias; if (f_t * y_t < 0) { for (int i = 0; i < x_hat.length; i++) { denom = this.numSelectOption.getValue() / x_hat.length * this.searchOption.getValue(); if (this.weights[i] != 0) denom += (1 - this.searchOption.getValue()) * this.weights[i]; x_hat[i] /= denom; } m_weights = scalar_vector(y_t * this.stepSizeOption.getValue(), x_hat); m_bias = y_t * this.stepSizeOption.getValue() * this.bias; m_weights = vector_add(m_weights, this.weights); m_bias += m_bias + this.bias; m_weights = l2_projection(m_weights, m_bias, this.boundOption.getValue()); m_weights = truncate(m_weights, this.numSelectOption.getValue()); for (int i = 0; i < m_weights.length - 1; i++) this.weights[i] = m_weights[i]; this.bias = m_weights[m_weights.length - 1]; } }
From source file:moa.classifiers.functions.SGD.java
License:Open Source License
/** * Trains the classifier with the given instance. * * @param instance the new training instance to include in the model *//*from w w w . j a v a 2s . com*/ @Override public void trainOnInstanceImpl(Instance instance) { if (m_weights == null) { m_weights = new DoubleVector(); m_bias = 0.0; } if (!instance.classIsMissing()) { double wx = dotProd(instance, m_weights, instance.classIndex()); double y; double z; if (instance.classAttribute().isNominal()) { y = (instance.classValue() == 0) ? -1 : 1; z = y * (wx + m_bias); } else { y = instance.classValue(); z = y - (wx + m_bias); y = 1; } // Compute multiplier for weight decay double multiplier = 1.0; if (m_numInstances == 0) { multiplier = 1.0 - (m_learningRate * m_lambda) / m_t; } else { multiplier = 1.0 - (m_learningRate * m_lambda) / m_numInstances; } for (int i = 0; i < m_weights.numValues(); i++) { m_weights.setValue(i, m_weights.getValue(i) * multiplier); } // Only need to do the following if the loss is non-zero if (m_loss != HINGE || (z < 1)) { // Compute Factor for updates double factor = m_learningRate * y * dloss(z); // Update coefficients for attributes int n1 = instance.numValues(); for (int p1 = 0; p1 < n1; p1++) { int indS = instance.index(p1); if (indS != instance.classIndex() && !instance.isMissingSparse(p1)) { m_weights.addToValue(indS, factor * instance.valueSparse(p1)); } } // update the bias m_bias += factor; } m_t++; } }
From source file:moa.classifiers.functions.SGD.java
License:Open Source License
/** * Calculates the class membership probabilities for the given test * instance./* w ww . j a va 2 s .c om*/ * * @param instance the instance to be classified * @return predicted class probability distribution */ @Override public double[] getVotesForInstance(Instance inst) { if (m_weights == null) { return new double[inst.numClasses()]; } double[] result = (inst.classAttribute().isNominal()) ? new double[2] : new double[1]; double wx = dotProd(inst, m_weights, inst.classIndex());// * m_wScale; double z = (wx + m_bias); if (inst.classAttribute().isNumeric()) { result[0] = z; return result; } if (z <= 0) { // z = 0; if (m_loss == LOGLOSS) { result[0] = 1.0 / (1.0 + Math.exp(z)); result[1] = 1.0 - result[0]; } else { result[0] = 1; } } else { if (m_loss == LOGLOSS) { result[1] = 1.0 / (1.0 + Math.exp(-z)); result[0] = 1.0 - result[1]; } else { result[1] = 1; } } return result; }
From source file:moa.classifiers.functions.SGDMultiClass.java
License:Open Source License
/** * Trains the classifier with the given instance. * * @param instance the new training instance to include in the model *//*from w w w . j a va 2 s . c o m*/ @Override public void trainOnInstanceImpl(Instance instance) { if (m_weights == null) { int length; if (instance.classAttribute().isNominal()) { length = instance.numClasses(); } else { length = 1; } m_weights = new DoubleVector[length]; m_bias = new double[length]; for (int i = 0; i < m_weights.length; i++) { m_weights[i] = new DoubleVector(); m_bias[i] = 0.0; } } for (int i = 0; i < m_weights.length; i++) { this.trainOnInstanceImpl(instance, i); } m_t++; }
From source file:moa.classifiers.functions.SGDMultiClass.java
License:Open Source License
public void trainOnInstanceImpl(Instance instance, int classLabel) { if (!instance.classIsMissing()) { double wx = dotProd(instance, m_weights[classLabel], instance.classIndex()); double y; double z; if (instance.classAttribute().isNominal()) { y = (instance.classValue() != classLabel) ? -1 : 1; z = y * (wx + m_bias[classLabel]); } else {//w w w .j ava 2 s .c o m y = instance.classValue(); z = y - (wx + m_bias[classLabel]); y = 1; } // Compute multiplier for weight decay double multiplier = 1.0; if (m_numInstances == 0) { multiplier = 1.0 - (m_learningRate * m_lambda) / m_t; } else { multiplier = 1.0 - (m_learningRate * m_lambda) / m_numInstances; } for (int i = 0; i < m_weights[classLabel].numValues(); i++) { m_weights[classLabel].setValue(i, m_weights[classLabel].getValue(i) * multiplier); } // Only need to do the following if the loss is non-zero if (m_loss != HINGE || (z < 1)) { // Compute Factor for updates double factor = m_learningRate * y * dloss(z); // Update coefficients for attributes int n1 = instance.numValues(); for (int p1 = 0; p1 < n1; p1++) { int indS = instance.index(p1); if (indS != instance.classIndex() && !instance.isMissingSparse(p1)) { m_weights[classLabel].addToValue(indS, factor * instance.valueSparse(p1)); } } // update the bias m_bias[classLabel] += factor; } } }
From source file:moa.classifiers.functions.SGDMultiClass.java
License:Open Source License
/** * Calculates the class membership probabilities for the given test * instance./*from w ww. j a v a 2s . com*/ * * @param instance the instance to be classified * @return predicted class probability distribution */ @Override public double[] getVotesForInstance(Instance inst) { if (m_weights == null) { return new double[inst.numClasses()]; } double[] result = (inst.classAttribute().isNominal()) ? new double[inst.numClasses()] : new double[1]; if (inst.classAttribute().isNumeric()) { double wx = dotProd(inst, m_weights[0], inst.classIndex());// * m_wScale; double z = (wx + m_bias[0]); result[0] = z; return result; } for (int i = 0; i < m_weights.length; i++) { double wx = dotProd(inst, m_weights[i], inst.classIndex());// * m_wScale; double z = (wx + m_bias[i]); if (z <= 0) { // z = 0; if (m_loss == LOGLOSS) { //result[0] = 1.0 / (1.0 + Math.exp(z)); //result[1] = 1.0 - result[0]; result[i] = 1.0 - 1.0 / (1.0 + Math.exp(z)); } else { //result[0] = 1; result[i] = 0; } } else { if (m_loss == LOGLOSS) { //result[1] = 1.0 / (1.0 + Math.exp(-z)); //result[0] = 1.0 - result[1]; result[i] = 1.0 / (1.0 + Math.exp(-z)); } else { //result[1] = 1; result[i] = 1; } } } return result; }
From source file:moa.classifiers.functions.SGDOld.java
License:Open Source License
/** * Trains the classifier with the given instance. * * @param instance the new training instance to include in the model *///from www . j av a 2s. co m @Override public void trainOnInstanceImpl(Instance instance) { if (m_weights == null) { m_weights = new double[instance.numAttributes() + 1]; } if (!instance.classIsMissing()) { double wx = dotProd(instance, m_weights, instance.classIndex()); double y; double z; if (instance.classAttribute().isNominal()) { y = (instance.classValue() == 0) ? -1 : 1; z = y * (wx + m_weights[m_weights.length - 1]); } else { y = instance.classValue(); z = y - (wx + m_weights[m_weights.length - 1]); y = 1; } // Compute multiplier for weight decay double multiplier = 1.0; if (m_numInstances == 0) { multiplier = 1.0 - (m_learningRate * m_lambda) / m_t; } else { multiplier = 1.0 - (m_learningRate * m_lambda) / m_numInstances; } for (int i = 0; i < m_weights.length - 1; i++) { m_weights[i] *= multiplier; } // Only need to do the following if the loss is non-zero if (m_loss != HINGE || (z < 1)) { // Compute Factor for updates double factor = m_learningRate * y * dloss(z); // Update coefficients for attributes int n1 = instance.numValues(); for (int p1 = 0; p1 < n1; p1++) { int indS = instance.index(p1); if (indS != instance.classIndex() && !instance.isMissingSparse(p1)) { m_weights[indS] += factor * instance.valueSparse(p1); } } // update the bias m_weights[m_weights.length - 1] += factor; } m_t++; } }
From source file:moa.classifiers.functions.SGDOld.java
License:Open Source License
/** * Calculates the class membership probabilities for the given test * instance./*from w w w. j a v a2 s . c om*/ * * @param instance the instance to be classified * @return predicted class probability distribution */ @Override public double[] getVotesForInstance(Instance inst) { if (m_weights == null) { return new double[inst.numAttributes() + 1]; } double[] result = (inst.classAttribute().isNominal()) ? new double[2] : new double[1]; double wx = dotProd(inst, m_weights, inst.classIndex());// * m_wScale; double z = (wx + m_weights[m_weights.length - 1]); if (inst.classAttribute().isNumeric()) { result[0] = z; return result; } if (z <= 0) { // z = 0; if (m_loss == LOGLOSS) { result[0] = 1.0 / (1.0 + Math.exp(z)); result[1] = 1.0 - result[0]; } else { result[0] = 1; } } else { if (m_loss == LOGLOSS) { result[1] = 1.0 / (1.0 + Math.exp(-z)); result[0] = 1.0 - result[1]; } else { result[1] = 1; } } return result; }
From source file:moa.classifiers.meta.RandomRules.java
License:Open Source License
private Instance transformInstance(Instance inst, int classifierIndex) { if (this.listAttributes == null) { this.numAttributes = (int) (this.numAttributesPercentageOption.getValue() * inst.numAttributes() / 100.0);/*from w w w . ja va 2 s . c o m*/ this.listAttributes = new int[this.numAttributes][this.ensemble.length]; this.dataset = new InstancesHeader[this.ensemble.length]; for (int ensembleIndex = 0; ensembleIndex < this.ensemble.length; ensembleIndex++) { for (int attributeIndex = 0; attributeIndex < this.numAttributes; attributeIndex++) { boolean isUnique = false; while (isUnique == false) { this.listAttributes[attributeIndex][ensembleIndex] = this.classifierRandom .nextInt(inst.numAttributes() - 1); isUnique = true; for (int k = 0; k < attributeIndex; k++) { if (this.listAttributes[attributeIndex][ensembleIndex] == this.listAttributes[k][ensembleIndex]) { isUnique = false; break; } } } //this.listAttributes[attributeIndex][ensembleIndex] = attributeIndex; } //Create Header FastVector attributes = new FastVector(); for (int attributeIndex = 0; attributeIndex < this.numAttributes; attributeIndex++) { attributes.addElement(inst.attribute(this.listAttributes[attributeIndex][ensembleIndex])); System.out.print(this.listAttributes[attributeIndex][ensembleIndex]); } System.out.println("Number of attributes: " + this.numAttributes + "," + inst.numAttributes()); attributes.addElement(inst.classAttribute()); this.dataset[ensembleIndex] = new InstancesHeader( new Instances(getCLICreationString(InstanceStream.class), attributes, 0)); this.dataset[ensembleIndex].setClassIndex(this.numAttributes); this.ensemble[ensembleIndex].setModelContext(this.dataset[ensembleIndex]); } } //Instance instance = new DenseInstance(this.numAttributes+1); //instance.setDataset(dataset[classifierIndex]); double[] attVals = new double[this.numAttributes + 1]; for (int attributeIndex = 0; attributeIndex < this.numAttributes; attributeIndex++) { //instance.setValue(attributeIndex, inst.value(this.listAttributes[attributeIndex][classifierIndex])); attVals[attributeIndex] = inst.value(this.listAttributes[attributeIndex][classifierIndex]); } Instance instance = new DenseInstance(1.0, attVals); instance.setDataset(dataset[classifierIndex]); instance.setClassValue(inst.classValue()); // System.out.println(inst.toString()); // System.out.println(instance.toString()); // System.out.println("============"); return instance; }
From source file:moa.clusterers.outliers.AnyOut.AnyOut.java
License:Apache License
@Override protected void ProcessNewStreamObj(Instance i) { if (trainingSetSize >= trainingCount) { if (trainingSet == null) { trainingSet = new DataSet(i.numAttributes() - 1); }/*from w w w . j ava 2 s. co m*/ //fill training set DataObject o = new DataObject(idCounter++, i); trainingSet.addObject(o); trainingCount++; } else { // Train once. if (trainingSetSize != -1) { anyout.train(trainingSet); trainingSet.clear(); trainingSetSize = -1; outlierClass = i.classAttribute().numValues() - 1; } // Create DataObject from instance. DataObject o = new DataObject(idCounter++, i); objects.add(o); // Count ground truth. if (o.getClassLabel() == outlierClass) { totalOutliers += 1; } // Update window objects. if (objects.size() > windowSize) { DataObject obj = objects.get(0); objects.remove(0); anyout.removeObject(obj.getId()); RemoveExpiredOutlier(new Outlier(obj.getInstance(), obj.getId(), obj)); } // Calculate scores for the object. anyout.initObject(o.getId(), o.getFeatures()); // Simulate anyout characteristics. double depth = Math.random(); if (depth < minDepth) { depth = minDepth; } else if (depth > maxDepth) { depth = maxDepth; } while (anyout.moreImprovementsPossible(o.getId(), depth)) { anyout.improveObjectOnce(o.getId()); } // Learn object into ClusTree. anyout.learnObject(o.getFeatures()); // Evaluation of the window objects. for (DataObject obj : objects) { int id = obj.getId(); if (anyout.isOutlier(id)) { if (obj.isOutiler() == false) { // not already outlier. // Statistics gathering. if (obj.getClassLabel() == outlierClass) { truePositive += 1; } else { falsePositive += 1; } AddOutlier(new Outlier(obj.getInstance(), id, obj)); obj.setOutiler(true); } } else { RemoveOutlier(new Outlier(obj.getInstance(), id, obj)); obj.setOutiler(false); } } } }