Пример #1
0
        private float[] GetFloatInfo(string field)
        {
            ulong  lenULong;
            IntPtr result;
            int    output = XGBOOST_NATIVE_METHODS.XGDMatrixGetFloatInfo(_handle, field, out lenULong, out result);

            if (output == -1)
            {
                throw new DllFailException(XGBOOST_NATIVE_METHODS.XGBGetLastError());
            }

            int len = unchecked ((int)lenULong);

            float[] floatInfo = new float[len];
            for (int i = 0; i < len; i++)
            {
                byte[] floatBytes = new byte[4];
                floatBytes[0] = Marshal.ReadByte(result, 4 * i + 0);
                floatBytes[1] = Marshal.ReadByte(result, 4 * i + 1);
                floatBytes[2] = Marshal.ReadByte(result, 4 * i + 2);
                floatBytes[3] = Marshal.ReadByte(result, 4 * i + 3);
                float f = BitConverter.ToSingle(floatBytes, 0);
                floatInfo[i] = f;
            }
            return(floatInfo);
        }
Пример #2
0
        public string[] DumpModelEx(string fmap, int with_stats, string format)
        {
            int length;

            string[] dumpStr;
            XGBOOST_NATIVE_METHODS.XGBoosterDumpModel(handle, fmap, with_stats, out length, out dumpStr);
            return(dumpStr);
        }
Пример #3
0
        public void Update(DMatrix train, int iter)
        {
            var output = XGBOOST_NATIVE_METHODS.XGBoosterUpdateOneIter(Handle, iter, train.Handle);

            if (output == -1)
            {
                throw new DllFailException(XGBOOST_NATIVE_METHODS.XGBGetLastError());
            }
        }
Пример #4
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposed)
     {
         return;
     }
     XGBOOST_NATIVE_METHODS.XGDMatrixFree(handle);
     disposed = true;
 }
Пример #5
0
        public void SetParameter(string name, string val)
        {
            int output = XGBOOST_NATIVE_METHODS.XGBoosterSetParam(handle, name, val);

            if (output == -1)
            {
                throw new DllFailException(XGBOOST_NATIVE_METHODS.XGBGetLastError());
            }
        }
Пример #6
0
        private void SetFloatInfo(string field, float[] floatInfo)
        {
            ulong len    = (ulong)floatInfo.Length;
            int   output = XGBOOST_NATIVE_METHODS.XGDMatrixSetFloatInfo(_handle, field, floatInfo, len);

            if (output == -1)
            {
                throw new DllFailException(XGBOOST_NATIVE_METHODS.XGBGetLastError());
            }
        }
Пример #7
0
        public Booster(DMatrix train)
        {
            var dmats  = new[] { train.Handle };
            var len    = unchecked ((ulong)dmats.Length);
            var output = XGBOOST_NATIVE_METHODS.XGBoosterCreate(dmats, len, out handle);

            if (output == -1)
            {
                throw new DllFailException(XGBOOST_NATIVE_METHODS.XGBGetLastError());
            }
        }
Пример #8
0
        public Booster(string fileName, int silent = 1)
        {
            IntPtr tempPtr;
            var    newBooster = XGBOOST_NATIVE_METHODS.XGBoosterCreate(null, 0, out tempPtr);
            var    output     = XGBOOST_NATIVE_METHODS.XGBoosterLoadModel(tempPtr, fileName);

            if (output == -1)
            {
                throw new DllFailException(XGBOOST_NATIVE_METHODS.XGBGetLastError());
            }
            handle = tempPtr;
        }
Пример #9
0
        public float[] Predict(DMatrix test)
        {
            ulong  predsLen;
            IntPtr predsPtr;
            var    output = XGBOOST_NATIVE_METHODS.XGBoosterPredict(
                handle, test.Handle, normalPrediction, 0, out predsLen, out predsPtr);

            if (output == -1)
            {
                throw new DllFailException(XGBOOST_NATIVE_METHODS.XGBGetLastError());
            }
            return(GetPredictionsArray(predsPtr, predsLen));
        }
Пример #10
0
        public Booster(IDictionary <string, object> parameters, DMatrix train)
        {
            var dmats  = new[] { train.Handle };
            var len    = unchecked ((ulong)dmats.Length);
            var output = XGBOOST_NATIVE_METHODS.XGBoosterCreate(dmats, len, out handle);

            if (output == -1)
            {
                throw new DllFailException(XGBOOST_NATIVE_METHODS.XGBGetLastError());
            }

            SetParameters(parameters);
        }
Пример #11
0
        public DMatrix(float[] data1D, ulong nrows, ulong ncols, float[] labels = null)
        {
            int output = XGBOOST_NATIVE_METHODS.XGDMatrixCreateFromMat(data1D, nrows, ncols, Missing, out _handle);

            if (output == -1)
            {
                throw new DllFailException(XGBOOST_NATIVE_METHODS.XGBGetLastError());
            }

            if (labels != null)
            {
                Label = labels;
            }
        }
Пример #12
0
        // Dispose pattern from MSDN documentation
        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }

            int output = XGBOOST_NATIVE_METHODS.XGDMatrixFree(_handle);

            if (output == -1)
            {
                throw new DllFailException(XGBOOST_NATIVE_METHODS.XGBGetLastError());
            }

            disposed = true;
        }
Пример #13
0
        public DMatrix(float[][] data, float[] labels = null)
        {
            float[] data1D = Flatten2DArray(data);
            ulong   nrows  = unchecked ((ulong)data.Length);
            ulong   ncols  = unchecked ((ulong)data[0].Length);
            int     output = XGBOOST_NATIVE_METHODS.XGDMatrixCreateFromMat(data1D, nrows, ncols, Missing, out _handle);

            if (output == -1)
            {
                throw new DllFailException(XGBOOST_NATIVE_METHODS.XGBGetLastError());
            }

            if (labels != null)
            {
                Label = labels;
            }
        }
Пример #14
0
        public string[] DumpModelEx(string fmap, int with_stats, string format)
        {
            int    length;
            IntPtr treePtr;
            var    intptrSize = IntPtr.Size;

            XGBOOST_NATIVE_METHODS.XGBoosterDumpModel(handle, fmap, with_stats, out length, out treePtr);
            var trees    = new string[length];
            int readSize = 0;
            var handle2  = GCHandle.Alloc(treePtr, GCHandleType.Pinned);

            //iterate through the length of the tree ensemble and pull the strings out from the returned pointer's array of pointers. prepend python's api convention of adding booster[i] to the beginning of the tree
            for (var i = 0; i < length; i++)
            {
                var    ipt1 = Marshal.ReadIntPtr(Marshal.ReadIntPtr(handle2.AddrOfPinnedObject()), intptrSize * i);
                string s    = Marshal.PtrToStringAnsi(ipt1);
                trees[i] = string.Format("booster[{0}]\n{1}", i, s);
                var bytesToRead = (s.Length * 2) + IntPtr.Size;
                readSize += bytesToRead;
            }
            handle2.Free();
            return(trees);
        }
Пример #15
0
 public void Save(string fileName)
 {
     XGBOOST_NATIVE_METHODS.XGBoosterSaveModel(handle, fileName);
 }