1. Home
  2. /
  3. Lightgbm
  4. /
  5. Cross Validation Example

Cross Validation Example - Lightgbm Typing CST Test

Loading…

Cross Validation Example — Lightgbm Code

Performing k-fold cross-validation using LightGBM.

import lightgbm as lgb
from sklearn.datasets import load_iris
from sklearn.model_selection import KFold
import numpy as np

data = load_iris()
X = data.data
y = data.target
kf = KFold(n_splits=5)
params = {'objective':'multiclass','num_class':3,'metric':'multi_logloss'}
for train_index, test_index in kf.split(X):
	X_train, X_test = X[train_index], X[test_index]
	y_train, y_test = y[train_index], y[test_index]
	train_data = lgb.Dataset(X_train, label=y_train)
	model = lgb.train(params, train_data, num_boost_round=50)
	y_pred = np.argmax(model.predict(X_test), axis=1)
	print('Fold accuracy:', np.mean(y_pred==y_test))

Lightgbm Language Guide

LightGBM (Light Gradient Boosting Machine) is a fast, distributed, high-performance gradient boosting framework based on decision tree algorithms, used for ranking, classification, and many other machine learning tasks.

Primary Use Cases

  • ▸Binary and multiclass classification
  • ▸Regression problems
  • ▸Ranking tasks (learning-to-rank)
  • ▸Feature selection and importance analysis
  • ▸Integration in ML pipelines for large-scale structured data

Notable Features

  • ▸Faster training with histogram-based decision tree algorithm
  • ▸Low memory usage compared to XGBoost
  • ▸Supports parallel and GPU learning
  • ▸Handles categorical features directly
  • ▸Scales efficiently with large datasets

Origin & Creator

LightGBM was developed by Microsoft’s DMTK team and released in 2016 to provide a faster and more memory-efficient gradient boosting framework compared to existing solutions.

Industrial Note

LightGBM is widely used in Kaggle competitions, finance, advertising, recommendation systems, and any scenario requiring high-speed gradient boosting on large datasets.

More Lightgbm Typing Exercises

LightGBM Simple Classification ExampleLightGBM Binary Classification ExampleLightGBM Regression ExampleLightGBM with Categorical FeaturesLightGBM Early Stopping ExampleLightGBM Feature Importance ExampleLightGBM Regression with ValidationLightGBM Multi-class Classification Example

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher