Example usage for weka.core Instances numAttributes

List of usage examples for weka.core Instances numAttributes

Introduction

In this page you can find the example usage for weka.core Instances numAttributes.

Prototype


publicint numAttributes() 

Source Link

Document

Returns the number of attributes.

Usage

From source file:adams.data.instancesanalysis.PCA.java

License:Open Source License

/**
 * Performs the actual analysis./*from w  ww.  java2 s  . c o  m*/
 *
 * @param data   the data to analyze
 * @return      null if successful, otherwise error message
 * @throws Exception   if analysis fails
 */
@Override
protected String doAnalyze(Instances data) throws Exception {
    String result;
    Remove remove;
    PublicPrincipalComponents pca;
    int i;
    Capabilities caps;
    PartitionedMultiFilter2 part;
    Range rangeUnsupported;
    Range rangeSupported;
    TIntList listNominal;
    Range rangeNominal;
    ArrayList<ArrayList<Double>> coeff;
    Instances filtered;
    SpreadSheet transformed;
    WekaInstancesToSpreadSheet conv;
    String colName;

    result = null;
    m_Loadings = null;
    m_Scores = null;

    if (!m_AttributeRange.isAllRange()) {
        if (isLoggingEnabled())
            getLogger().info("Filtering attribute range: " + m_AttributeRange.getRange());
        remove = new Remove();
        remove.setAttributeIndicesArray(m_AttributeRange.getIntIndices());
        remove.setInvertSelection(true);
        remove.setInputFormat(data);
        data = Filter.useFilter(data, remove);
    }
    if (isLoggingEnabled())
        getLogger().info("Performing PCA...");

    listNominal = new TIntArrayList();
    if (m_SkipNominal) {
        for (i = 0; i < data.numAttributes(); i++) {
            if (i == data.classIndex())
                continue;
            if (data.attribute(i).isNominal())
                listNominal.add(i);
        }
    }

    // check for unsupported attributes
    caps = new PublicPrincipalComponents().getCapabilities();
    m_Supported = new TIntArrayList();
    m_Unsupported = new TIntArrayList();
    for (i = 0; i < data.numAttributes(); i++) {
        if (!caps.test(data.attribute(i)) || (i == data.classIndex()) || (listNominal.contains(i)))
            m_Unsupported.add(i);
        else
            m_Supported.add(i);
    }
    data.setClassIndex(-1);

    m_NumAttributes = m_Supported.size();

    // the principal components will delete the attributes without any distinct values.
    // this checks which instances will be kept.
    m_Kept = new ArrayList<>();
    for (i = 0; i < m_Supported.size(); i++) {
        if (data.numDistinctValues(m_Supported.get(i)) > 1)
            m_Kept.add(m_Supported.get(i));
    }

    // build a model using the PublicPrincipalComponents
    pca = new PublicPrincipalComponents();
    pca.setMaximumAttributes(m_MaxAttributes);
    pca.setVarianceCovered(m_Variance);
    pca.setMaximumAttributeNames(m_MaxAttributeNames);
    part = null;
    if (m_Unsupported.size() > 0) {
        rangeUnsupported = new Range();
        rangeUnsupported.setMax(data.numAttributes());
        rangeUnsupported.setIndices(m_Unsupported.toArray());
        rangeSupported = new Range();
        rangeSupported.setMax(data.numAttributes());
        rangeSupported.setIndices(m_Supported.toArray());
        part = new PartitionedMultiFilter2();
        part.setFilters(new Filter[] { pca, new AllFilter(), });
        part.setRanges(new weka.core.Range[] { new weka.core.Range(rangeSupported.getRange()),
                new weka.core.Range(rangeUnsupported.getRange()), });
    }
    try {
        if (part != null)
            part.setInputFormat(data);
        else
            pca.setInputFormat(data);
    } catch (Exception e) {
        result = Utils.handleException(this, "Failed to set data format", e);
    }

    transformed = null;
    if (result == null) {
        try {
            if (part != null)
                filtered = weka.filters.Filter.useFilter(data, part);
            else
                filtered = weka.filters.Filter.useFilter(data, pca);
        } catch (Exception e) {
            result = Utils.handleException(this, "Failed to apply filter", e);
            filtered = null;
        }
        if (filtered != null) {
            conv = new WekaInstancesToSpreadSheet();
            conv.setInput(filtered);
            result = conv.convert();
            if (result == null) {
                transformed = (SpreadSheet) conv.getOutput();
                // shorten column names again
                if (part != null) {
                    for (i = 0; i < transformed.getColumnCount(); i++) {
                        colName = transformed.getColumnName(i);
                        colName = colName.replaceFirst("filtered-[0-9]*-", "");
                        transformed.getHeaderRow().getCell(i).setContentAsString(colName);
                    }
                }
            }
        }
    }

    if (result == null) {
        // get the coefficients from the filter
        m_Scores = transformed;
        coeff = pca.getCoefficients();
        m_Loadings = extractLoadings(data, coeff);
        m_Loadings.setName("Loadings for " + data.relationName());
    }

    return result;
}

