
- LightGBM - Home
- LightGBM - Overview
- LightGBM - Architecture
- LightGBM - Installation
- LightGBM - Core Parameters
- LightGBM - Boosting Algorithms
- LightGBM - Tree Growth Strategy
- LightGBM - Dataset Structure
- LightGBM - Binary Classification
- LightGBM - Regression
- LightGBM - Ranking
- LightGBM - Implementation in Python
- LightGBM - Parameter Tuning
- LightGBM - Plotting Functionality
- LightGBM - Early Stopping Training
- LightGBM - Feature Interaction Constraints
- LightGBM vs Other Boosting Algorithms
- LightGBM Useful Resources
- LightGBM - Quick Guide
- LightGBM - Useful Resources
- LightGBM - Discussion
LightGBM - Dataset Structure
LightGBM is well known for its large dataset handling abilities, efficient memory usage, and short training times. The main LightGBM data structure is called a dataset. This type of structure is created mainly to store data in a way that makes prediction and training go faster. Let us look at the use and importance of this dataset.
Dataset Structure
A dataset in LightGBM is an efficient format for data storage used in gradient-boosting model training. Generating a LightGBM Dataset from your input data like a NumPy array or a Pandas DataFrame-is the first step in applying LightGBM.
The structure of the dataset aids LightGBM −
Reduce the amount of memory used by storing data effectively.
Pre-compute some data, like the feature histogram, to speed up training.
Effectively handle sparse data, which is the data having a large number of missing or zero values.
Creating a Dataset
To create a LightGBM Dataset, you have to generally follow these steps −
Step 1: Load your data
Your data can be in various formats like −
Pandas DataFrame: data = pd.DataFrame(...)
NumPy array: data = np.array(...)
CSV file: data = pd.read_csv('data.csv')
Step 2: Convert your data to LightGBM Dataset format
Like below you can convert your data to LightGBM dataset format −
import lightgbm as lgb # Example with Pandas DataFrame lgb_data = lgb.Dataset(data, label=labels)
Here the data is your input dataset and labels are the target values that you want to predict.
Step 3: Save in Binary Format
You can store the Dataset using LightGBM's binary format, which works better with larger datasets −
lgb_data.save_binary('data.bin')
Loading and Using a Dataset
Once the dataset is created you can use it for training. Below is how to train a model with LightGBM:
params = { 'objective': 'binary', # Example for binary classification 'metric': 'binary_logloss' } # Train the model model = lgb.train(params, lgb_data, num_boost_round=100)
If you need to run several tests on the same piece of data, you can also reuse the dataset in multiple scripts or sessions.
Key Features of LightGBM Data Structure
Here are some key features of LightGBM Data Structure −
LightGBM maximizes data access and storage to make the most use of memory.
To manage datasets that are too large to fit in memory, it uses data in a compressed binary format.
Sparse data is common in real-world applications like natural language processing (NLP) and recommendation systems, where a large number of attributes contain zero or missing values.
LightGBM effectively deals with sparse data by saving only the non-zero values and reducing memory usage and speeds up computations.
LightGBM by default supports categorical characteristics. Unlike one-hot encoding, which can lead to hundreds of additional columns, LightGBM handles these differently.
LightGBM uses a histogram-based technique to divide the data into decision trees. Basically, it generates feature value histograms, which allows it to find the ideal split points far faster than it could with traditional methods.
How LightGBM Handles Different Data Types
LightGBM can handle different data types and they are listed below −
Numerical Features
LightGBM handles numerical features as continuous data. To determine the best way to separate them, it automatically divides them into histograms. LightGBM is capable of handling features without the requirement for scaling or normalization.
Categorical Features
As we have seen earlier, LightGBM supports category characteristics natively. When you mark some columns as categorical, it takes care of the sorting and organizing the categories automatically which makes better splitting possible.
Missing Values
Imputation, or filling in the blanks with the mean, median, etc., is not necessary when using LightGBM to handle missing data. It automatically determines how to handle missing data optimally throughout the training phase by recognizing them as independent values.