A timer for ML functions
"A timer for ML functions"
- toc:true- branch: master- badges: true- comments: true
- author: David Kearney
- categories: [timer, jupyter]
- description: A timer for ML functions
- title: A timer for ML functions
#collapse-hide
from functools import wraps
import time
def timer(func):
"""[This decorator is a timer for functions]
Args:
func ([function]): [This decorator takes a function as argument]
Returns:
[string]: [states the duration of time between the function begining and ending]
"""
@wraps(func)
def wrapper(*args, **kwargs):
print(f"{func.__name__!r} begins")
start_time = time.time()
result = func(*args, **kwargs)
print(f"{func.__name__!r} ends in {time.time()-start_time} secs")
return result
return wrapper
@timer
def model_metrics(*args, **kwargs):
"""[This is a function to print model metrics of interest]
"""
print("Model ID Number:", args)
print("Metric of Interest:", kwargs)
model_metrics(1, 2, 10, key="word", key2="word2", numtrees="200")
from collections import Counter
import math, random
#
# data splitting
#
def split_data(data, prob):
"""split data into fractions [prob, 1 - prob]"""
results = [], []
for row in data:
results[0 if random.random() < prob else 1].append(row)
return results
def train_test_split(x, y, test_pct):
data = list(zip(x, y)) # pair corresponding values
train, test = split_data(data, 1 - test_pct) # split the dataset of pairs
x_train, y_train = list(zip(*train)) # magical un-zip trick
x_test, y_test = list(zip(*test))
return x_train, x_test, y_train, y_test
#
# correctness
#
def accuracy(tp, fp, fn, tn):
correct = tp + tn
total = tp + fp + fn + tn
return correct / total
def precision(tp, fp, fn, tn):
return tp / (tp + fp)
def recall(tp, fp, fn, tn):
return tp / (tp + fn)
def f1_score(tp, fp, fn, tn):
p = precision(tp, fp, fn, tn)
r = recall(tp, fp, fn, tn)
return 2 * p * r / (p + r)
if __name__ == "__main__":
print("accuracy(70, 4930, 13930, 981070)", accuracy(70, 4930, 13930, 981070))
print("precision(70, 4930, 13930, 981070)", precision(70, 4930, 13930, 981070))
print("recall(70, 4930, 13930, 981070)", recall(70, 4930, 13930, 981070))
print("f1_score(70, 4930, 13930, 981070)", f1_score(70, 4930, 13930, 981070))
favorite_number = 7
def add(a, b):
return a + b
def sub(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
return a / b
def count_vowels(word):
count = 0
for letter in word.lower():
count += letter in 'aeiou'
return count
# import example_module as sm
# print(sm.favorite_number)
# # add two numbers together
# print(sm.add(3, 8))
# # count the number of vowels in a string
# print(sm.count_vowels('Testing'))
import pandas as pd
from alive_progress import alive_bar, showtime, show_bars, show_spinners, config_handler
config_handler.set_global(theme='ascii', spinner='notes', bar='solid')
with alive_bar(3) as bar:
df = pd.read_csv('https://gist.githubusercontent.com/davidrkearney/bb461ba351da484336a19bd00a2612e2/raw/18dd90b57fec46a247248d161ffd8085de2a00db/china_province_economicdata_1996_2007.csv')
bar('file read, printing file')
print(df.head)
bar('data printed ok, printing methods of data')
print(dir(df))
bar('process complete')
from functools import wraps
import time
def timer(func):
"""[This decorator is a timer for functions]
Args:
func ([function]): [This decorator takes a function as argument]
Returns:
[string]: [states the duration of time between the function begining and ending]
"""
@wraps(func)
def wrapper(*args, **kwargs):
print(f"{func.__name__!r} begins")
start_time = time.time()
result = func(*args, **kwargs)
print(f"{func.__name__!r} ends in {time.time()-start_time} secs")
return result
return wrapper
@timer
def model_metrics(*args, **kwargs):
"""[This is a function to print model metrics of interest]
"""
print("Model ID Number:", args)
print("Metric of Interest:", kwargs)
model_metrics(1, 2, 10, key="word", key2="word2", numtrees="200")
This post includes code adapted from Data Science from Scratch