From source file:adams.data.instancesanalysis.pls.AbstractMultiClassPLS.java

License:Open Source License

/**
 * Determines the output format based on the input format and returns this.
 *
 * @param input    the input format to base the output format on
 * @return       the output format//ww w  .  ja v  a 2 s  .c o  m
 * @throws Exception    in case the determination goes wrong
 */
@Override
public Instances determineOutputFormat(Instances input) throws Exception {
    ArrayList<Attribute> atts;
    String prefix;
    int i;
    Instances result;
    List<String> classes;

    // collect classes
    m_ClassAttributeIndices = new TIntArrayList();
    classes = new ArrayList<>();
    for (i = 0; i < input.numAttributes(); i++) {
        if (m_ClassAttributes.isMatch(input.attribute(i).name())) {
            classes.add(input.attribute(i).name());
            m_ClassAttributeIndices.add(i);
        }
    }
    if (!classes.contains(input.classAttribute().name())) {
        classes.add(input.classAttribute().name());
        m_ClassAttributeIndices.add(input.classAttribute().index());
    }

    // generate header
    atts = new ArrayList<>();
    prefix = getClass().getSimpleName();
    for (i = 0; i < getNumComponents(); i++)
        atts.add(new Attribute(prefix + "_" + (i + 1)));
    for (String cls : classes)
        atts.add(new Attribute(cls));
    result = new Instances(prefix, atts, 0);
    result.setClassIndex(result.numAttributes() - 1);

    m_OutputFormat = result;

    return result;
}

From source file:adams.data.instancesanalysis.pls.AbstractSingleClassPLS.java

License:Open Source License

/**
 * Determines the output format based on the input format and returns this.
 *
 * @param input    the input format to base the output format on
 * @return       the output format//  www  .  j  a  v  a2s.c o m
 * @throws Exception    in case the determination goes wrong
 */
@Override
public Instances determineOutputFormat(Instances input) throws Exception {
    ArrayList<Attribute> atts;
    String prefix;
    int i;
    Instances result;

    // generate header
    atts = new ArrayList<>();
    prefix = getClass().getSimpleName();
    for (i = 0; i < getNumComponents(); i++)
        atts.add(new Attribute(prefix + "_" + (i + 1)));
    atts.add(new Attribute(input.classAttribute().name()));
    result = new Instances(prefix, atts, 0);
    result.setClassIndex(result.numAttributes() - 1);

    m_OutputFormat = result;

    return result;
}

From source file:adams.data.instancesanalysis.pls.PLS1.java

License:Open Source License

/**
 * Transforms the data, initializes if necessary.
 *
 * @param data   the data to use//from   ww  w  .j  av  a  2 s.  co  m
 */
