Skip to content

Instantly share code, notes, and snippets.

@harshitsinghai77
Created August 8, 2021 09:18
Show Gist options
  • Save harshitsinghai77/8b2515f2d0417c9207a1610828dd24f7 to your computer and use it in GitHub Desktop.
Save harshitsinghai77/8b2515f2d0417c9207a1610828dd24f7 to your computer and use it in GitHub Desktop.
from functools import wraps
from time import time
import click
import numba
import pandas as pd
import numpy as np
@click.group()
def cli():
pass
def timing(f):
@wraps(f)
def wrap(*args, **kwargs):
ts = time()
result = f(*args, **kwargs)
te = time()
print(f"fun: {f.__name__}, args: [{args}, {kwargs}] took: {te-ts} sec")
return result
return wrap
@timing
def expmean(rea):
"""Regular Function"""
val = rea.mean() ** 2
return val
@timing
@numba.jit(nopython=True)
def expmean_jit(real):
"""Perform multiple mean calculations"""
val = real.mean() ** 2
return val
def real_estate_df():
"""30 Years of Housing Prices"""
df = pd.read_csv("https://raw.githubusercontent.com/noahgift/real_estate_ml/master/data/Zip_Zhvi_SingleFamilyResidence.csv")
df.rename(columns={"RegionName":"ZipCode"}, inplace=True)
df["ZipCode"]=df["ZipCode"].map(lambda x: "{:.0f}".format(x))
df["RegionID"]=df["RegionID"].map(lambda x: "{:.0f}".format(x))
return df
def numerical_real_estate_array(df):
"""Converts df to numpy numerical array"""
columns_to_drop = ['RegionID', 'ZipCode', 'City', 'State', 'Metro', 'CountyName']
df_numerical = df.dropna()
df_numerical = df_numerical.drop(columns_to_drop, axis=1)
return df_numerical.values
def real_estate_array():
"""Returns Real Estate Array"""
df = real_estate_df()
rea = numerical_real_estate_array(df)
return np.float32(rea)
@timing
@numba.jit(parallel=True)
def add_sum_threaded(rea):
"""Use all the cores"""
x, _ = rea.shape
total = 0
for _ in numba.prange(x):
total += rea.sum()
print(total)
@timing
def add_sum(rea):
"""traditional for loop"""
x,_ = rea.shape
total = 0
for _ in numba.prange(x):
total += rea.sum()
print(total)
@cli.command()
@click.option('--jit/--no-jit', default=False)
def jit_test(jit):
rea = real_estate_array()
if jit:
click.echo(click.style('Running with JIT', fg='green'))
expmean_jit(rea)
else:
click.echo(click.style('Running NO JIT', fg='red'))
expmean(rea)
@cli.command()
@click.option('--threads/--no-jit', default=False)
def thread_test(threads):
rea = real_estate_array()
if threads:
click.echo(click.style('Running with multicore threads', fg='green'))
add_sum_threaded(rea)
else:
click.echo(click.style('Running NO THREADS', fg='red'))
add_sum(rea)
if __name__ == "__main__":
cli()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment