예제 #1
0
        /**
         * <summary> Converts discrete attributes of a single instance to binary discrete version using 1-of-L encoding. For example, if
         * an attribute has values red, green, blue; this attribute will be converted to 3 binary attributes where
         * red will have the value true false false, green will have the value false true false, and blue will have the value false false true.</summary>
         *
         * <param name="instance">The instance to be converted.</param>
         */
        protected override void ConvertInstance(Instance.Instance instance)
        {
            var size = instance.AttributeSize();

            for (var i = 0; i < size; i++)
            {
                if (attributeDistributions[i].Count > 0)
                {
                    var index = attributeDistributions[i].GetIndex(instance.GetAttribute(i).ToString());
                    for (var j = 0; j < attributeDistributions[i].Count; j++)
                    {
                        if (j != index)
                        {
                            instance.AddAttribute(new BinaryAttribute(false));
                        }
                        else
                        {
                            instance.AddAttribute(new BinaryAttribute(true));
                        }
                    }
                }
            }

            RemoveDiscreteAttributes(instance, size);
        }
예제 #2
0
        /**
         * <summary> The convertInstance method takes an {@link Instance} as an input and creates a {@link java.util.Vector} attributes from continuousAttributes.
         * After removing all attributes of given instance, it then adds new {@link ContinuousAttribute} by using the dot
         * product of attributes Vector and the eigenvectors.</summary>
         *
         * <param name="instance">Instance that will be converted to {@link ContinuousAttribute} by using eigenvectors.</param>
         */
        protected override void ConvertInstance(Instance.Instance instance)
        {
            var attributes = new Vector(instance.ContinuousAttributes());

            instance.RemoveAllAttributes();
            foreach (var eigenvector in _eigenvectors)
            {
                instance.AddAttribute(new ContinuousAttribute(attributes.DotProduct(eigenvector)));
            }
        }
예제 #3
0
        /**
         * <summary> Returns the standard deviation of attributes for instances.</summary>
         *
         * <returns>Standard deviation of attributes for instances.</returns>
         */
        public Instance.Instance StandardDeviation()
        {
            var result = new Instance.Instance(_list[0].GetClassLabel());

            for (var i = 0; i < _list[0].AttributeSize(); i++)
            {
                result.AddAttribute(AttributeStandardDeviation(i));
            }

            return(result);
        }
예제 #4
0
        /**
         * <summary> Constructor for an instance list with a given data definition, data file and a separator character. Each instance
         * must be stored in a separate line separated with the character separator. The last item must be the class label.
         * The function reads the file line by line and for each line; depending on the data definition, that is, type of
         * the attributes, adds discrete and continuous attributes to a new instance. For example, given the data set file
         * <p/>
         * red;1;0.4;true
         * green;-1;0.8;true
         * blue;3;1.3;false
         * <p/>
         * where the first attribute is a discrete attribute, second and third attributes are continuous attributes, the
         * fourth item is the class label.</summary>
         *
         * <param name="definition">Data definition of the data set.</param>
         * <param name="separator"> Separator character which separates the attribute values in the data file.</param>
         * <param name="fileName">  Name of the data set file.</param>
         */
        public InstanceList(DataDefinition definition, string separator, string fileName)
        {
            _list = new List <Instance.Instance>();
            var streamReader = new StreamReader(fileName);
            var line         = streamReader.ReadLine();

            while (line != null)
            {
                var attributeList = line.Split(separator);
                if (attributeList.Length == definition.AttributeCount() + 1)
                {
                    var current = new Instance.Instance(attributeList[attributeList.Length - 1]);
                    for (var i = 0; i < attributeList.Length - 1; i++)
                    {
                        switch (definition.GetAttributeType(i))
                        {
                        case AttributeType.DISCRETE:
                            current.AddAttribute(new DiscreteAttribute(attributeList[i]));
                            break;

                        case AttributeType.BINARY:
                            current.AddAttribute(new BinaryAttribute(bool.Parse(attributeList[i])));
                            break;

                        case AttributeType.CONTINUOUS:
                            current.AddAttribute(new ContinuousAttribute(double.Parse(attributeList[i])));
                            break;
                        }
                    }

                    _list.Add(current);
                }

                line = streamReader.ReadLine();
            }
        }
        /**
         * <summary> Converts discrete attributes of a single instance to indexed version.</summary>
         *
         * <param name="instance">The instance to be converted.</param>
         */
        protected override void ConvertInstance(Instance.Instance instance)
        {
            var size = instance.AttributeSize();

            for (var i = 0; i < size; i++)
            {
                if (attributeDistributions[i].Count > 0)
                {
                    var index = attributeDistributions[i].GetIndex(instance.GetAttribute(i).ToString());
                    instance.AddAttribute(new DiscreteIndexedAttribute(instance.GetAttribute(i).ToString(), index,
                                                                       attributeDistributions[i].Count));
                }
            }

            RemoveDiscreteAttributes(instance, size);
        }