PicNet / XGBoost.Net

.Net wrappers for the awesome XGBoost library
51 stars 18 forks source link

Failed to train if decimal separator in regional settings is ',' (comma) #7

Closed slonoten closed 7 years ago

slonoten commented 7 years ago

XGBGetLastError returns:

Invalid Parameter format for base_score expect float but value='0,5'

Possible solution is to use System.Globalization.NumberFormatInfo to specify dot as decimal separator:

    public void SetParameters(IDictionary<string, Object> parameters)
    {
      var nfi = new NumberFormatInfo { NumberDecimalSeparator = "." };

      SetParameter("max_depth", ((int)parameters["max_depth"]).ToString());
      SetParameter("learning_rate", ((float)parameters["learning_rate"]).ToString(nfi));
        ....
    }
cycloidistic commented 7 years ago

Thanks for the possible solution.

When did you get this error? Was it after doing new XGBRegressor()/new XGBClassifier(), after doing Fit(), or after doing Predict()?

Also, would you be able to share what code you used to create this error?

slonoten commented 7 years ago

I got this error after doing Fit().

Simplest way to reproduce this issue is temporary change decimal separator character in control panel regional settings from '.' (dot) to ',' (comma). Another way is add

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("ru-RU");

to code of unit test 'Predict' before call

XGBRegressor xgbr = new XGBRegressor();

If you add this code before

float[][] dataTrain = GetDataTrain();

you'll get similar error in float.Parse(string) call, which can be cured if specify instance of NumberFormatInfo as second parameter.

cycloidistic commented 7 years ago

Ok, I've replicated the problem and I've pushed the changes.

If you want, I can update the NuGet package tomorrow with the change.