Skip to content

Instantly share code, notes, and snippets.

@righthandabacus
righthandabacus / seed_everything.py
Last active December 22, 2021 21:54
Random seeding
# Source: @kastnerkyle
import numpy as np
import torch
import random
import os
default_seed=4142
print("Setting all possible default seeds based on {}".format(default_seed))
# try to get deterministic runs
@righthandabacus
righthandabacus / autoclick.js
Created July 20, 2021 01:06
autocomplete NYC training
function make_clicks() {
var x = document.evaluate(
"//div[@role='button' and contains(@id,'SmartShape_') and @class='cp-frameset']",
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue;
if (x) {
x.click();
@righthandabacus
righthandabacus / least_square.py
Created April 10, 2021 17:56
Example of scipy.optimize on least square linear regression
import numpy as np
import scipy as sp
import scipy.optimize as opt
# Generate linear regression
a = 3
b = 2
n = 1000
np.random.seed(42)
X = np.random.normal(loc=0, scale=4, size=(n,1))
@righthandabacus
righthandabacus / slidingwindow.py
Created December 20, 2020 18:28
Sliding window on a 2D array using numpy
def sliding_window(a, win_size):
'''Slding window view of a 2D array a using numpy stride tricks.
For a given input array `a` and the output array `b`, we will have
`b[i] = a[i:i+w]`
Args:
a: numpy array of shape (N,M)
Returns:
numpy array of shape (K,w,M) where K=N-w+1
'''
@righthandabacus
righthandabacus / dictionaries.py
Created September 18, 2020 05:01
Raymond Gettinger, Modern Python Dictionaries: A confluence of a dozen great idea, PyCon 2017
# typed from video: youtu.be/npw4s1QTmPg
# Raymond Gettinger, Modern Python Dictionaries: A confluence of a dozen great idea, PyCon 2017
from __future__ import division, print_function
import array
import collections
import itertools
# Placeholder constants
FREE = -1
@righthandabacus
righthandabacus / wpct.py
Created January 13, 2020 03:49
weighted percentile function
def weighted_percentile(a, q, weights=None, interpolation='step'):
"""
Compute the qth percentile of the data a, optionally weight can be provided.
Returns the qth percentile(s) of the array elements.
Methodology
-----------
If weights are not provided, we set all `a` of equal weight of 1. Then we
normalize the weight by equal factor so that their sum is 1. Then, in sorted
ascending order of `a`, we plot the values as a curve from 0 to 1 and lookup
@righthandabacus
righthandabacus / netcat.py
Last active March 26, 2019 20:46
netcat in python
import socket
import socketserver
import threading
def sender(hostname, port, content):
"""Connect to a TCP port and send content, then wait for reply
"""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((hostname, port))
sock.sendall(content)
@righthandabacus
righthandabacus / pyann.py
Created March 21, 2019 23:26
Artificial neural network with only numpy
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Provide class of a generic ANN for classification (using binary cross entropy)
"""
import numpy as np
# define activation functions g(Z), and their first derivative, using numpy
# hold all activation function pairs in a global dict
@righthandabacus
righthandabacus / hypchoid.py
Created March 7, 2019 02:45
Hyperchoid and hypochoid animated GIF creator
import argparse
from math import sin, cos, radians, gcd
from typing import Tuple, List
import numpy as np
from PIL import Image, ImageDraw
RADIANS = np.pi / 180
DIMENSION = (500, 500) # coordinate origin at centre
@righthandabacus
righthandabacus / selrank.py
Created February 23, 2019 21:22
Selection rank algorithm demo
"""Selecton rank algorithm
"""
import random
def selrank(array, start, stop, k):
"""return the k-th largest element (0-th = max) in sliced list
`array[start:stop]` using the selection rank algorithm
This move the larger elements of array to lower indices such that at the end