Esempio n. 1
0
        static BoosterHandle Create(DatasetHandle dataset, string parameters)
        {
            BoosterHandle bsHandle;

            SafeCall(LGBM_BoosterCreate(dataset, parameters, out bsHandle));
            return(bsHandle);
        }
        static DatasetHandle CreateFromFile(string fileName, string parameters, DatasetHandle reference = null)
        {
            DatasetHandle dsHandle = null;

            reference = reference ?? DatasetHandle.Zero;
            SafeCall(LGBM_DatasetCreateFromFile(fileName, parameters, reference, out dsHandle));
            return(dsHandle);
        }
Esempio n. 3
0
        public Dataset(string fileName, IReadOnlyDictionary <string, object> parameters = null, Dataset reference = null)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException(nameof(fileName));
            }
            var refHandle = reference?._handle ?? DatasetHandle.Zero;

            _handle = CreateFromFile(fileName, parameters.ToParamsString(), refHandle);
        }
        static DatasetHandle CreateFromMat(Array data, LGBMDataType dataType, int isRowMajor, string parameters, DatasetHandle reference)
        {
            DatasetHandle dsHandle = null;
            var           numCols  = data.GetColsCount();
            var           numRows  = data.GetRowsCount();
            DatasetHandle ptr      = reference ?? DatasetHandle.Zero;
            IntPtr        dataPtr  = data.GetPointer();

            SafeCall(LGBM_DatasetCreateFromMat(dataPtr, (int)dataType, numRows, numCols, isRowMajor, parameters, ptr, out dsHandle));
            return(dsHandle);
        }
Esempio n. 5
0
 Dataset(double[,] data, float[] label = null, int maxBin = 255, Dataset reference = null, string paramString = "")
 {
     if (data == null)
     {
         throw new ArgumentNullException(nameof(data));
     }
     _maxBin = maxBin;
     _handle = CreateFromMat(data, 1, paramString, reference?._handle);
     if (label != null)
     {
         SetField <float>("label", label);
     }
 }
Esempio n. 6
0
 public static extern int LGBM_DatasetSetField(DatasetHandle handle, [In][MarshalAs(UnmanagedType.LPStr)] string field_name, IntPtr field_data, int num_element, LGBMDataType type);
Esempio n. 7
0
 public static extern int LGBM_DatasetCreateFromMat(IntPtr data, int data_type, int numRows, int numCols, int is_row_major, [In][MarshalAs(UnmanagedType.LPStr)] string parameters, DatasetHandle reference, out DatasetHandle @out);
        /// <summary>
        /// Create DatasetHandle from float matrix
        /// </summary>
        /// <param name="data">data matrix</param>
        /// <param name="isRowMajor"></param>
        /// <param name="parameters"></param>
        /// <param name="reference"></param>
        /// <returns>a new DatasetHandle</returns>
        static DatasetHandle CreateFromMat(float[,] data, int isRowMajor, string parameters, DatasetHandle reference = null)
        {
            var dataType = Constants.TypeLgbmTypeMap[typeof(float)];

            return(CreateFromMat(data, dataType, isRowMajor, parameters, reference));
        }
