Skip to content

Instantly share code, notes, and snippets.

View christinebuckler's full-sized avatar

Christine christinebuckler

View GitHub Profile
@christinebuckler
christinebuckler / wordle.py
Created August 10, 2018 03:39
create wordle image from csv text
##
# Usage:
# python wordle_csv_upload.py /tmp/file.csv
##
import webbrowser
import sys, os
filename = sys.argv[1]
csv_file = open(filename)
#!/usr/bin/python
"""
cPickle works well and it is very flexible, but if array only data are available
numpy's save features can provide better read speed
"""
import pickle,os
import numpy as np
#######################################
'''
To run test files, type the following into the terminal
py.test file.py –vv
'''
import unittest as unittest
from folder.file import MyClass
class TestMyClass(unittest.TestCase):
def flatten_json(y):
out = {}
def flatten(x, name=''):
if type(x) is dict:
for a in x:
flatten(x[a], name + a + '_')
elif type(x) is list:
i = 0
for a in x:
@christinebuckler
christinebuckler / np_2d_to_1d_array.py
Last active January 24, 2019 20:12
np 2d array (n,1) to 1d (n,) array
import numpy
array_2d = np.array([1, 2, 3]).reshape(-1,1)
array_2d.shape # (3,1)
array_1d = array_2d.flatten()
array_1d.shape # (3,)
# and from 1d back to 2d...
@christinebuckler
christinebuckler / multi-label_metric_functions.py
Created November 11, 2018 21:42
multi-label metric functions: precision, recall, f1 score
def precision(y_true, y_pred):
i = set(y_true).intersection(y_pred)
len1 = len(y_pred)
if len1 == 0:
return 0
else:
return len(i) / len1
def recall(y_true, y_pred):
i = set(y_true).intersection(y_pred)
@christinebuckler
christinebuckler / ndcg.py
Created November 11, 2018 21:46
NDCG metric function
# Normalize Discounted Communicative Gain
# NDCG evaluated within list impression for top n items (where NCG is actually ranking and IDCG is the ideal ranking).
# Relevance score is generally related to funnel metrics.
def dcg(data, n):
rel = data['relevance_score']
dcg = sum((rel/np.log2(i + 1)) for i in range(1, n+1)])
return dcg
def ndcg(dcg, idcg):
@christinebuckler
christinebuckler / function_docstring_template.py
Last active June 5, 2019 16:33
python function with docstring template
def func(arg1, arg2):
"""Summary line.
Extended description of function.
Parameters
----------
arg1 : int
Description of arg1
arg2 : str
@christinebuckler
christinebuckler / runipy_example.sh
Created November 11, 2018 21:52
running jupyter notebooks as script with runipy
# https://pypi.org/project/runipy
# To save the output of each cell back to the notebook file, run:
runipy -o MyNotebook.ipynb
# To save the notebook output as a new notebook, run:
runipy MyNotebook.ipynb OutputNotebook.ipynb
# To run a .ipynb file and generate an HTML report, run:
runipy MyNotebook.ipynb --html report.html
@christinebuckler
christinebuckler / excel_join_text_cells
Created November 11, 2018 21:53
join cells to list in Excel
Use example code to join all cell values in column A, separated by a comma.
=TEXTJOIN(",",TRUE,A:A)