Search code examples
pythonlabelvalueerrorlightgbm

Proper label type assignment while working with LightGBM package in python


I am trying to work with LightGBM package in Python and encountered this error:

"TypeError: Wrong type(ndarray) for label, should be list or numpy array".

My target(label) is created as: y_train.values and is an array which has characteristics like:

Type: int64,
Size: (1000,1)
Value: array([[0],
              [0],
...)

When I traced back this error,I found this code @ Basic.py code of lightgbm package:

Function list_to_1d_numpy is throwing this error.

I couldn't find any reason though why this function should throw error. However it is calling one function is_numpy_1d_array which checks for condition

len(data.shape) == 1, however when i do len(y_train.shape) it says 2.

Any ideas how can I resolve it?


Solution

  • Ok, I was thinking in right direction. The label(y_train) needs to be a one dimensional array. I changed it to one dimensional by using:

    y=y_train.ravel()
    

    and it worked!

    However while creating the target itself, we could have had

    y_train=dataframe['target'].values
    I had it like: dataframe[['target']].values,
    

    which created 2 dimensional arrays