Esempio n. 9
0
        static void Main(string[] args)

        {
            string model_path   = "../../model/regression_model.txt";
            string data_path    = "../../../data/regression/regression.test";
            string out_filepath = "prediction_result_cs.csv";
            string parameters   = "";
            int    ret;
            var    sw = new System.Diagnostics.Stopwatch();

            sw.Start();
            ret = BoosterMethods.LGBM_BoosterCreateFromModelfile(model_path, out int num_models, out BoosterHandle booster);
            sw.Stop();
            if (ret != 0)
            {
                var err = Marshal.PtrToStringAnsi(NativeMethods.LGBM_GetLastError());
                throw new Exception(err);
            }
            else
            {
                Console.WriteLine($"Successfully load model from {model_path}");
                Console.WriteLine($"Elapsed {sw.ElapsedMilliseconds}ms");
            }
            DatasetHandle reference = null;

            reference = reference ?? DatasetHandle.Zero;
            ret       = DatasetMethods.LGBM_DatasetCreateFromFile(data_path, parameters, reference, out DatasetHandle x_test);
            if (ret != 0)
            {
                var err = Marshal.PtrToStringAnsi(NativeMethods.LGBM_GetLastError());
                throw new Exception(err);
            }
            else
            {
                Console.WriteLine($"Successfully load data from {data_path}");
                //Console.WriteLine($"Elapsed {sw.ElapsedMilliseconds}ms");
            }

            DatasetMethods.LGBM_DatasetGetNumData(x_test, out int num_rows);
            DatasetMethods.LGBM_DatasetGetNumFeature(x_test, out int num_cols);
            BoosterMethods.LGBM_BoosterCalcNumPredict(booster, num_rows, LGBMPredictType.PredictNormal, num_models, out int num_predicts);
            var result = new double[num_predicts];

            sw.Restart();
            ret = BoosterMethods.LGBM_BoosterPredictForFile(booster, data_path, false, LGBMPredictType.PredictNormal, num_models, parameters, out_filepath);
            sw.Stop();
            if (ret != 0)
            {
                var err = Marshal.PtrToStringAnsi(NativeMethods.LGBM_GetLastError());
                throw new Exception(err);
            }
            else
            {
                Console.WriteLine($"Successfully predict(from file) result is saved to {out_filepath}");
                Console.WriteLine($"Elapsed {sw.ElapsedMilliseconds}ms");
            }


            sw.Restart();
            ret = BoosterMethods.LGBM_BoosterPredictForMat(booster, x_test.DangerousGetHandle(), LGBMDataType.Float32, num_rows, num_cols,
                                                           0, LGBMPredictType.PredictNormal, num_models, parameters,
                                                           out int outputlen, result);
            sw.Stop();
            if (ret != 0)
            {
                var err = Marshal.PtrToStringAnsi(NativeMethods.LGBM_GetLastError());
                throw new Exception(err);
            }
            else
            {
                Console.WriteLine($"Successfully predict(from mat)");
                Console.WriteLine($"Elapsed {sw.ElapsedMilliseconds}ms");
            }


            Console.ReadLine();
            booster.Close();
            x_test.Close();
        }
Esempio n. 10
0
 public static extern int LGBM_DatasetGetNumFeature(DatasetHandle handle, out int @out);
Esempio n. 11
0
 public void Dispose()
 {
     _handle.Dispose();
     _handle = null;
 }
Esempio n. 12
0
 public static extern int LGBM_BoosterAddValidData(BoosterHandle handle, DatasetHandle valid_data);
Esempio n. 13
0
 public static extern int LGBM_DatasetSaveBinary(DatasetHandle handle, [In][MarshalAs(UnmanagedType.LPStr)] string filename);
Esempio n. 14
0
 public static extern int LGBM_DatasetGetFeatureNames(DatasetHandle handle, SafeCharPp feature_names, out int num_feature_names);
Esempio n. 15
0
 public static extern int LGBM_DatasetGetFeatureNames(DatasetHandle handle, IntPtr feature_names, out int num_feature_names);
Esempio n. 16
0
 public static extern int LGBM_DatasetCreateFromFile([In][MarshalAs(UnmanagedType.LPStr)] string filename, [In][MarshalAs(UnmanagedType.LPStr)] string parameters, DatasetHandle reference, out DatasetHandle @out);
Esempio n. 17
0
 public static extern int LGBM_DatasetGetSubset(DatasetHandle handle, ref int used_row_indices, int num_used_row_indices, [In][MarshalAs(UnmanagedType.LPStr)] string parameters, ref IntPtr @out);
Esempio n. 18
0
 public static extern int LGBM_BoosterCreate(DatasetHandle train_data, [In][MarshalAs(UnmanagedType.LPStr)] string parameters, out BoosterHandle @out);
Esempio n. 19
0
 public static extern int LGBM_DatasetGetField(DatasetHandle handle, [In][MarshalAs(UnmanagedType.LPStr)] string field_name, out int out_len, out IntPtr out_ptr, out LGBMDataType out_type);
Esempio n. 20
0
 public static extern int LGBM_BoosterResetTrainingData(BoosterHandle handle, DatasetHandle train_data);
Esempio n. 21
0
 public static extern int LGBM_DatasetGetField(DatasetHandle handle, [In][MarshalAs(UnmanagedType.LPStr)] string field_name, out int out_len, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] out long[] out_ptr, out int out_type);