Skip to content

Instantly share code, notes, and snippets.

@grej
grej / numba_combinations.py
Created March 26, 2020 05:04
Ramanujan's approximation to approximate the number of ways to choose a things from n things
@numba.njit
def ln_ramanujan(n):
"""
Ramanujan's approximation to take the ln of the factorial of a number
"""
return n * np.log(n) - n + np.log(n * (1 + 4 * n * (1 + 2 * n)))/6. + np.log(pi)/2
@numba.njit
def n_choose_k(n, k):
"""
def replace_str_cols(df):
"""
replace all columns with string labels with numeric values
"""
new_df = df.copy()
dd = defaultdict(LabelEncoder)
# create the encoded dataframe
edf = new_df.apply(lambda x: dd[x.name].fit_transform(x))
for c in new_df.columns:
if new_df[c].dtype == np.object:
import string
def enumerate_letters(count):
"""
Generates a list of unique capital letter strings accordingly
eg. ['A', 'B', ... , 'AA', 'BB', 'CC', ...]
:param df: number of unique letter strings to return
:returns: list <str>: list of capital letter strings
"""
@grej
grej / mb_transforms.py
Created April 4, 2019 04:55
partial derivative and ratio for pandas dataframes
def _all_vars_are_numeric(df, *args):
"""
Check if all variables in a dataframe are numeric
:param df: pandas dataframe
:param *args: n dataframe columns names
:returns: bool_: True if all are numeric
"""
return np.issubdtype(df[list(args)].dtypes.unique()[0], np.number)
@grej
grej / rank_encode_dataframe.py
Created November 13, 2018 15:28
Rank encode a dataframe column
"""
USAGE:
>>> replace_with_rank_indexes(dataframe, column_name)
or
>>> df.pipe(replace_with_rank_indexes, column='column_name')
Returns a copy of the dataframe with the designated column replaced by
@grej
grej / AliasSampling.ipynb
Created March 6, 2018 20:49 — forked from jph00/AliasSampling.ipynb
Fast weighted sampling using the alias method in numba
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
(venv) ubuntu@ip-10-30-254-4:/ops/mbdevops$ python deploy_stack.py emea master master
Region: eu-central-1
Stack: emea
Portal branch: master
Solver branch: master
packer build -var-file=packer/aws/vars/eu-central-1/emea/worker.json -var 'region=eu-central-1' -var 'stack=emea' -var 'portal_branch=master'
-var 'solver_branch=master' -var 'mode=worker' packer/aws/mb-app.json
packer build -var-file=packer/aws/vars/eu-central-1/emea/portal.json -var 'region=eu-central-1' -var 'stack=emea' -var 'portal_branch=master'
-var 'solver_branch=master' -var 'mode=portal' packer/aws/mb-app.json
amazon-ebs output will be in this color.
from timeit import default_timer as time
import numpy as np
from numba import cuda
import os
os.environ['NUMBAPRO_LIBDEVICE']='/usr/lib/nvidia-cuda-toolkit/libdevice/'
os.environ['NUMBAPRO_NVVM']='/usr/lib/x86_64-linux-gnu/libnvvm.so.3.1.0'
import numpy
import torch
import ctypes
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">