protected Instances doTransform(Instances data, Map<String, Object> params) throws Exception {
    Matrix X, X_trans;
    Matrix y;
    Matrix W, w;
    Matrix T, t, t_trans;
    Matrix P, p, p_trans;
    double b;
    Matrix b_hat;
    int j;
    Matrix tmp;
    Instances result;

    // initialization
    if (!isInitialized()) {
        // split up data
        X = MatrixHelper.getX(data);
        y = MatrixHelper.getY(data);
        X_trans = X.transpose();

        // init
        W = new Matrix(data.numAttributes() - 1, getNumComponents());
        P = new Matrix(data.numAttributes() - 1, getNumComponents());
        T = new Matrix(data.numInstances(), getNumComponents());
        b_hat = new Matrix(getNumComponents(), 1);

        for (j = 0; j < getNumComponents(); j++) {
            // 1. step: wj
            w = X_trans.times(y);
            MatrixHelper.normalizeVector(w);
            MatrixHelper.setVector(w, W, j);

            // 2. step: tj
            t = X.times(w);
            t_trans = t.transpose();
            MatrixHelper.setVector(t, T, j);

            // 3. step: ^bj
            b = t_trans.times(y).get(0, 0) / t_trans.times(t).get(0, 0);
            b_hat.set(j, 0, b);

            // 4. step: pj
            p = X_trans.times(t).times(1 / t_trans.times(t).get(0, 0));
            p_trans = p.transpose();
            MatrixHelper.setVector(p, P, j);

            // 5. step: Xj+1
            X = X.minus(t.times(p_trans));
            y = y.minus(t.times(b));
        }

        // W*(P^T*W)^-1
        tmp = W.times(((P.transpose()).times(W)).inverse());

        // factor = W*(P^T*W)^-1 * b_hat
        m_r_hat = tmp.times(b_hat);

        // save matrices
        m_P = P;
        m_W = W;
        m_b_hat = b_hat;

        result = predict(data);
    }
    // prediction
    else {
        result = predict(data);
    }

    return result;
}

From source file:adams.data.instancesanalysis.pls.SIMPLS.java

License:Open Source License

/**
 * Transforms the data, initializes if necessary.
 *
 * @param data   the data to use/*w  ww .  j ava2s . co  m*/
 */
protected Instances doTransform(Instances data, Map<String, Object> params) throws Exception {
    Matrix A, A_trans;
    Matrix M;
    Matrix X, X_trans;
    Matrix X_new;
    Matrix Y, y;
    Matrix C, c;
    Matrix Q, q;
    Matrix W, w;
    Matrix P, p, p_trans;
    Matrix v, v_trans;
    Matrix T;
    Instances result;
    int h;

    if (!isInitialized()) {
        // init
        X = MatrixHelper.getX(data);
        X_trans = X.transpose();
        Y = MatrixHelper.getY(data);
        A = X_trans.times(Y);
        M = X_trans.times(X);
        C = Matrix.identity(data.numAttributes() - 1, data.numAttributes() - 1);
        W = new Matrix(data.numAttributes() - 1, getNumComponents());
        P = new Matrix(data.numAttributes() - 1, getNumComponents());
        Q = new Matrix(1, getNumComponents());

        for (h = 0; h < getNumComponents(); h++) {
            // 1. qh as dominant EigenVector of Ah'*Ah
            A_trans = A.transpose();
            q = MatrixHelper.getDominantEigenVector(A_trans.times(A));

            // 2. wh=Ah*qh, ch=wh'*Mh*wh, wh=wh/sqrt(ch), store wh in W as column
            w = A.times(q);
            c = w.transpose().times(M).times(w);
            w = w.times(1.0 / StrictMath.sqrt(c.get(0, 0)));
            MatrixHelper.setVector(w, W, h);

            // 3. ph=Mh*wh, store ph in P as column
            p = M.times(w);
            p_trans = p.transpose();
            MatrixHelper.setVector(p, P, h);

            // 4. qh=Ah'*wh, store qh in Q as column
            q = A_trans.times(w);
            MatrixHelper.setVector(q, Q, h);

            // 5. vh=Ch*ph, vh=vh/||vh||
            v = C.times(p);
            MatrixHelper.normalizeVector(v);
            v_trans = v.transpose();

            // 6. Ch+1=Ch-vh*vh', Mh+1=Mh-ph*ph'
            C = C.minus(v.times(v_trans));
            M = M.minus(p.times(p_trans));

            // 7. Ah+1=ChAh (actually Ch+1)
            A = C.times(A);
        }

        // finish
        if (getNumCoefficients() > 0)
            slim(W);
        m_W = W;
        T = X.times(m_W);
        X_new = T;
        m_B = W.times(Q.transpose());

        switch (m_PredictionType) {
        case ALL:
            y = T.times(P.transpose()).times(m_B);
            break;
        case NONE:
        case EXCEPT_CLASS:
            y = MatrixHelper.getY(data);
            break;
        default:
            throw new IllegalStateException("Unhandled prediction type: " + m_PredictionType);
        }

        result = MatrixHelper.toInstances(getOutputFormat(), X_new, y);
    } else {
        X = MatrixHelper.getX(data);
        X_new = X.times(m_W);

        switch (m_PredictionType) {
        case ALL:
            y = X.times(m_B);
            break;
        case NONE:
        case EXCEPT_CLASS:
            y = MatrixHelper.getY(data);
            break;
        default:
            throw new IllegalStateException("Unhandled prediction type: " + m_PredictionType);
        }

        result = MatrixHelper.toInstances(getOutputFormat(), X_new, y);
    }

    return result;
}

