This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from numpy.typing import NDArray | |
class LinearRegression(object): | |
"""Linear Regression model | |
TODO: | |
- docstring | |
- input validation | |
- data reformat/reshape |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Singleton { | |
private Singleton() {} | |
/** | |
* 辅助内部类 | |
* | |
* 由于类的加载是线程安全的,这里不需要显式的锁 | |
*/ | |
private static class SingletonHolder { | |
private static final Singleton INSTANCE = new Singleton(); |