Skip to content

Instantly share code, notes, and snippets.

@ababycat
ababycat / idCard.py
Last active May 16, 2020 03:02
Id card.
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):
@ababycat
ababycat / permutation_combination.py
Created July 16, 2019 02:26
Generate permutaion and combination
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):
@ababycat
ababycat / lawofcosine.py
Last active April 11, 2019 05:03
more stable implementation of the Law of Cosines.
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
@ababycat
ababycat / image_data_augmentation.py
Created April 6, 2019 02:46
simple image data augmentation using OpenCV. flip, rotation, resize, statuer.
import os
import shutil
from math import ceil, floor
import numpy as np
import cv2
"""
Data augmentation.
"""
@ababycat
ababycat / em.py
Created August 8, 2018 08:28
EM(Expectation-Maimization) algorithm for find multi Gaussian means.
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
@ababycat
ababycat / show_score.py
Last active March 17, 2018 13:26
show f1 score use matplotlib in python
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)