LightGBM:ValueError: DataFrame.dtypes for data must be int, float or bool.

Published on:
Last updated:

This post is also available in: 日本語 (Japanese)

ValueError: DataFrame.dtypes for data must be int, float or bool. Did not expect the data types in fields ...

This error is about using LightGBM.
In short, LightGBM is not compatible with "Object" type with pandas DataFrame, so you need to encode to "int, float or bool" by using LabelEncoder(sklearn.preprocessing.LabelEncoder) etc...
Following is simple sample code.

fit() and transform() are the pandas DataFrame object by using LabelEncoder(sklearn.preprocessing.LabelEncoder) of sklearn.

# coding:utf-8
from sklearn import preprocessing
import numpy as np
import pandas as pd
# Sample data
data = ["ABC", "123", 1, 2, "def"]
# Create pandas DataFrame Object
df = pd.DataFrame(data)
# Create LabelEncoder Object and fit(),transform()
lbl = preprocessing.LabelEncoder()
sample = lbl.transform(df)
# Converted value by LabelEncoder
print sample # This Outputs [2 3 0 1 4]

Kuniyoshi Takemoto is the founder of LLC, and editor of this blog( more and follow me on LinkedIn.