Package weka.core

Examples of weka.core.Instance


    // now populate the series
    if (history != null) {

      // do the actuals first
      for (int i = 0; i < history.numInstances(); i++) {
        Instance current = history.instance(i);

        for (String targetName : targetNames) {
          int dataIndex = history.attribute(targetName.trim()).index();

          if (dataIndex >= 0) {
            XYIntervalSeries actualSeries = null;
            int actualIndex = xyDataset.indexOf(targetName);
            actualSeries = xyDataset.getSeries(actualIndex);
            double x = Utils.missingValue();

            if (timeAxisIsDate) {
              x = current.value(timeIndex);
              if (!Utils.isMissingValue(x)) {
                lastRealTimeValue = x;
              }
            } else {
              x = artificialTimeStart;
            }

            double y = Utils.missingValue();
            y = current.value(dataIndex);

            if (!Utils.isMissingValue(x) && !Utils.isMissingValue(y)) {
              if (actualSeries != null) {
                actualSeries.add(x, x, x, y, y, y);
              }
View Full Code Here


    double[] newVals = new double[numAtts];
    for (int i = 0; i < newVals.length; i++) {
      newVals[i] = weka.core.Utils.missingValue();
    }
    newVals[timeIndex] = incrTime;
    Instance newInst = new DenseInstance(1.0, newVals);

    /*
     * if (missingReport != null) { if (periodicityHandler.isDateBased()) {
     * String timeFormat = "yyyy-MM-dd'T'HH:mm:ss"; SimpleDateFormat sdf = new
     * SimpleDateFormat(); sdf.applyPattern(timeFormat); Date d = new
View Full Code Here

     * necessary for detecting whether the intermediate rows are missing.
     * Similarly the missing date handling routine assumes that there aren't
     * missing rows so that it can advance the previous date value by one unit
     * in order to compute the value for the current missing date value.
     */
    Instance prevInst = null;
    int current = 0;
    while (true) {
      if (current == toInsert.numInstances()) {
        break;
      }

      if (prevInst != null && !toInsert.instance(current).isMissing(timeIndex)) {
        if (periodicityHandler.getPeriodicity() != Periodicity.MONTHLY
            && periodicityHandler.getPeriodicity() != Periodicity.QUARTERLY) {
          double delta = periodicityHandler.getPeriodicity().deltaTime();

          double diff = toInsert.instance(current).value(timeIndex)
              - prevInst.value(timeIndex);

          /*
           * if the difference is more than 1 delta then insert a new row
           */
          if (diff > 1.5 * delta) {
            if (!periodicityHandler.isDateBased()
                || !periodicityHandler.dateInSkipList(new Date((long) prevInst
                    .value(timeIndex)))) {
              Instance newInst = makeInstance(prevInst.value(timeIndex),
                  periodicityHandler, toInsert.numAttributes(), timeIndex);

              Date candidate = new Date((long) newInst.value(timeIndex));
              if (!periodicityHandler.dateInSkipList(candidate)
                  && candidate.getTime() < toInsert.instance(current).value(
                      timeIndex)) {
                newInst.setDataset(toInsert);
                toInsert.add(current, newInst);
                addToMissingReport(newInst.value(timeIndex),
                    periodicityHandler, missingReport);
              }
            }
          }
        } else {
          Date d = new Date((long) toInsert.instance(current).value(timeIndex));
          Calendar c = new GregorianCalendar();
          c.setTime(d);
          int currentMonth = c.get(Calendar.MONTH);

          d = new Date((long) prevInst.value(timeIndex));
          c.setTime(d);
          int prevMonth = c.get(Calendar.MONTH);

          double diff = (currentMonth + 1)
              - ((prevMonth == Calendar.DECEMBER) ? 0 : prevMonth + 1);

          if (periodicityHandler.getPeriodicity() == Periodicity.MONTHLY) {
            /*
             * if the difference is more than 1 month then insert a new row
             */
            if (diff > 1.5) {
              if (!periodicityHandler.isDateBased()
                  || !periodicityHandler.dateInSkipList(new Date(
                      (long) prevInst.value(timeIndex)))) {
                Instance newInst = makeInstance(prevInst.value(timeIndex),
                    periodicityHandler, toInsert.numAttributes(), timeIndex);
                Date candidate = new Date((long) newInst.value(timeIndex));
                if (!periodicityHandler.dateInSkipList(candidate)
                    && candidate.getTime() < toInsert.instance(current).value(
                        timeIndex)) {
                  newInst.setDataset(toInsert);
                  toInsert.add(current, newInst);
                  addToMissingReport(newInst.value(timeIndex),
                      periodicityHandler, missingReport);
                }
              }
            }
          } else if (periodicityHandler.getPeriodicity() == Periodicity.QUARTERLY) {
            /*
             * if the difference is more than 1 quarter (3 months) then insert a
             * new row
             */
            if (diff > 4.5) {
              if (!periodicityHandler.isDateBased()
                  || !periodicityHandler.dateInSkipList(new Date(
                      (long) prevInst.value(timeIndex)))) {
                Instance newInst = makeInstance(prevInst.value(timeIndex),
                    periodicityHandler, toInsert.numAttributes(), timeIndex);
                Date candidate = new Date((long) newInst.value(timeIndex));
                if (!periodicityHandler.dateInSkipList(candidate)
                    && candidate.getTime() < toInsert.instance(current).value(
                        timeIndex)) {
                  toInsert.add(current, newInst);
                  addToMissingReport(newInst.value(timeIndex),
                      periodicityHandler, missingReport);
                }
              }
            }
          }
View Full Code Here

          // the missing values that are created by default by the lagging
          // process showed inferior performance compared to just letting
          // Weka take care of it via mean/mode replacement

          for (int i = 0; i < result.numInstances(); i++) {
            Instance current = result.instance(i);
            if (current.isMissing(attIndex)) {
              if (!weka.core.Utils.isMissingValue(lastNonMissing)) {
                // Search forward to the next non missing value (if any)
                double futureNonMissing = weka.core.Utils.missingValue();

                double x2 = 2; // number of x steps (lastNonMissing is at 0 on x
                               // axis)
                for (int j = i + 1; j < result.numInstances(); j++) {
                  if (!result.instance(j).isMissing(attIndex)) {
                    futureNonMissing = result.instance(j).value(attIndex);
                    break;
                  }
                  x2++;
                }

                if (!weka.core.Utils.isMissingValue(futureNonMissing)) {
                  // Now do the linear interpolation
                  double offset = lastNonMissing;
                  double slope = (futureNonMissing - lastNonMissing) / x2;

                  // fill in the missing values
                  for (int j = i; j < i + x2; j++) {
                    if (result.instance(j).isMissing(attIndex)) {
                      double interpolated = (((j - i) + 1) * slope) + offset;

                      result.instance(j).setValue(attIndex, interpolated);
                      if (missingTargetList != null) {
                        missingTargetList.add(new Integer(j + 1));
                      }
                    }
                  }
                }
              } else {
                // won't do anything with start/end missing values
              }
            } else {
              lastNonMissing = current.value(attIndex);
            }
          }
        }
      }
    }

    // now check for missing date values (if necessary)
    if (timeStampAtt != null) {

      int attIndex = timeStampAtt.index(); // result.attribute(timeStampName).index();

      double firstNonMissing = result.instance(0).value(attIndex);
      double previousNonMissing = firstNonMissing;
      int firstNonMissingIndex = -1;
      boolean leadingMissingDates = weka.core.Utils
          .isMissingValue(firstNonMissing);

      for (int i = 0; i < result.numInstances(); i++) {
        Instance current = result.instance(i);

        if (current.isMissing(attIndex)) {
          if (!weka.core.Utils.isMissingValue(previousNonMissing)) {
            double newV = advanceSuppliedTimeValue(previousNonMissing, detected);
            current.setValue(attIndex, newV);
            // previousNonMissing = newV;
            if (missingTimeStampList != null) {
              missingTimeStampList.add(new Integer(i + 1));
            }
          }
        } else if (firstNonMissingIndex == -1) {
          firstNonMissingIndex = i;
          firstNonMissing = current.value(attIndex);
        }
        previousNonMissing = current.value(attIndex);
      }

      if (leadingMissingDates) {
        if (firstNonMissingIndex > 0) {
          for (int i = firstNonMissingIndex - 1; i >= 0; i--) {
            Instance current = result.instance(i);
            double newV = decrementSuppliedTimeValue(firstNonMissing, detected);
            current.setValue(attIndex, newV);
            if (missingTimeStampList != null) {
              missingTimeStampList.add(new Integer(i + 1));
            }
            firstNonMissing = newV;
          }
View Full Code Here

   
    // set all targets to missing
    List<String> fieldsToForecast =
      AbstractForecaster.stringToList(forecaster.getFieldsToForecast());
    for (int i = 0; i < overlay.numInstances(); i++) {
      Instance current = overlay.instance(i);
      for (String target : fieldsToForecast) {
        current.setValue(overlay.attribute(target), Utils.missingValue());
      }
    }

    return overlay;
  }
View Full Code Here

     
      // update the error modules
      for (int j = 0; j < numSteps &&
        (i + j < insts.numInstances()); j++) {
       
        Instance toPredict = insts.instance(i + j);
//        double[] forecastsForStepJ = new double[m_targetFields.size()];
        List<NumericPrediction> predsForTargets = forecastForSteps.get(j);
       
/*        for (int k = 0; k < m_targetFields.size(); k++) {
          forecastsForStepJ[k] = predsForTargets.get(k).predicted();
View Full Code Here

    // set all targets to missing
    List<String> fieldsToForecast = AbstractForecaster.stringToList(forecaster
        .getFieldsToForecast());
    for (int i = 0; i < overlay.numInstances(); i++) {
      Instance current = overlay.instance(i);
      for (String target : fieldsToForecast) {
        current.setValue(overlay.attribute(target), Utils.missingValue());
      }
    }

    return overlay;
  }
View Full Code Here

          ((TSLagUser) forecaster).getTSLagMaker().setArtificialTimeStartValue(
              m_primeWindowSize);
        }
      }
      for (int i = 0; i < m_trainingData.numInstances(); i++) {
        Instance current = m_trainingData.instance(i);

        if (i < m_primeWindowSize) {
          primeData.add(current);
        } else {
          if (i % 10 == 0) {
            for (PrintStream p : progress) {
              p.println("Evaluating on training set: processed " + i
                  + " instances...");
            }
          }

          forecaster.primeForecaster(primeData);
          /*
           * System.err.println(primeData); System.exit(1);
           */

          List<List<NumericPrediction>> forecast = null;
          if (forecaster instanceof OverlayForecaster
              && ((OverlayForecaster) forecaster).isUsingOverlayData()) {

            // can only generate forecasts for remaining training data that
            // we can use as overlay data
            if (current != null) {
              Instances overlay = createOverlayForecastData(forecaster,
                  m_trainingData, i, m_horizon);

              forecast = ((OverlayForecaster) forecaster).forecast(m_horizon,
                  overlay, progress);
            }
          } else {
            forecast = forecaster.forecast(m_horizon, progress);
          }

          updateEvalModules(m_predictionsForTrainingData,
              m_metricsForTrainingData, forecast, i, m_trainingData);

          // remove the oldest prime instance and add this one
          if (m_primeWindowSize > 0 && current != null) {
            primeData.remove(0);
            primeData.add(current);
            primeData.compactify();
          }
        }
      }
    }

    if (m_trainingData != null && m_forecastFuture
    /* && !m_evaluateTrainingData */) {

      // generate a forecast beyond the end of the training data
      for (PrintStream p : progress) {
        p.println("Generating future forecast for training data...");
      }
      Instances primeData = new Instances(m_trainingData,
          m_trainingData.numInstances() - m_primeWindowSize, m_primeWindowSize);

      forecaster.primeForecaster(primeData);

      // if overlay data is being used then the only way we can make a forecast
      // beyond the end of the training data is if we have a held-out test set
      // to
      // use for the "future" overlay fields values
      if (forecaster instanceof OverlayForecaster
          && ((OverlayForecaster) forecaster).isUsingOverlayData()) {
        if (m_testData != null) {
          Instances overlay = createOverlayForecastData(forecaster, m_testData,
              0, m_horizon);
          m_trainingFuture = ((OverlayForecaster) forecaster).forecast(
              m_horizon, overlay, progress);
        } else {
          // print an error message
          for (PrintStream p : progress) {
            p.println("WARNING: Unable to generate a future forecast beyond the end "
                + "of the training data because there is no future overlay "
                + "data available.");
          }
        }
      } else {
        m_trainingFuture = forecaster.forecast(m_horizon);
      }
    }

    if (m_evaluateTestData) {
      for (PrintStream p : progress) {
        p.println("Evaluating on test set...");
      }

      // set up training set prediction and eval modules
      m_predictionsForTestData = new ArrayList<ErrorModule>();
      m_metricsForTestData = new HashMap<String, List<TSEvalModule>>();
      setupEvalModules(m_predictionsForTestData, m_metricsForTestData,
          AbstractForecaster.stringToList(forecaster.getFieldsToForecast()));

      Instances primeData = null;
      Instances rebuildData = null;
      if (m_trainingData != null) {
        primeData = new Instances(m_trainingData, 0);
        if (forecaster instanceof TSLagUser) {
          // initialize the artificial time stamp value (if in use)
          if (((TSLagUser) forecaster).getTSLagMaker()
              .isUsingAnArtificialTimeIndex()) {
            ((TSLagUser) forecaster).getTSLagMaker()
                .setArtificialTimeStartValue(m_trainingData.numInstances());
          }
        }
        if (m_rebuildModelAfterEachTestForecastStep) {
          rebuildData = new Instances(m_trainingData);
        }
      } else {
        primeData = new Instances(m_testData, 0);
      }

      int predictionOffsetForTestData = 0;
      if (m_trainingData == null || m_primeForTestDataWithTestData) {
        if (m_primeWindowSize >= m_testData.numInstances()) {
          throw new Exception("The test data needs to have at least as many "
              + "instances as the the priming window size!");
        }
        predictionOffsetForTestData = m_primeWindowSize;
        if (predictionOffsetForTestData >= m_testData.numInstances()) {
          throw new Exception(
              "Priming using test data requires more instances "
                  + "than are available in the test data!");
        }
        primeData = new Instances(m_testData, 0, m_primeWindowSize);

        if (forecaster instanceof TSLagUser) {
          if (((TSLagUser) forecaster).getTSLagMaker()
              .isUsingAnArtificialTimeIndex()) {
            double artificialTimeStampStart = 0;
            if (m_primeForTestDataWithTestData) {
              if (m_trainingData == null) {
                artificialTimeStampStart = ((TSLagUser) forecaster)
                    .getTSLagMaker().getArtificialTimeStartValue();
                artificialTimeStampStart += m_primeWindowSize;
                ((TSLagUser) forecaster).getTSLagMaker()
                    .setArtificialTimeStartValue(artificialTimeStampStart);
              } else {
                ((TSLagUser) forecaster).getTSLagMaker()
                    .setArtificialTimeStartValue(
                        m_trainingData.numInstances() + m_primeWindowSize);
              }
            }
          }
        }
      } else {
        // use the last primeWindowSize instances from the training
        // data to prime with
        predictionOffsetForTestData = 0;
        primeData = new Instances(m_trainingData,
            (m_trainingData.numInstances() - m_primeWindowSize),
            m_primeWindowSize);

      }

      for (int i = predictionOffsetForTestData; i < m_testData.numInstances(); i++) {
        Instance current = m_testData.instance(i);
        if (m_primeWindowSize > 0) {
          forecaster.primeForecaster(primeData);
        }

        if (i % 10 == 0) {
View Full Code Here

          + ((insts.numInstances() + m_horizon) * timeDelta);
      maxTimeColWidth = maxWidth(maxTimeColWidth, maxTime);
    }

    for (int i = 0; i < insts.numInstances(); i++) {
      Instance current = insts.instance(i);
      for (String t : targets) {
        if (!current.isMissing(insts.attribute(t))) {
          maxColWidth = maxWidth(maxColWidth, current.value(insts.attribute(t)));
        }
      }
    }

    StringBuffer result = new StringBuffer();
    if (dateFormat != null) {
      result.append(pad("Time", " ", maxTimeColWidth, false)).append("  ");
    } else {
      result.append(pad("inst#", " ", maxTimeColWidth, false)).append("  ");
    }

    for (String t : targets) {
      result.append(pad(t, " ", maxColWidth, true)).append(" ");
    }
    result.append("\n");

    int instNum = (int) timeStart;
    long dateTime = (long) timeStart;
    for (int i = 0; i < insts.numInstances() + futureForecast.size(); i++) {
      String predMarker = (i < insts.numInstances()) ? "" : "*";
      if (timeIsInstanceNum) {
        result.append(
            pad("" + instNum + predMarker, " ", maxTimeColWidth, false))
            .append("  ");
        instNum += (int) timeDelta;
      } else if (dateFormat != null) {
        result.append(
            pad(format.format(new Date(dateTime)) + predMarker, " ",
                maxTimeColWidth, false)).append("  ");
        if (lagMaker != null) {
          dateTime = (long) lagMaker.advanceSuppliedTimeValue(dateTime);
        } else {
          dateTime += (long) timeDelta;
        }
      } else {
        result.append(
            pad(Utils.doubleToString(timeStart, 4) + predMarker, " ",
                maxTimeColWidth, false)).append("  ");
        timeStart += timeDelta;
      }

      if (i < insts.numInstances()) {
        Instance current = insts.instance(i);
        for (String t : targets) {
          if (!current.isMissing(insts.attribute(t))) {
            double value = current.value(insts.attribute(t));
            result.append(
                pad(Utils.doubleToString(value, 4), " ", maxColWidth, true))
                .append(" ");
          } else {
            result.append(pad("?", " ", maxColWidth, true)).append(" ");
View Full Code Here

        m_addDateMap = new Add();
        m_addDateMap.setAttributeName(m_timeStampName + "-remapped");
        m_addDateMap.setInputFormat(result);
        result = Filter.useFilter(result, m_addDateMap);

        Instance previous = result.instance(0);
        // now loop through and compute remapped date
        for (int i = 0; i < result.numInstances(); i++) {
          Instance current = result.instance(i);

          current = m_dateBasedPeriodicity.remapDateTimeStamp(current,
              previous, m_timeStampName);
          previous = current;
          /*
 
View Full Code Here

TOP

Related Classes of weka.core.Instance

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.