Esempio n. 1
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();
            }
        }