Skip to content

Instantly share code, notes, and snippets.

View idCard.py
def calcId(digit):
if not isinstance(digit, int) or len(str(digit)) != 17:
return False
table = '10X123456789'
alpha = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
return table[sum([int(x)*a for x, a in zip(str(digit), alpha)]) % 11]
def isGoodId(instr):
@heylfda
heylfda / permutation_combination.py
Created Jul 16, 2019
Generate permutaion and combination
View permutation_combination.py
def permutation(array):
if len(array) == 1:
return [array]
output = []
for i in range(len(array)):
if i == len(array) - 1:
remain = array[0:i]
else:
remain = array[0:i] + array[i+1:]
for back in permutation(remain):
@heylfda
heylfda / lawofcosine.py
Last active Apr 11, 2019
more stable implementation of the Law of Cosines.
View lawofcosine.py
import numpy as np
method_list = [1, 2, 3]
for method in method_list:
for i in range(2000):
length = 10000
a = np.random.randn(length).astype(np.float32)
b = np.random.randn(length).astype(np.float32)
# non-stable when theta is small
@heylfda
heylfda / image_data_augmentation.py
Created Apr 6, 2019
simple image data augmentation using OpenCV. flip, rotation, resize, statuer.
View image_data_augmentation.py
import os
import shutil
from math import ceil, floor
import numpy as np
import cv2
"""
Data augmentation.
"""
@heylfda
heylfda / em.py
Created Aug 8, 2018
EM(Expectation-Maimization) algorithm for find multi Gaussian means.
View em.py
import numpy as np
import matplotlib.pyplot as plt
SIGMA = 1
def gauss(x, mu, sigma, eps=1e-8):
return 1./np.sqrt(np.pi*2*sigma**2+eps) * np.exp(-(x-mu)**2/(2*sigma**2+eps))
def gauss_vec(x, mu, sigma, eps=1e-8):
""" x: Nx1
@heylfda
heylfda / show_score.py
Last active Mar 17, 2018
show f1 score use matplotlib in python
View show_score.py
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def f1_score(precision, recall):
return precision*recall/(precision+recall)
x = np.arange(1e-3, 1, 1e-3)
y = np.arange(1e-3, 1, 1e-3)
X, Y = np.meshgrid(x, y)