Skip to content

Instantly share code, notes, and snippets.

View freiz's full-sized avatar
🥷

Baojun Su freiz

🥷
  • Uber
  • Seattle, WA
View GitHub Profile
@freiz
freiz / k_means.py
Last active October 12, 2025 02:08
ML Coding: K-Means Algorithm for Clustering
import numpy as np
class KMeans(object):
def __init__(self, X, k, max_iter=100):
assert X.shape[0] >= k, f"X must contain at least {k} samples"
self.X = X.astype(np.float64) # Convert X to float64
self.k = k
self.max_iter = max_iter
@freiz
freiz / linear_regression.py
Last active October 12, 2025 02:08
ML Coding: Linear Regression
import numpy as np
from numpy.typing import NDArray
class LinearRegression(object):
"""Linear Regression model
TODO:
- docstring
- input validation
- data reformat/reshape
# A recursive function is tail recursive when a recursive call is the last thing executed by the function.
# This is not a tail recursion version
def fact(n):
if n == 1:
return 1
return n * fact(n - 1)
# This is a tail recursion version
@freiz
freiz / download_file.py
Created September 9, 2016 07:58
This gist shows how to simply download file with python requests
# you need to first install requests, in command window, install vai pip 'pip install requests'
import requests
def DownloadFile(url, filename=None):
if filename is None:
local_filename = url.split('/')[-1]
else:
local_filename = filename
r = requests.get(url)
@freiz
freiz / Singleton.java
Created April 6, 2015 09:09
Singleton in Java
public class Singleton {
private Singleton() {}
/**
* 辅助内部类
*
* 由于类的加载是线程安全的,这里不需要显式的锁
*/
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();