Package weka.core

Examples of weka.core.SparseInstance


      if (instances.classIndex() > -1)
  values[values.length - 1] = inst.value(instances.classIndex());

      // generate and add instance
      if (inst instanceof SparseInstance)
  newInst = new SparseInstance(instances.instance(i).weight(), values);
      else
  newInst = new DenseInstance(instances.instance(i).weight(), values);
      result.add(newInst);
    }
View Full Code Here


          return;
        }
       
        if((m_classIndex >= 0) &&
        (m_data.attribute(m_classIndex).isNominal())) {
          SparseInstance histClassCounts[];
          histClassCounts = new SparseInstance[m_data.attribute(m_attribIndex).numValues()];
                                  //[m_data.attribute(m_classIndex).numValues()+1];
         
          if (m_as.nominalWeights.length > 0) {
            m_maxValue = m_as.nominalWeights[0];
            for(int i=0; i<m_data.attribute(m_attribIndex).numValues(); i++) {
              if(m_as.nominalWeights[i]>m_maxValue)
          m_maxValue = m_as.nominalWeights[i];
            }
          }
          else {
            m_maxValue = 0;
          }
         
          if(m_colorList.size()==0)
            m_colorList.addElement(Color.black);
          for(int i=m_colorList.size();
          i < m_data.attribute(m_classIndex).numValues()+1; i++) {
            Color pc = m_defaultColors[(i-1) % 10];
            int ija =  (i-1) / 10;
            ija *= 2;
           
            for (int j=0;j<ija;j++) {
              pc = pc.darker();
            }
           
            m_colorList.addElement(pc);
          }
         
          // first sort data on attribute values
          m_data.sort(m_attribIndex);
          double[] tempClassCounts = null;
          int tempAttValueIndex = -1;
         
          for(int k=0; k<m_data.numInstances(); k++) {
            //System.out.println("attrib: "+
            //                   m_data.instance(k).value(m_attribIndex)+
            //                   " class: "+
            //                   m_data.instance(k).value(m_classIndex));
            if(!m_data.instance(k).isMissing(m_attribIndex)) {
              // check to see if we need to allocate some space here
              if (m_data.instance(k).value(m_attribIndex) != tempAttValueIndex) {
                if (tempClassCounts != null) {
                  // set up the sparse instance for the previous bar (if any)
                  int numNonZero = 0;
                  for (int z = 0; z < tempClassCounts.length; z++) {
                    if (tempClassCounts[z] > 0) {
                      numNonZero++;
                    }
                  }
                  double[] nonZeroVals = new double[numNonZero];
                  int[] nonZeroIndices = new int[numNonZero];
                  int count = 0;
                  for (int z = 0; z < tempClassCounts.length; z++) {
                    if (tempClassCounts[z] > 0) {
                      nonZeroVals[count] = tempClassCounts[z];
                      nonZeroIndices[count++] = z;
                    }
                  }
                  SparseInstance tempS =
                    new SparseInstance(1.0, nonZeroVals, nonZeroIndices, tempClassCounts.length);
                  histClassCounts[tempAttValueIndex] = tempS;
                }
               
                tempClassCounts = new double[m_data.attribute(m_classIndex).numValues() + 1];
                tempAttValueIndex = (int)m_data.instance(k).value(m_attribIndex);
               
                /* histClassCounts[(int)m_data.instance(k).value(m_attribIndex)] =
                  new double[m_data.attribute(m_classIndex).numValues()+1]; */
              }
              if(m_data.instance(k).isMissing(m_classIndex)) {
                /* histClassCounts[(int)m_data.instance(k).value(m_attribIndex)]
                               [0] += m_data.instance(k).weight(); */
                tempClassCounts[0] += m_data.instance(k).weight();
              } else {
                tempClassCounts[(int)m_data.instance(k).value(m_classIndex)+1]
                                += m_data.instance(k).weight();
               
                /*histClassCounts[(int)m_data.instance(k).value(m_attribIndex)]
                              [(int)m_data.instance(k).value(m_classIndex)+1] += m_data.instance(k).weight();*/
              }
            }
          }
         
          // set up sparse instance for last bar?
          if (tempClassCounts != null) {
            // set up the sparse instance for the previous bar (if any)
            int numNonZero = 0;
            for (int z = 0; z < tempClassCounts.length; z++) {
              if (tempClassCounts[z] > 0) {
                numNonZero++;
              }
            }
            double[] nonZeroVals = new double[numNonZero];
            int[] nonZeroIndices = new int[numNonZero];
            int count = 0;
            for (int z = 0; z < tempClassCounts.length; z++) {
              if (tempClassCounts[z] > 0) {
                nonZeroVals[count] = tempClassCounts[z];
                nonZeroIndices[count++] = z;
              }
            }
            SparseInstance tempS =
              new SparseInstance(1.0, nonZeroVals, nonZeroIndices, tempClassCounts.length);
            histClassCounts[tempAttValueIndex] = tempS;
          }
         
          //for(int i=0; i<histClassCounts.length; i++) {
          //int sum=0;
View Full Code Here

                sparseValues[count] = histClassCounts[i][j];
                sparseIndices[count++] = j;
              }
            }
           
            SparseInstance tempS =
              new SparseInstance(1.0, sparseValues, sparseIndices,
                  histClassCounts[i].length);
            histClassCountsSparse[i] = tempS;
           
          }
         
View Full Code Here

    vals[i - 1] = Utils.missingValue();
  }
      }
      Instance newInst;
      if (m_CreateSparseData) {
  newInst = new SparseInstance(1.0, vals);
      } else {
  newInst = new DenseInstance(1.0, vals);
      }
      instances.addElement(newInst);
      rowCount++;
