List of usage examples for weka.core Instances classAttribute
publicAttribute classAttribute()
From source file:moa.classifiers.macros.TACNB.java
License:Open Source License
public void initHeader(Instances dataset) { int numLabels = this.numOldLabelsOption.getValue(); Attribute target = dataset.classAttribute(); List<String> possibleValues = new ArrayList<String>(); int n = target.numValues(); for (int i = 0; i < n; i++) { possibleValues.add(target.value(i)); }//from ww w . j a v a2s . co m ArrayList<Attribute> attrs = new ArrayList<Attribute>(numLabels + dataset.numAttributes()); for (int i = 0; i < numLabels; i++) { attrs.add(new Attribute(target.name() + "_" + i, possibleValues)); } for (int i = 0; i < dataset.numAttributes(); i++) { attrs.add((Attribute) dataset.attribute(i).copy()); } this.header = new Instances("extended_" + dataset.relationName(), attrs, 0); this.header.setClassIndex(numLabels + dataset.classIndex()); }
From source file:moa.clusterer.outliers.Sieve.java
License:Apache License
/** * Todo - move to utils package/* ww w . ja v a 2s .co m*/ * @param v * @param h */ public final static void safeNormalize(double[] v, Instances h) { int outlierIdx = h.classAttribute().indexOfValue(AbstractNovelClassClassifier.OUTLIER_LABEL_STR); if (outlierIdx < 0) { outlierIdx = h.numAttributes(); } double sum = 0; for (int i = 0; i < v.length && i < outlierIdx; ++i) { sum += v[i]; } if (sum != 0) { for (int i = 0; i < v.length && i < outlierIdx; ++i) { v[i] /= sum; } } }
From source file:moa.clusterer.outliers.Sieve.java
License:Apache License
/** * Todo - move to utils package/*from ww w .ja va 2s. c o m*/ * @param v * @param h * @return */ public final static int maxNonOutlier(double[] v, Instances h) { int outlierIdx = h.classAttribute().indexOfValue(AbstractNovelClassClassifier.OUTLIER_LABEL_STR); if (outlierIdx < 0) { outlierIdx = h.numAttributes(); } int maxIdx = 0; double maxVal = 0; for (int i = 0; i < v.length && i < outlierIdx; ++i) { if (v[i] > maxVal) { maxIdx = i; maxVal = v[i]; } } return maxIdx; }
From source file:moa.streams.ConceptDriftRealStream.java
License:Open Source License
@Override public void prepareForUseImpl(TaskMonitor monitor, ObjectRepository repository) { this.inputStream = (InstanceStream) getPreparedClassOption(this.streamOption); this.driftStream = (InstanceStream) getPreparedClassOption(this.driftstreamOption); this.random = new Random(this.randomSeedOption.getValue()); numberInstanceStream = 0;//from w w w . j av a 2 s. c o m if (this.alphaOption.getValue() != 0.0) { this.widthOption.setValue((int) (1 / Math.tan(this.alphaOption.getValue() * Math.PI / 180))); } // generate header Instances first = this.inputStream.getHeader(); Instances second = this.driftStream.getHeader(); FastVector newAttributes = new FastVector(); for (int i = 0; i < first.numAttributes() - 1; i++) { newAttributes.addElement(first.attribute(i)); } for (int i = 0; i < second.numAttributes() - 1; i++) { newAttributes.addElement(second.attribute(i)); } Attribute classLabels; if (first.numClasses() < second.numClasses()) { classLabels = second.classAttribute(); } else { classLabels = first.classAttribute(); } newAttributes.addElement(classLabels); this.streamHeader = new InstancesHeader( new Instances(getCLICreationString(InstanceStream.class), newAttributes, 0)); this.streamHeader.setClassIndex(this.streamHeader.numAttributes() - 1); restart(); }
From source file:myclassifier.MyC45.java
/** * Method building ID3 tree.//from ww w.j a v a 2 s.c om * * @param data the training data * @exception Exception if decision tree can't be built successfully */ private void makeTree(Instances data) throws Exception { // Check if no instances have reached this node. if (data.numInstances() == 0) { m_Attribute = null; m_ClassValue = -1; //Instance.missingValue(); m_Distribution = new double[data.numClasses()]; return; } // Compute attribute with maximum information gain. double[] gainRatios = new double[data.numAttributes()]; Enumeration attEnum = data.enumerateAttributes(); while (attEnum.hasMoreElements()) { Attribute att = (Attribute) attEnum.nextElement(); gainRatios[att.index()] = computeGainRatio(data, att); } m_Attribute = data.attribute(Utils.maxIndex(gainRatios)); // Make leaf if information gain is zero. // Otherwise create successors. if (Utils.eq(gainRatios[m_Attribute.index()], 0)) { m_Attribute = null; m_Distribution = new double[data.numClasses()]; Enumeration instEnum = data.enumerateInstances(); while (instEnum.hasMoreElements()) { Instance inst = (Instance) instEnum.nextElement(); m_Distribution[(int) inst.classValue()]++; } Utils.normalize(m_Distribution); m_ClassValue = Utils.maxIndex(m_Distribution); m_ClassAttribute = data.classAttribute(); } else { Instances[] splitData = splitData(data, m_Attribute); m_Successors = new MyC45[m_Attribute.numValues()]; for (int j = 0; j < m_Attribute.numValues(); j++) { m_Successors[j] = new MyC45(); m_Successors[j].makeTree(splitData[j]); } } }
From source file:myclassifier.naiveBayes.java
public void Klasifikasi(String filename) throws Exception { // load unlabeled data and set class attribute Instances unlabeled = ConverterUtils.DataSource.read("unlabeled_" + filename); unlabeled.setClassIndex(unlabeled.numAttributes() - 1); // create copy Instances labeled = new Instances(unlabeled); // label instances for (int i = 0; i < unlabeled.numInstances(); i++) { double clsLabel = NBClassifier.classifyInstance(labeled.instance(i)); labeled.instance(i).setClassValue(clsLabel); }//www.j a v a2 s . com // save newly labeled data ConverterUtils.DataSink.write("labeled_" + filename, labeled); //print hasil System.out.println("Classification Result"); System.out.println("# - actual - predicted - distribution"); for (int i = 0; i < labeled.numInstances(); i++) { double pred = NBClassifier.classifyInstance(labeled.instance(i)); double[] dist = NBClassifier.distributionForInstance(labeled.instance(i)); System.out.print((i + 1) + " - "); System.out.print(labeled.instance(i).toString(labeled.classIndex()) + " - "); System.out.print(labeled.classAttribute().value((int) pred) + " - "); System.out.println(Utils.arrayToString(dist)); } }
From source file:myclassifier.wekaCode.java
public static void classifyUnseenData(String[] attributes, Classifier classifiers, Instances data) throws Exception { Instance newInstance = new Instance(data.numAttributes()); newInstance.setDataset(data);//from w w w . j ava2 s .c o m for (int i = 0; i < data.numAttributes() - 1; i++) { if (Attribute.NUMERIC == data.attribute(i).type()) { Double value = Double.valueOf(attributes[i]); newInstance.setValue(i, value); } else { newInstance.setValue(i, attributes[i]); } } double clsLabel = classifiers.classifyInstance(newInstance); newInstance.setClassValue(clsLabel); String result = data.classAttribute().value((int) clsLabel); System.out.println("Hasil Classify Unseen Data Adalah: " + result); }
From source file:myclusterer.WekaCode.java
public static void classifyUnseenData(String[] attributes, Clusterer clusterer, Instances data) throws Exception { Instance newInstance = new Instance(data.numAttributes()); newInstance.setDataset(data);/*from w w w. java 2 s.co m*/ for (int i = 0; i < data.numAttributes() - 1; i++) { if (Attribute.NUMERIC == data.attribute(i).type()) { Double value = Double.valueOf(attributes[i]); newInstance.setValue(i, value); } else { newInstance.setValue(i, attributes[i]); } } double clsLabel = clusterer.clusterInstance(newInstance); newInstance.setClassValue(clsLabel); String result = data.classAttribute().value((int) clsLabel); System.out.println("Hasil Classify Unseen Data Adalah: " + result); }
From source file:myID3.MyId3.java
/** * Construct the tree using the given instance * Find the highest attribute value which best at dividing the data * @param data Instance/*from w ww. ja va 2 s .c o m*/ */ public void buildTree(Instances data) { if (data.numInstances() > 0) { // Lets find the highest Information Gain! // First compute each information gain attribute double IG[] = new double[data.numAttributes()]; Enumeration enumAttribute = data.enumerateAttributes(); while (enumAttribute.hasMoreElements()) { Attribute attribute = (Attribute) enumAttribute.nextElement(); IG[attribute.index()] = informationGain(data, attribute); // System.out.println(attribute.toString() + ": " + IG[attribute.index()]); } // Assign it as the tree attribute! currentAttribute = data.attribute(maxIndex(IG)); //System.out.println(Arrays.toString(IG) + IG[currentAttribute.index()]); // IG = 0 then current node = leaf! if (Utils.eq(IG[currentAttribute.index()], 0)) { // Set the class value as the highest frequency of the class currentAttribute = null; classDistribution = new double[data.numClasses()]; Enumeration enumInstance = data.enumerateInstances(); while (enumInstance.hasMoreElements()) { Instance temp = (Instance) enumInstance.nextElement(); classDistribution[(int) temp.classValue()]++; } Utils.normalize(classDistribution); classValue = Utils.maxIndex(classDistribution); classAttribute = data.classAttribute(); } else { // Create another node from the current tree Instances[] splitData = splitDataByAttribute(data, currentAttribute); nodes = new MyId3[currentAttribute.numValues()]; for (int i = 0; i < currentAttribute.numValues(); i++) { nodes[i] = new MyId3(); nodes[i].buildTree(splitData[i]); } } } else { classAttribute = null; classValue = Utils.missingValue(); classDistribution = new double[data.numClasses()]; } }
From source file:myid3andc45classifier.Model.MyC45.java
public void makeMyC45Tree(Instances data) throws Exception { if (data.numInstances() == 0) { attribute = null;/*from w w w . jav a 2 s .c o m*/ label = Instance.missingValue(); return; } //System.out.println("NEW"); double[] infoGainRatios = new double[data.numAttributes()]; Enumeration attEnum = data.enumerateAttributes(); while (attEnum.hasMoreElements()) { Attribute att = (Attribute) attEnum.nextElement(); if (!att.isNumeric()) infoGainRatios[att.index()] = computeInfoGainRatio(data, att); else infoGainRatios[att.index()] = Double.NEGATIVE_INFINITY; //System.out.println(att.name() + " " + infoGainRatios[att.index()]); } // TODO: build the tree attribute = data.attribute(maxIndex(infoGainRatios)); //System.out.println(infoGainRatios[maxIndex(infoGainRatios)]); // Make leaf if information gain is zero. // Otherwise create successors. if (infoGainRatios[maxIndex(infoGainRatios)] <= epsilon || Double.isNaN(infoGainRatios[maxIndex(infoGainRatios)])) { attribute = null; double[] numClasses = new double[data.numClasses()]; Enumeration instEnum = data.enumerateInstances(); while (instEnum.hasMoreElements()) { Instance inst = (Instance) instEnum.nextElement(); numClasses[(int) inst.classValue()]++; } label = maxIndex(numClasses); classAttribute = data.classAttribute(); } else { classAttribute = data.classAttribute(); Instances[] splitData = splitInstancesByAttribute(data, attribute); Instances[] distrData = splitInstancesByAttribute(data, data.classAttribute()); distribution = new double[distrData.length]; for (int j = 0; j < distribution.length; j++) { distribution[j] = distrData[j].numInstances(); } successors = new MyC45[attribute.numValues()]; for (int j = 0; j < attribute.numValues(); j++) { successors[j] = new MyC45(); successors[j].buildClassifier(splitData[j]); } } // TODO: prune //pruneTree(data); }