Beispiel #1
0
 /**
  * <summary> Adds a new instance to the {@link InstanceList}.</summary>
  *
  * <param name="current">{@link Instance} to add.</param>
  */
 public void AddInstance(Instance.Instance current)
 {
     if (_definition == null)
     {
         SetDefinition(current);
         _instances.Add(current);
     }
     else
     {
         if (CheckDefinition(current))
         {
             _instances.Add(current);
         }
     }
 }
        /**
         * <summary> Training algorithm for K-Means classifier. K-Means finds the mean of each class for training.</summary>
         *
         * <param name="trainSet">  Training data given to the algorithm.</param>
         * <param name="parameters">distanceMetric: distance metric used to calculate the distance between two instances.</param>
         */
        public override void Train(InstanceList.InstanceList trainSet, Parameter.Parameter parameters)
        {
            var priorDistribution = trainSet.ClassDistribution();
            var classMeans        = new InstanceList.InstanceList();
            var classLists        = trainSet.DivideIntoClasses();

            for (var i = 0; i < classLists.Size(); i++)
            {
                classMeans.Add(classLists.Get(i).Average());
            }

            model = new KMeansModel(priorDistribution, classMeans, ((KMeansParameter)parameters).GetDistanceMetric());
        }
Beispiel #3
0
        /**
         * <summary> Constructor for generating a new {@link DataSet} from given {@link File}.</summary>
         *
         * <param name="file">{@link File} to generate {@link DataSet} from.</param>
         */
        public DataSet(string fileName)
        {
            var i = 0;

            _instances  = new InstanceList.InstanceList();
            _definition = new DataDefinition();
            var input = new StreamReader(fileName);

            while (!input.EndOfStream)
            {
                var instanceText = input.ReadLine();
                var attributes   = instanceText.Split(",");
                if (i == 0)
                {
                    for (var j = 0; j < attributes.Length - 1; j++)
                    {
                        try
                        {
                            double.Parse(attributes[j]);
                            _definition.AddAttribute(AttributeType.CONTINUOUS);
                        }
                        catch (Exception)
                        {
                            _definition.AddAttribute(AttributeType.DISCRETE);
                        }
                    }
                }
                else
                {
                    if (attributes.Length != _definition.AttributeCount() + 1)
                    {
                        continue;
                    }
                }

                Instance.Instance instance;
                if (!attributes[attributes.Length - 1].Contains(";"))
                {
                    instance = new Instance.Instance(attributes[attributes.Length - 1]);
                }
                else
                {
                    var labels = attributes[attributes.Length - 1].Split(";");
                    instance = new CompositeInstance(labels);
                }

                for (var j = 0; j < attributes.Length - 1; j++)
                {
                    switch (_definition.GetAttributeType(j))
                    {
                    case AttributeType.CONTINUOUS:
                        instance.AddAttribute(new ContinuousAttribute(double.Parse(attributes[j])));
                        break;

                    case AttributeType.DISCRETE:
                        instance.AddAttribute(new DiscreteAttribute(attributes[j]));
                        break;
                    }
                }

                if (instance.AttributeSize() == _definition.AttributeCount())
                {
                    _instances.Add(instance);
                }

                i++;
            }

            input.Close();
        }