Skip to content

Instantly share code, notes, and snippets.

View alejio's full-sized avatar

Alex Spanos alejio

  • Greater London
View GitHub Profile
@alejio
alejio / lift_report.py
Created July 28, 2019 22:04
Calculate lift
def lift_report(filename, septr='|', jitter_on=False):
## Import necessary packages
import sys
import pandas as pd
import numpy as np
import pandas.core.algorithms as algos
from pandas import Series
from sklearn.metrics import roc_auc_score
@alejio
alejio / split_train_test.py
Last active July 6, 2019 20:05
Simple train test splitting in pandas
import pandas as pd
import numpy as np
def split_train_test(df_in, test_ratio):
# Reserve an out-of-place sample for final evaluation
# Could have used train_test_split from sklearn
df = df_in.copy(deep=True)
np.random.seed(42)
shuffled_indices = np.random.permutation(len(df))
test_set_size = int(len(df) * test_ratio)
@alejio
alejio / plot_confusion_matrix.py
Created July 6, 2019 20:02
Handy confusion matrix plotting
from sklearn.metrics import confusion_matrix
from matplotlib import pyplot as plt
# Plot confusion matrix
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
@alejio
alejio / dsg_conda.yaml
Last active October 31, 2018 18:37
Generic data science conda environment
name: dsg
channels:
- conda-forge
- defaults
dependencies:
- bleach=3.0.2=py_0
- click=7.0=py_0
- entrypoints=0.2.3=py37_1002
- gmp=6.1.2=hfc679d8_0
- ipywidgets=7.4.2=py_0
@alejio
alejio / dump_keys.py
Created October 19, 2018 14:00
Pretty print long dict keys
def dump_keys(d, lvl=0):
for k, v in d.items():
print('%s%s' % (lvl * ' ', k))
if type(v) == dict:
dump_keys(v, lvl+1)
@alejio
alejio / keybase.md
Created October 12, 2018 09:42
keybase.io

Keybase proof

I hereby claim:

  • I am alejio on github.
  • I am alex_sp (https://keybase.io/alex_sp) on keybase.
  • I have a public key ASBvurBYR5vP4COZTzP5Ef6536AguENaIq-TwcHUBBS2Ugo

To claim this, I am signing this object:

@alejio
alejio / jupyter_commands
Last active October 26, 2018 14:33
jupyter commands to remember
git clone https://github.com/hakimel/reveal.js.git
jupyter-nbconvert --to slides Myslides.ipynb --reveal-prefix=reveal.js
Make sure widgets work
conda install -c conda-forge ipywidgets
jupyter nbextension install --py widgetsnbextension --sys-prefix
jupyter nbextension enable widgetsnbextension --py --sys-prefix
from ipywidgets import *
IntSlider()
https://github.com/jupyter-widgets/ipywidgets/issues/1411
# added by Anaconda3 4.4.0 installer
export PATH="/Users/alexi/anaconda/bin:$PATH"
# Make python run in anaconda
# alias anacondainit='export PATH="/Library/Python/anaconda/bin:$PATH"; export PATH="/Applications/anaconda/bin:$PATH"'
# MISC SHORTCUTS I LIKE
@alejio
alejio / .condarc
Created May 29, 2018 13:23
Data Science boilerplate for new conda environments
# Show channel URLs when displaying what is going to be downloaded
# and in 'conda list'. The default is False.
show_channel_urls: True
ssl_verify: true
create_default_packages:
- pip
- ipython
- scipy
@alejio
alejio / display_side_by_side
Created March 21, 2018 14:15
Display pandas dataframes side-by-side
from IPython.display import display_html
def display_side_by_side(*args):
html_str=''
for df in args:
html_str+=df.to_html()
display_html(html_str.replace('table','table style="display:inline"'),raw=True)
#https://stackoverflow.com/questions/38783027/jupyter-notebook-display-two-pandas-tables-side-by-side