Skip to content

Instantly share code, notes, and snippets.

@nkt1546789
nkt1546789 / collage_template_generation.py
Created June 25, 2017 02:07
Collage template generation
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
def generate_template(n, width, height, random_state=1, max_random_state=10000, offset=0):
L = [np.array([offset, offset, width-offset, height-offset])]
random_state_lists = stats.randint.rvs(0, max_random_state, size=(n-1, 4), random_state=random_state)
for random_state_list in random_state_lists:
n_areas = len(L)
@nkt1546789
nkt1546789 / kde_regression.py
Last active August 25, 2021 22:36
An example of regression using kernel density estimation (KDE)
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
from sklearn.model_selection import GridSearchCV
from sklearn.neighbors import KernelDensity
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
random_state = 1
n_samples = 200
@nkt1546789
nkt1546789 / burst_detection_on_artificial_data.py
Last active March 19, 2017 06:50
A demo of Burst detection on artificial data.
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
def generate_samples(n, n_states, p, lam):
A = generate_transition_matrix(n_states, p)
x = []
y = []
state = 0
@nkt1546789
nkt1546789 / viterbi.py
Last active March 18, 2017 10:56
Viterbi algorithm on Python.
def viterbi(P, A):
"""
P: log probability matrix (n_samples by n_states)
A: log transition probability matrix (n_states by n_states)
"""
n_samples = P.shape[0]
states = np.arange(P.shape[1])
V = np.zeros((n_samples, 2))
S = np.zeros((n_samples, 2), dtype=int)
V[0] = P[0]
@nkt1546789
nkt1546789 / gaussian_transformer.py
Created January 25, 2017 16:07
Nonlinear data transformer using Gaussian kernel.
import numpy as np
class GaussianTransformer(object):
def __init__(self, sigma=1.0, b=100, random_state=1):
self.sigma = sigma
self.b = 100
self.random_state = random_state
def fit(self, X):
b = np.min([X.shape[0], self.b])
@nkt1546789
nkt1546789 / simple_nn.py
Created January 23, 2017 18:01
Simple neural network with tensorflow.
import tensorflow as tf
import numpy as np
class SimpleClassifier(object):
def __init__(self, m=None, random_state=1, n_epochs=20):
self.m = m
self.random_state = random_state
self.n_epochs = n_epochs
def fit(self, X, y):
@nkt1546789
nkt1546789 / simple_classifier.py
Last active March 13, 2017 04:26
Probably this is the simplest classifier.
import numpy as np
from sklearn import preprocessing, base
class SimpleBinaryClassifier(base.BaseEstimator):
def fit(self, X, y):
"""
Requirement: y \in \{0, 1\}
"""
self.scaler = preprocessing.StandardScaler()
X = self.scaler.fit_transform(X)
@nkt1546789
nkt1546789 / itml.py
Last active August 29, 2016 14:29
(low-rank kernel) Information theoretic metric leanring
import numpy as np
import scipy.sparse.linalg as sla
def KITML_lr(K0, constraints, dm=None, dc=None, gamma=1., max_iter=1000, stop_threshold=1e-3, max_k=None):
# check if K0 is symmetric
if max_k is None:
max_k = K0.shape[0]-1
S, U = sla.eigsh(K0, k=max_k)
U = U[:,::-1]
S = S[::-1]
@nkt1546789
nkt1546789 / single_topic_unigram_generator.py
Last active August 29, 2016 14:45
Single topic unigram generator in Python.
import numpy as np
from scipy import sparse
class SingleTopicUnigramGenerator(object):
def __init__(self, n_topics=3, n_features=1000, alpha=1.0, beta=1.0):
self.n_topics = n_topics
self.n_features = n_features
self.alpha = alpha
self.beta = beta
@nkt1546789
nkt1546789 / puclassifier.py
Last active May 12, 2022 15:13
Learning Classifiers from positive and unlabeled data by sample weighting proposed by Elkan and Noto 2008.
import numpy as np
from sklearn.linear_model import SGDClassifier
from sklearn.cross_validation import StratifiedKFold
from sklearn.grid_search import GridSearchCV
class PUClassifier(object):
def __init__(self, trad_clf=None, n_folds=2):
self.trad_clf = trad_clf
self.n_folds = n_folds