From source file:adams.data.outlier.AbstractInstanceOutlierDetectorTestCase.java

License:Open Source License

/**
 * Loads the data to process.//  w ww.  j  a va 2s  . co m
 *
 * @param filename   the filename to load (without path)
 * @return      the first instance in the dataset, or null if it failed
 *          to load; class attribute is always the last
 */
protected Instance load(String filename) {
    Instance result;
    Instances data;

    result = new Instance();
    try {
        m_TestHelper.copyResourceToTmp(filename);
        data = DataSource.read(new TmpFile(filename).getAbsolutePath());
        data.setClassIndex(data.numAttributes() - 1);
        result.set(data.instance(0));
    } catch (Exception e) {
        e.printStackTrace();
        result = null;
    } finally {
        m_TestHelper.deleteFileFromTmp(filename);
    }

    return result;
}

From source file:adams.flow.template.MyTransformer.java

License:Open Source License

/**
 * Counts the number of missing values in each row
 * /*w w w .j  ava 2 s .  com*/
 * @return array with the number of missing values in each row
 */
private int[] getMissingValuesByRow(Instances data) {
    int numRows = data.size();
    int numColumns = data.numAttributes();

    int[] missingValues = new int[numRows];
    for (int i = 0; i < numRows; i++)
        for (int j = 0; j < numColumns; j++)
            if (data.get(i).isMissing(j))
                missingValues[i]++;

    return missingValues;
}

From source file:adams.flow.template.MyTransformer.java

License:Open Source License

/**
 * Counts the number of missing values in each column
 * //from w  w w  .j a  v  a2 s.  c o  m
 * @return array with the number of missing values in each column
 */
private int[] getMissingValuesByColumn(Instances data) {
    int numRows = data.size();
    int numColumns = data.numAttributes();

    int[] missingValues = new int[numColumns];
    for (int i = 0; i < numRows; i++)
        for (int j = 0; j < numColumns; j++)
            if (data.get(i).isMissing(j))
                missingValues[j]++;

    return missingValues;
}

From source file:adams.flow.transformer.AbstractWekaPredictionsTransformer.java

License:Open Source License

/**
 * Filters the data accordingly to the selected attribute range.
 *
 * @param data   the data to filter//w  w w . ja  v  a  2s.  c  o  m
 * @return      the filtered data, null if filtering failed
 */
protected Instances filterTestData(Instances data) {
    int[] indices;
    Remove remove;

    try {
        m_TestAttributes.setMax(data.numAttributes());
        indices = m_TestAttributes.getIntIndices();
        remove = new Remove();
        remove.setAttributeIndicesArray(indices);
        remove.setInvertSelection(true);
        remove.setInputFormat(data);

        return Filter.useFilter(data, remove);
    } catch (Exception e) {
        getLogger().log(Level.SEVERE, "Failed to filter test data using range: " + m_TestAttributes, e);
        return null;
    }
}

From source file:adams.flow.transformer.WekaAttributeIterator.java

License:Open Source License

/**
 * Executes the flow item.//from   w w w .  j  av a2s  .c o  m
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instances data;
    int[] indices;
    String name;
    boolean useRegExp;

    result = null;

    if (m_InputToken.getPayload() instanceof Instance)
        data = ((Instance) m_InputToken.getPayload()).dataset();
    else
        data = ((Instances) m_InputToken.getPayload());

    m_Range.setMax(data.numAttributes());
    indices = m_Range.getIntIndices();
    useRegExp = !m_RegExp.isEmpty() && !m_RegExp.isMatchAll();
    m_Names.clear();
    for (int index : indices) {
        name = data.attribute(index).name();
        if (useRegExp)
            if (!m_RegExp.isMatch(name))
                continue;
        m_Names.add(name);
    }

    return result;
}