Skip to content

Instantly share code, notes, and snippets.

@ytsaig
ytsaig / custom_subsampling.py
Created February 21, 2017 12:54
Reproducible example for custom subsampling with LightGBM
import lightgbm as lgb
import numpy as np
import pandas as pd
from sklearn.metrics import mean_squared_error
def custom_subsample(n, frac):
"""Subsample frac*n indices."""
return np.random.choice(n, int(n*frac), replace=False)
# load data
@ytsaig
ytsaig / custom_softmax.py
Created June 6, 2017 07:29
Multiclass classification (softmax regression) via xgboost custom objective
import numpy as np
from sklearn import datasets
from sklearn.metrics import confusion_matrix
from sklearn.preprocessing import OneHotEncoder
import xgboost as xgb
def softmax(z):
z -= np.max(z)
sm = (np.exp(z).T / np.sum(np.exp(z), axis=1)).T