Package: nyaggle
nyaggleDocumentation|Slide (Japanese)nyaggleis an utility library for Kaggle and offline competitions.
It is particularly focused on experiment tracking, feature engineering, and validation.nyaggle.ensemble- Averaging & stackingnyaggle.experiment- Experiment trackingnyaggle.feature_store- Lightweight feature storage using feather-formatnyaggle.features- sklearn-compatible featuresnyaggle.hyper_parameters- Collection of GBDT hyper-parameters used in past Kaggle competitionsnyaggle.validation- Adversarial validation & sklearn-compatible CV splittersInstallationYou can install nyaggle via pip:pipinstallnyaggleExamplesExperiment Trackingrun_experiment()is a high-level API for experiments with cross validation.
It outputs parameters, metrics, out of fold predictions, test predictions,
feature importance, and submission.csv under the specified directory.To enable mlflow tracking, include the optionalwith_mlflow=Trueparameter.fromsklearn.model_selectionimporttrain_test_splitfromnyaggle.experimentimportrun_experimentfromnyaggle.testingimportmake_classification_dfX,y=make_classification_df()X_train,X_test,y_train,y_test=train_test_split(X,y)params={'n_estimators':1000,'max_depth':8}result=run_experiment(params,X_train,y_train,X_test)# You can get outputs that are needed in data science competitions with 1 APIprint(result.test_prediction)# Test prediction in numpy arrayprint(result.oof_prediction)# Out-of-fold prediction in numpy arrayprint(result.models)# Trained models for each foldprint(result.importance)# Feature importance for each foldprint(result.metrics)# Evalulation metrics for each foldprint(result.time)# Elapsed timeprint(result.submission_df)# The output dataframe saved as submission.csv# ...and all outputs have been saved under the logging directory (default: output/yyyymmdd_HHMMSS).# You can use it with mlflow and track your experiments through mlflow-uiresult=run_experiment(params,X_train,y_train,X_test,with_mlflow=True)nyaggle also has a low-level API which has similar interface tomlflow trackingandwandb.fromnyaggle.experimentimportExperimentwithExperiment(logging_directory='./output/')asexp:# log key-value pair as a parameterexp.log_param('lr',0.01)exp.log_param('optimizer','adam')# log textexp.log('blah blah blah')# log metricexp.log_metric('CV',0.85)# log numpy ndarray, pandas dafaframe and any artifactsexp.log_numpy('predicted',predicted)exp.log_dataframe('submission',sub,file_format='csv')exp.log_artifact('path-to-your-file')Feature EngineeringTarget Encoding with K-Foldimportpandasaspdimportnumpyasnpfromsklearn.model_selectionimportKFoldfromnyaggle.feature.category_encoderimportTargetEncodertrain=pd.read_csv('train.csv')test=pd.read_csv('test.csv')all=pd.concat([train,test]).copy()cat_cols=[cforcintrain.columnsiftrain[c].dtype==np.object]target_col='y'kf=KFold(5)# Target encoding with K-foldte=TargetEncoder(kf.split(train))# use fit/fit_transform to train data, then apply transform to test datatrain.loc[:,cat_cols]=te.fit_transform(train[cat_cols],train[target_col])test.loc[:,cat_cols]=te.transform(test[cat_cols])# ... or just call fit_transform to concatenated dataall.loc[:,cat_cols]=te.fit_transform(all[cat_cols],all[cat_cols])Text Vectorization using BERTYou need to install pytorch to your virtual environment to use BertSentenceVectorizer.
MaCab and mecab-python3 are also required if you use the Japanese BERT model.importpandasaspdfromnyaggle.feature.nlpimportBertSentenceVectorizertrain=pd.read_csv('train.csv')test=pd.read_csv('test.csv')all=pd.concat([train,test]).copy()text_cols=['body']target_col='y'group_col='user_id'# extract BERT-based sentence vectorbv=BertSentenceVectorizer(text_columns=text_cols)text_vector=bv.fit_transform(train)# BERT + SVD, with cudabv=BertSentenceVectorizer(text_columns=text_cols,use_cuda=True,n_components=40)text_vector_svd=bv.fit_transform(train)# Japanese BERTbv=BertSentenceVectorizer(text_columns=text_cols,lang='jp')japanese_text_vector=bv.fit_transform(train)Adversarial Validationimportpandasaspdfromnyaggle.validationimportadversarial_validatetrain=pd.read_csv('train.csv')test=pd.read_csv('test.csv')auc,importance=adversarial_validate(train,test,importance_type='gain')Validation Splittersnyaggle provides a set of validation splitters that are compatible with sklearn.importpandasaspdfromsklearn.model_selectionimportcross_validate,KFoldfromnyaggle.validationimportTimeSeriesSplit,Take,Skip,Nthtrain=pd.read_csv('train.csv',parse_dates='dt')# time-series splitts=TimeSeriesSplit(train['dt'])ts.add_fold(train_interval=('2019-01-01','2019-01-10'),test_interval=('2019-01-10','2019-01-20'))ts.add_fold(train_interval=('2019-01-06','2019-01-15'),test_interval=('2019-01-15','2019-01-25'))cross_validate(...,cv=ts)# take the first 3 folds out of 10cross_validate(...,cv=Take(3,KFold(10)))# skip the first 3 folds, and evaluate the remaining 7 foldscross_validate(...,cv=Skip(3,KFold(10)))# evaluate 1st foldcross_validate(...,cv=Nth(1,ts))Other Awesome RepositoriesHere is a list of awesome repositories that provide general utility functions for data science competitions.
Please let me know if you have another one :)jeongyoonlee/Kagglermxbi/mlcrateanalokmaus/kuma_utilsFar0n/kaggletilsMLWave/Kaggle-Ensemble-Guiderushter/heamy