Beispiel #1
0
        void ComputeNearestNeighbors()
        {
            lock (_lock)
            {
                if (_trees != null)
                {
                    return;
                }

                using (var ch = _host.Start("Build k-d tree"))
                {
                    ch.Info(MessageSensitivity.None, "ComputeNearestNeighbors: build a k-d tree.");
                    int featureIndex, labelIndex, idIndex, weightIndex;
                    featureIndex = GetColumnIndex(ch, _args.column);
                    if (featureIndex == -1)
                    {
                        throw ch.Except($"Unable to find column '{_args.column}' in {SchemaHelper.ToString(Schema)}.");
                    }
                    labelIndex  = GetColumnIndex(ch, _args.labelColumn);
                    weightIndex = GetColumnIndex(ch, _args.weightColumn);
                    idIndex     = GetColumnIndex(ch, _args.colId);

                    Dictionary <long, Tuple <long, float> > merged;
                    _trees = NearestNeighborsBuilder.NearestNeighborsBuild <long>(ch, _input, featureIndex, labelIndex,
                                                                                  idIndex, weightIndex, out merged, _args);
                    ch.Info(MessageSensitivity.UserData, "Done. Tree size: {0} points.", _trees.Count());
                }
            }
        }
        private INearestNeighborsPredictor TrainPredictorLabel <TLabel>(IChannel ch, RoleMappedData data)
            where TLabel : IComparable <TLabel>
        {
            int featureIndex = data.Schema.Feature.Index;
            int labelIndex   = data.Schema.Label.Index;
            int idIndex      = -1;
            int weightIndex  = data.Schema.Weight == null ? -1 : data.Schema.Weight.Index;
            var indexes      = new HashSet <int>()
            {
                featureIndex, labelIndex, weightIndex
            };

            if (!string.IsNullOrEmpty(_args.colId) && data.Schema.Schema.TryGetColumnIndex(_args.colId, out idIndex))
            {
                indexes.Add(idIndex);
            }
            if (idIndex != -1)
            {
                var colType = data.Schema.Schema.GetColumnType(idIndex);
                if (colType.IsVector() || colType.RawKind() != DataKind.I8)
                {
                    throw ch.Except("Column '{0}' must be of type '{1}' not '{2}'", _args.colId, DataKind.I8, colType);
                }
            }

            Dictionary <long, Tuple <TLabel, float> > merged;
            var kdtrees = NearestNeighborsBuilder.NearestNeighborsBuild <TLabel>(ch, data.Data, featureIndex, labelIndex,
                                                                                 idIndex, weightIndex, out merged, _args);

            // End.
            return(CreateTrainedPredictor(kdtrees.Trees, merged));
        }
Beispiel #3
0
        void ComputeNearestNeighbors()
        {
            lock (_lock)
            {
                if (_trees != null)
                {
                    return;
                }

                using (var ch = _host.Start("Build k-d tree"))
                {
                    ch.Info("ComputeNearestNeighbors: build a k-d tree.");
                    int featureIndex, labelIndex, idIndex, weightIndex;
                    if (!_input.Schema.TryGetColumnIndex(_args.column, out featureIndex))
                    {
                        throw ch.Except("Unable to find column '{0}'.", _args.column);
                    }
                    labelIndex  = GetColumnIndex(ch, _args.labelColumn);
                    weightIndex = GetColumnIndex(ch, _args.weightColumn);
                    idIndex     = GetColumnIndex(ch, _args.colId);

                    Dictionary <long, Tuple <long, float> > merged;
                    _trees = NearestNeighborsBuilder.NearestNeighborsBuild <long>(ch, _input, featureIndex, labelIndex,
                                                                                  idIndex, weightIndex, out merged, _args);
                    ch.Info("Done. Tree size: {0} points.", _trees.Count());
                }
            }
        }