xgboost sklearn in python

To use XGBoost with scikit-learn in Python, you need to follow these steps:

  1. Install the required dependencies:

    main.py
    pip install xgboost
    
    20 chars
    2 lines
  2. Import the necessary libraries:

    main.py
    import xgboost as xgb
    from sklearn.model_selection import train_test_split
    from sklearn.datasets import load_boston
    from sklearn.metrics import mean_squared_error
    
    163 chars
    5 lines
  3. Load your dataset. In this example, we'll use the Boston Housing dataset provided by scikit-learn.

    main.py
    boston = load_boston()
    X, y = boston.data, boston.target
    
    57 chars
    3 lines
  4. Split the dataset into training and testing sets:

    main.py
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    
    90 chars
    2 lines
  5. Prepare the data for XGBoost:

    main.py
    dtrain = xgb.DMatrix(X_train, label=y_train)
    dtest = xgb.DMatrix(X_test, label=y_test)
    
    87 chars
    3 lines
  6. Set the hyperparameters for the XGBoost model:

    main.py
    params = {
        'objective': 'reg:squarederror',
        'max_depth': 3,
        'learning_rate': 0.1,
        'subsample': 0.8,
        'colsample_bytree': 0.8,
        'seed': 42
    }
    
    162 chars
    9 lines
  7. Train the XGBoost model:

    main.py
    num_rounds = 100
    model = xgb.train(params, dtrain, num_rounds)
    
    63 chars
    3 lines
  8. Make predictions on the test set:

    main.py
    y_pred = model.predict(dtest)
    
    30 chars
    2 lines
  9. Evaluate the model's performance:

    main.py
    mse = mean_squared_error(y_test, y_pred)
    print(f"Mean Squared Error: {mse}")
    
    77 chars
    3 lines

This is a basic example of how to use XGBoost with scikit-learn in Python. You can adjust the hyperparameters and modify the code to fit your specific use case.

gistlibby LogSnag