Exemplo n.º 1
0
        public double getInformationFor()
        {
            string             attributeName = specification.getTarget();
            IMap <string, int> counts        = CollectionFactory.CreateInsertionOrderedMap <string, int>();

            foreach (Example e in examples)
            {
                string val = e.getAttributeValueAsString(attributeName);
                if (counts.ContainsKey(val))
                {
                    counts.Put(val, counts.Get(val) + 1);
                }
                else
                {
                    counts.Put(val, 1);
                }
            }

            double[] data = new double[counts.GetKeys().Size()];
            int      i    = 0;

            foreach (int value in counts.GetValues())
            {
                data[i] = value;
                ++i;
            }
            data = Util.normalize(data);

            return(Util.information(data));
        }
Exemplo n.º 2
0
        public static Example exampleFromString(string data, DataSetSpecification dataSetSpec, string separator)
        {
            IRegularExpression        splitter        = TextFactory.CreateRegularExpression(separator);
            IMap <string, IAttribute> attributes      = CollectionFactory.CreateInsertionOrderedMap <string, IAttribute>();
            ICollection <string>      attributeValues = CollectionFactory.CreateQueue <string>(splitter.Split(data));

            if (dataSetSpec.isValid(attributeValues))
            {
                ICollection <string> names = dataSetSpec.getAttributeNames();
                int min = names.Size() > attributes.Size() ? names.Size() : attributes.Size();

                for (int i = 0; i < min; ++i)
                {
                    string name = names.Get(i);
                    IAttributeSpecification attributeSpec = dataSetSpec.getAttributeSpecFor(name);
                    IAttribute attribute = attributeSpec.CreateAttribute(attributeValues.Get(i));
                    attributes.Put(name, attribute);
                }
                string targetAttributeName = dataSetSpec.getTarget();
                return(new Example(attributes, attributes.Get(targetAttributeName)));
            }
            else
            {
                throw new RuntimeException("Unable to construct Example from " + data);
            }
        }