Skip to content

Instantly share code, notes, and snippets.

@hellpanderrr
hellpanderrr / Python concave hull ( alpha shape ) .md
Last active April 27, 2022 22:51
Concave hull in python using scipy and networkx
from scipy.spatial import Delaunay, ConvexHull
import networkx as nx
 
points = [ [0,0],[0,50],[50,50],[50,0],[0,400],[0,450],[50,400],[50,450],[700,300],[700,350],[750,300],[750,350],
          [900,600],[950,650],[950,600],[900,650]
]
def concave(points,alpha_x=150,alpha_y=250):
    points = [(i[0],i[1]) if type(i) <> tuple else i for i in points]
    de = Delaunay(points)
@maheshakya
maheshakya / LSH_forest_hack.py
Last active November 26, 2015 10:52
This is a rough implementation of LSH forest using sorted arrays and binary search for queries. (Still incomplete)
import numpy as np
from sklearn.metrics import euclidean_distances
#Re-implementation of bisect functions of bisect module to suit the application
def bisect_left(a, x):
lo = 0
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if a[mid] < x:
@mblondel
mblondel / letor_metrics.py
Last active April 24, 2024 19:43
Learning to rank metrics.
# (C) Mathieu Blondel, November 2013
# License: BSD 3 clause
import numpy as np
def ranking_precision_score(y_true, y_score, k=10):
"""Precision at rank k
Parameters
@mblondel
mblondel / matrix_sketch.py
Last active February 13, 2019 09:26
Frequent directions algorithm for matrix sketching.
# (C) Mathieu Blondel, November 2013
# License: BSD 3 clause
import numpy as np
from scipy.linalg import svd
def frequent_directions(A, ell, verbose=False):
"""
Return the sketch of matrix A.
@stober
stober / gp.py
Created February 16, 2013 00:17
Gaussian Process in Python
#!/usr/bin/python
"""
Author: Jeremy M. Stober
Program: GP.PY
Date: Thursday, July 17 2008
Description: Example of Gaussian Process Regression.
"""
from numpy import *
import pylab
@umbrae
umbrae / genders.sql
Created January 17, 2012 19:00
Roughly detect gender by first name in SQL
-- This table is useful for rough statistics based on gender
-- Don't consider it scientific in any way.
--
-- Example use:
-- select COALESCE(
-- (SELECT gender
-- FROM gender_types
-- WHERE name=users.first_name
-- LIMIT 1),
-- 'U') gender_type, count(*)
@hayesdavis
hayesdavis / blizzard.rb
Created October 19, 2010 17:09
Fun with snowflake
#First tweet on 21 Mar 2006 at 20:50:14.000 GMT (in ms)
TWEPOCH = 1288834974657
#High 42 bytes are timestamp, low 22 are worker, datacenter and sequence bits
SHIFT = 22
# Give it a snowflake id, it tells you what time it was created
# Will fail for very high ids because Ruby Time can only represent up to
# Jan 18, 2038 at 19:14:07 UTC (max signed int in seconds since unix epoch)
def what_time?(id)
# Chan's Convex Hull O(n log h) - Tom Switzer <thomas.switzer@gmail.com>
TURN_LEFT, TURN_RIGHT, TURN_NONE = (1, -1, 0)
def turn(p, q, r):
"""Returns -1, 0, 1 if p,q,r forms a right, straight, or left turn."""
return cmp((q[0] - p[0])*(r[1] - p[1]) - (r[0] - p[0])*(q[1] - p[1]), 0)
def _keep_left(hull, r):
while len(hull) > 1 and turn(hull[-2], hull[-1], r) != TURN_LEFT: