Skip to content

Instantly share code, notes, and snippets.

View MBKraus's full-sized avatar

Mike Kraus MBKraus

  • Mollie
  • Amsterdam, the Netherlands
  • 03:39 (UTC +02:00)
View GitHub Profile
@MBKraus
MBKraus / dataset.py
Last active March 25, 2019 14:31
Generate dataset
from sklearn import datasets
from sklearn.model_selection import train_test_split
# Generate dataset with 1000 samples, 100 features and 2 classes
def gen_dataset(n_samples=1000, n_features=100, n_classes=2, random_state=123):
X, y = datasets.make_classification(
n_features=n_features,
n_samples=n_samples,
n_informative=int(0.6 * n_features), # the number of informative features
@MBKraus
MBKraus / Parameters_random_grid.py
Last active March 25, 2019 14:35
Pipeline + Parameter grid - GridSearch / Random Search
import lightgbm as lgb
import numpy as np
from sklearn import pipeline
from hyperopt import hp
pipe = pipeline.Pipeline([
('clf', lgb.LGBMClassifier())
])
param_gridsearch = {
@MBKraus
MBKraus / grid_random.py
Last active March 25, 2019 14:32
GridSearch and Random Search function
from sklearn.model_selection import cross_val_score, GridSearchCV, RandomizedSearchCV
import pandas as pd
import time
def search(pipeline, parameters, X_train, y_train, X_test, y_test, optimizer='grid_search', n_iter=None):
start = time.time()
if optimizer == 'grid_search':
grid_obj = GridSearchCV(estimator=pipeline,
@MBKraus
MBKraus / hyperopt.py
Last active March 25, 2019 14:33
Hyperopt
import lightgbm as lgb
from sklearn.model_selection import cross_val_score
from hyperopt import fmin, tpe, hp, STATUS_OK, Trials
from time import time
def hyperopt(param_space, X_train, y_train, X_test, y_test, num_eval):
start = time.time()
def objective_function(params):
@MBKraus
MBKraus / hyperopt_param.py
Last active October 3, 2020 08:30
Hyperopt Parameters
param_hyperopt= {
'learning_rate': hp.loguniform('learning_rate', np.log(0.01), np.log(1)),
'max_depth': scope.int(hp.quniform('max_depth', 5, 15, 1)),
'n_estimators': scope.int(hp.quniform('n_estimators', 5, 35, 1)),
'num_leaves': scope.int(hp.quniform('num_leaves', 5, 50, 1)),
'boosting_type': hp.choice('boosting_type', ['gbdt', 'dart']),
'colsample_bytree': hp.uniform('colsample_by_tree', 0.6, 1.0),
'reg_lambda': hp.uniform('reg_lambda', 0.0, 1.0),
}
@MBKraus
MBKraus / objective_function.py
Last active March 22, 2019 20:40
Objective function
from hyperopt import STATUS_OK
import lightgbm as lgb
def objective_function(params):
clf = lgb.LGBMClassifier(**params)
score = cross_val_score(clf, X_train, y_train, cv=5).mean()
return {'loss': -score, 'status': STATUS_OK}
@MBKraus
MBKraus / Optimiser.py
Last active March 25, 2019 14:36
Optimizer
from hyperopt import fmin, tpe, Trials
import numpy as np
trials = Trials()
best_param = fmin(objective_function, param_space, algo=tpe.suggest, max_evals=num_eval, trials=trials, rstate= np.random.RandomState(1))
@MBKraus
MBKraus / run.py
Last active March 24, 2019 16:25
Run functions
num_eval =75
results_grid, estimator_grid = search(pipe, param_gridsearch, X_train, y_train, X_test, y_test, 'grid_search')
results_random, estimator_random = search(pipe, param_random, X_train, y_train, X_test, y_test, 'random_search', num_eval)
results_hyperopt = hyperopt(param_hyperopt, X_train, y_train, X_test, y_test, num_eval)
@MBKraus
MBKraus / initial_model_DAG.py
Last active January 17, 2022 01:29
DAG for training the initial model
import airflow
from airflow.models import DAG
from airflow.operators.python_operator import PythonOperator
from src.models.initial_model_functions import load_preprocess, fit_model
PATH_STREAM_SAMPLE = "/data/stream_sample.p"
PATH_TEST_SET = "/data/test_set.p"
INITIAL_MODEL_PATH = "/models/current_model/initial_model.H5"
BATCH_SIZE = 128
@MBKraus
MBKraus / initial_model_functions.py
Last active November 5, 2019 15:50
Functions for training the initial model
import keras
from keras.datasets import mnist
from keras.models import Sequential, load_model
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
import pickle
import logging
import os