/// <summary> Convert a single instance over. The converted instance is /// added to the end of the output queue. /// /// </summary> /// <param name="instance">the instance to convert /// </param> private void convertInstance(Instance instance) { Instance inst = null; if (instance is SparseInstance) { double[] vals = new double[instance.numValues()]; int[] indices = new int[instance.numValues()]; int num = 0; for (int j = 0; j < instance.numValues(); j++) { if (instance.isMissingSparse(j) && (getInputFormat().classIndex() != instance.index(j)) && (instance.attributeSparse(j).Nominal || instance.attributeSparse(j).Numeric)) { if (m_ModesAndMeans[instance.index(j)] != 0.0) { vals[num] = m_ModesAndMeans[instance.index(j)]; indices[num] = instance.index(j); num++; } } else { vals[num] = instance.valueSparse(j); indices[num] = instance.index(j); num++; } } if (num == instance.numValues()) { inst = new SparseInstance(instance.weight(), vals, indices, instance.numAttributes()); } else { double[] tempVals = new double[num]; int[] tempInd = new int[num]; Array.Copy(vals, 0, tempVals, 0, num); Array.Copy(indices, 0, tempInd, 0, num); inst = new SparseInstance(instance.weight(), tempVals, tempInd, instance.numAttributes()); } } else { double[] vals = new double[getInputFormat().numAttributes()]; for (int j = 0; j < instance.numAttributes(); j++) { if (instance.isMissing(j) && (getInputFormat().classIndex() != j) && (getInputFormat().attribute(j).Nominal || getInputFormat().attribute(j).Numeric)) { vals[j] = m_ModesAndMeans[j]; } else { vals[j] = instance.value_Renamed(j); } } inst = new Instance(instance.weight(), vals); } inst.Dataset = instance.dataset(); push(inst); }
/// <summary> Constructor that copies the info from the given instance. /// Reference to the dataset is set to null. /// (ie. the instance doesn't have access to information about the /// attribute types) /// /// </summary> /// <param name="instance">the instance from which the attribute /// info is to be copied /// </param> public SparseInstance(SparseInstance instance) { m_AttValues = instance.m_AttValues; m_Indices = instance.m_Indices; m_Weight = instance.m_Weight; m_NumAttributes = instance.m_NumAttributes; m_Dataset = null; }
/// <summary> Convert a single instance over if the class is nominal. The converted /// instance is added to the end of the output queue. /// /// </summary> /// <param name="instance">the instance to convert /// </param> private void convertInstance(Instance instance) { double[] vals = new double[outputFormatPeek().numAttributes()]; int attSoFar = 0; for (int j = 0; j < getInputFormat().numAttributes(); j++) { weka.core.Attribute att = getInputFormat().attribute(j); if (!att.Nominal || (j == getInputFormat().classIndex()) || !m_Columns.isInRange(j)) { vals[attSoFar] = instance.value_Renamed(j); attSoFar++; } else { if (att.numValues() <= 2) { vals[attSoFar] = instance.value_Renamed(j); attSoFar++; } else { if (instance.isMissing(j)) { for (int k = 0; k < att.numValues(); k++) { vals[attSoFar + k] = instance.value_Renamed(j); } } else { for (int k = 0; k < att.numValues(); k++) { //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" if (k == (int) instance.value_Renamed(j)) { vals[attSoFar + k] = 1; } else { vals[attSoFar + k] = 0; } } } attSoFar += att.numValues(); } } } Instance inst = null; if (instance is SparseInstance) { inst = new SparseInstance(instance.weight(), vals); } else { inst = new Instance(instance.weight(), vals); } copyStringValues(inst, false, instance.dataset(), InputStringIndex, getOutputFormat(), OutputStringIndex); inst.Dataset = getOutputFormat(); push(inst); }
/// <summary> Produces a shallow copy of this instance. The copy has /// access to the same dataset. (if you want to make a copy /// that doesn't have access to the dataset, use /// <code>new SparseInstance(instance)</code> /// /// </summary> /// <returns> the shallow copy /// </returns> public override System.Object copy() { Instance result = new SparseInstance(this); result.m_Dataset = m_Dataset; return result; }