LightGBM Regression for Boston Housing Prices Prediction

  • Share this:

Code introduction


This function uses the LightGBM library to perform regression prediction on Boston housing prices. It first creates a training dataset, defines model parameters, and then trains the model to make predictions on test data.


Technology Stack : The package and technology stack used in this code include LightGBM, scikit-learn, and NumPy.

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import lightgbm as lgb
from sklearn.datasets import load_boston
import numpy as np

def predict_boston_housing_prices(X_train, y_train, X_test):
    # Create a train data set
    train_data = lgb.Dataset(X_train, label=y_train)
    
    # Create a parameter dictionary
    params = {
        'objective': 'regression',
        'metric': 'rmse',
        'boosting_type': 'gbdt',
        'num_leaves': 31,
        'learning_rate': 0.05,
        'feature_fraction': 0.9,
        'bagging_fraction': 0.8,
        'bagging_freq': 5,
        'verbose': -1
    }
    
    # Train the model
    bst = lgb.train(params, train_data)
    
    # Predict on the test data
    y_pred = bst.predict(X_test)
    
    return y_pred

# Example usage:
# Assuming X_train, y_train, X_test are already defined and preprocessed

# X_train, y_train = load_boston(return_X_y=True)
# X_test = np.random.rand(10, 13)  # Example test data
# predictions = predict_boston_housing_prices(X_train, y_train, X_test)