Пример #1
0
        /// <summary>
        /// Create a dense matrix used in XGBoost.
        /// </summary>
        /// <param name="data">Matrix as a Float array</param>
        /// <param name="nrow">Number of rows</param>
        /// <param name="ncol">Number of columns</param>
        /// <param name="labels">Labels</param>
        /// <param name="missing">Missing value</param>
        /// <param name="weights">Vector of weights (can be null)</param>
        /// <param name="groups">Vector of groups (can be null)</param>
        /// <param name="featureNames">Set names for features.</param>
        /// <param name="featureTypes">Set types for features.</param>
        public DMatrix(Float[] data, uint nrow, uint ncol, Float[] labels = null, Float missing = Float.NaN,
                       Float[] weights = null, uint[] groups = null,
                       IEnumerable <string> featureNames = null, IEnumerable <string> featureTypes = null)
        {
#if (DEBUG)
            _gcKeep = new GcKeep()
            {
                data    = data,
                labels  = labels,
                weights = weights,
                groups  = groups
            };
#endif

            WrappedXGBoostInterface.Check(WrappedXGBoostInterface.XGDMatrixCreateFromMat(data, nrow, ncol, missing, ref _handle));

            if (labels != null)
            {
                SetLabel(labels, nrow);
            }
            if (weights != null)
            {
                SetWeight(weights, nrow);
            }
            if (groups != null)
            {
                SetGroups(groups, nrow);
            }

            _featureNames = featureNames == null ? null : featureNames.ToArray();
            _featureTypes = featureTypes == null ? null : featureTypes.ToArray();
        }
Пример #2
0
        /// <summary>
        /// Create a sparse matrix used in XGBoost.
        /// </summary>
        /// <param name="numColumn">number of features or columns</param>
        /// <param name="indptr">Pointer to row headers</param>
        /// <param name="indices">column indices</param>
        /// <param name="data">Matrix as a Float array</param>
        /// <param name="nrow">Rows in the matix</param>
        /// <param name="nelem">Number of nonzero elements in the matrix</param>
        /// <param name="labels">Labels</param>
        /// <param name="weights">Vector of weights (can be null)</param>
        /// <param name="groups">Vector of groups (can be null)</param>
        /// <param name="featureNames">Set names for features.</param>
        /// <param name="featureTypes">Set types for features.</param>
        public DMatrix(/*bst_ulong*/ uint numColumn, /*size_t*/ ulong[] indptr, uint[] indices, Float[] data,
                       uint nrow, uint nelem, Float[] labels = null,
                       Float[] weights = null, uint[] groups = null,
                       IEnumerable <string> featureNames = null, IEnumerable <string> featureTypes = null)
        {
            Contracts.Assert(nrow + 1 == indptr.Length);
#if (DEBUG)
            _gcKeep = new GcKeep()
            {
                indptr  = indptr,
                indices = indices,
                data    = data,
                labels  = labels,
                weights = weights,
                groups  = groups
            };
#endif

#if (XGB_EXTENDED)
            WrappedXGBoostInterface.Check(WrappedXGBoostInterface.XGDMatrixCreateFromCSREx(indptr,
                                                                                           indices, data, (ulong)indptr.Length, nelem, numColumn, ref _handle));
#else
            WrappedXGBoostInterface.Check(WrappedXGBoostInterface.XGDMatrixCreateFromCSR(indptr,
                                                                                         indices, data, (uint)indptr.Length, nelem, ref _handle));
#endif

            if (labels != null)
            {
                SetLabel(labels, nrow);
            }
            if (weights != null)
            {
                SetWeight(weights, nrow);
            }
            if (groups != null)
            {
                SetGroups(groups, nrow);
            }

            _featureNames = featureNames == null ? null : featureNames.ToArray();
            _featureTypes = featureTypes == null ? null : featureTypes.ToArray();

            Contracts.Assert(nrow == (int)GetNumRows());
            Contracts.Assert((int)GetNumCols() == numColumn);
        }