Skip to content

Instantly share code, notes, and snippets.

View joshlk's full-sized avatar

Josh Levy-Kramer joshlk

View GitHub Profile
@joshlk
joshlk / bash_cheatsheet.bash
Created September 23, 2018 12:21
Bash cheatsheet
# SSH port forwarding/tunnel
# If I want to be able to access port 6006 on a remote server locally
ssh -L 6006:localhost:6006 user@remote
@joshlk
joshlk / postgis_notes.sql
Last active September 18, 2018 16:23
PostGIS notes
-- Convert a lat,long pair to a projected (British grid system) coordiantes
-- SRID 4326: WGS84 GPS coordiantes
-- SRID 27700: British National Grid
SELECT
ST_X(ST_Transform(ST_SetSRID(ST_Point(long, lat),4326), 27700)) as x
,ST_Y(ST_Transform(ST_SetSRID(ST_Point(long, lat),4326), 27700)) as y
;
-- Number of points from another within X meters (for BSG)
SELECT
@joshlk
joshlk / dataiku_dss_public_api_examples.py
Last active September 13, 2018 11:33
Dataiku DDS Public API examples
import dataiku
import dataikuapi
from dataikuapi import SQLQueryRecipeCreator
### Creare SQL recipy
builder = SQLQueryRecipeCreator('compute_output', project)
builder.with_input('input')
builder.with_new_output('output', 'connection')
recipe = builder.build()
@joshlk
joshlk / varible_in_out_table.sql
Last active September 6, 2018 16:11
Using a variable for input and output table in Postgres SQL pl/pgsql
CREATE OR REPLACE FUNCTION query(
in_tb character varying,
out_tb character varying
)
RETURNS integer
LANGUAGE plpgsql
AS $function$
BEGIN
EXECUTE
'CREATE TABLE '
@joshlk
joshlk / keras_one_hot_encode.py
Last active August 7, 2018 10:28
Keras one-hot-encode layer
class OneHotEncode(Embedding):
"""
One-hot-encode layer eg. [[1, 3]] -> [[[0,1,0,0],[0,0,0,1]]]
input_dim: Number of categories to one-hot encode
input_length: length of X vecotrs (optional)
mask_zero: treat 0 as masking
"""
def __init__(self, input_dim, input_length=None, mask_zero=False):
@joshlk
joshlk / postcode.md
Last active February 3, 2023 14:02
Parsing and splitting UK postcodes

Parsing and splitting UK postcodes

You can use this regex to split the postcodes into its constituent parts:area (capture groups a1, a2 ... a5), district (d1...), sector (s1...) and unit (u1...). Adapted from wikipedia:

^(?:(?P<a1>[Gg][Ii][Rr])(?P<d1>) (?P<s1>0)(?P<u1>[Aa]{2}))|(?:(?:(?:(?P<a2>[A-Za-z])(?P<d2>[0-9]{1,2}))|(?:(?:(?P<a3>[A-Za-z][A-Ha-hJ-Yj-y])(?P<d3>[0-9]{1,2}))|(?:(?:(?P<a4>[A-Za-z])(?P<d4>[0-9][A-Za-z]))|(?:(?P<a5>[A-Za-z][A-Ha-hJ-Yj-y])(?P<d5>[0-9]?[A-Za-z]))))) (?P<s2>[0-9])(?P<u2>[A-Za-z]{2}))$

Demonstrated in this Regex101.

Postgres and SQL

@joshlk
joshlk / cross_validate_keras.py
Last active July 18, 2018 08:57
Scikit-learn cross-validation that returns Keras results
#!/usr/bin/env python
import warnings
import numbers
import time
import numpy as np
from sklearn.base import is_classifier, clone
from sklearn.utils import indexable
@joshlk
joshlk / first_not_null.sql
Created July 5, 2018 08:44
User-defined aggregate/window function that selects the first non-null element (Postgres SQL)
-- https://wiki.postgresql.org/wiki/First/last_(aggregate)
-- Create a function that always returns the first non-NULL item
CREATE OR REPLACE FUNCTION public.first_agg ( anyelement, anyelement )
RETURNS anyelement LANGUAGE SQL IMMUTABLE STRICT AS $$
SELECT $1;
$$;
-- And then wrap an aggregate around it
CREATE AGGREGATE public.FIRST (
sfunc = public.first_agg,
@joshlk
joshlk / custome_aggregate_functions.sql
Created July 4, 2018 14:24
Postgres custom aggregate functions
-- See: https://stackoverflow.com/questions/48770517/take-first-second-third-last-value-and-selecting-rows-window-function-wit
----- N = 1
-- State transition function
-- agg_state: the current state, el: new element
create or replace function lag_agg_sfunc_1(agg_state point, el float)
returns point
immutable
language plpgsql
as $$
declare
@joshlk
joshlk / or_tools_SimpleMinCostFlow_cython_benchmark.ipynb
Created April 27, 2018 16:05
Benchmark of Google's or-tools and a prototype cython interface
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.