View Full Code Here

      // Add instance to dataset
      double[] tempValues = new double[numValues];
      int[] tempIndices = new int[numValues];
      System.arraycopy(m_ValueBuffer, 0, tempValues, 0, numValues);
      System.arraycopy(m_IndicesBuffer, 0, tempIndices, 0, numValues);
      Instance inst = new SparseInstance(weight, tempValues, tempIndices, m_Data.numAttributes());
      inst.setDataset(m_Data);
     
      return inst;
    }
View Full Code Here

        vals[i] = dest.value(i);
      }
    }
    Instance inst = null;
    if (dest instanceof SparseInstance) {
      inst = new SparseInstance(dest.weight(), vals);
    } else {
      inst = new DenseInstance(dest.weight(), vals);
    }
    inst.setDataset(dest.dataset());
    return inst;
View Full Code Here

      INST_HEADERS = new InstancesHeader((Instances) obj);
      classifier.setModelContext(INST_HEADERS);
      classifier.resetLearningImpl();
    }else if(obj.getClass() == SparseInstance.class){
      //If it's an instance
      SparseInstance inst = (SparseInstance) obj;
      //Emit the entire prediction array and the correct value
      collector.emit(new Values(classifier.getVotesForInstance(inst), inst.classValue()));
      //Train on instance
      classifier.trainOnInstanceImpl(inst);
      //Send out our latest classifier
      ClassifierInstance latestClassifier = classifier.getLatestClassifier();
      if(latestClassifier != null)
View Full Code Here

    if(obj.getClass() == Instances.class){
      INST_HEADERS = new InstancesHeader((Instances) obj);
      classifier.setModelContext(INST_HEADERS);
      classifier.resetLearningImpl();
    }else{
      SparseInstance inst = (SparseInstance) obj;
      //Emit the entire prediction array and the correct value
      collector.emit(new Values(classifier.getVotesForInstance(inst), inst.classValue()));
      //Train on instance
      classifier.trainOnInstanceImpl(inst);
    }
   
    collector.ack(input);
View Full Code Here

        SparseData bagOfWordsData,
        SortedMap<Long, String> instanceClasses) throws IOException {
      for (Map.Entry<Long, String> entry : instanceClasses.entrySet()) {
        double[] zeroValues = new double[instances.numAttributes()];
        Arrays.fill(zeroValues, 0.0d);
        SparseInstance wekaInstance = new SparseInstance(1.0d,
            zeroValues);
        wekaInstance.setDataset(instances);
        // set instance id
        Attribute instanceId = instances.attribute(INSTANCE_ID);
        wekaInstance.setValue(instanceId.index(), entry.getKey()
            .doubleValue());
        // set document class
        Attribute classAttr = instances.attribute(CLASS);
        wekaInstance.setValue(classAttr.index(),
            classAttr.indexOfValue(entry.getValue()));
        // set numeric words
        if (bagOfWordsData.getInstanceNumericWords()
            .get(entry.getKey()) != null) {
          for (Map.Entry<String, Double> word : bagOfWordsData
              .getInstanceNumericWords().get(entry.getKey())
              .entrySet()) {
            Attribute wordAttr = instances.attribute(word.getKey());
            wekaInstance.setValue(wordAttr.index(), word.getValue()
                .doubleValue());
          }
        }
        // set nominal words
        if (bagOfWordsData.getInstanceNominalWords()
            .get(entry.getKey()) != null) {
          for (Map.Entry<String, String> word : bagOfWordsData
              .getInstanceNominalWords().get(entry.getKey())
              .entrySet()) {
            Attribute wordAttr = instances.attribute(word.getKey());
            int valueIndex = wordAttr.indexOfValue(word.getValue());
            if (valueIndex == -1) {
              throw new IOException("oops! " + word);
            }
            wekaInstance.setValue(wordAttr.index(), valueIndex);
          }
        }
        instances.add(wekaInstance);
      }
    }
View Full Code Here

      BagOfWordsData bagOfWordsData) throws IOException {
    for (Map.Entry<Integer, String> entry : bagOfWordsData
        .getDocumentClasses().entrySet()) {
      double[] zeroValues = new double[instances.numAttributes()];
      Arrays.fill(zeroValues, 0.0d);
      SparseInstance wekaInstance = new SparseInstance(1.0d, zeroValues);
      wekaInstance.setDataset(instances);
      // set instance id
      Attribute instanceId = instances.attribute(INSTANCE_ID);
      wekaInstance.setValue(instanceId.index(), entry.getKey()
          .doubleValue());
      // set document class
      Attribute classAttr = instances.attribute(CLASS);
      wekaInstance.setValue(classAttr.index(),
          classAttr.indexOfValue(entry.getValue()));
      // set numeric words
      if (bagOfWordsData.getInstanceNumericWords().get(entry.getKey()) != null) {
        for (Map.Entry<String, Double> word : bagOfWordsData
            .getInstanceNumericWords().get(entry.getKey())
            .entrySet()) {
          Attribute wordAttr = instances.attribute(word.getKey());
          wekaInstance.setValue(wordAttr.index(), word.getValue()
              .doubleValue());
        }
      }
      // set nominal words
      if (bagOfWordsData.getInstanceNominalWords().get(entry.getKey()) != null) {
        for (Map.Entry<String, String> word : bagOfWordsData
            .getInstanceNominalWords().get(entry.getKey())
            .entrySet()) {
          Attribute wordAttr = instances.attribute(word.getKey());
          int valueIndex = wordAttr.indexOfValue(word.getValue());
          if (valueIndex == -1) {
            throw new IOException("oops! " + word);
          }
          wekaInstance.setValue(wordAttr.index(), valueIndex);
        }
      }
      instances.add(wekaInstance);
    }
  }
View Full Code Here

TOP

Related Classes of weka.core.SparseInstance

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.