Skip to content

Instantly share code, notes, and snippets.

View kastnerkyle's full-sized avatar

Kyle Kastner kastnerkyle

View GitHub Profile
@kastnerkyle
kastnerkyle / hadamard.py
Last active August 29, 2015 13:57
Hadmard matrix and basis plots
# (C) Kyle Kastner, June 2014
# License: BSD 3 clause
#This code is for fun only! Use scipy.linalg.hadamard
import numpy as np
import matplotlib.pyplot as plt
import functools
def memoize(obj):
@kastnerkyle
kastnerkyle / procrustes_rotation.py
Last active August 29, 2015 14:02
Procrustes rotation for minimizing RMSE between 2 differently scaled/rotated matrices
# (C) Kyle Kastner, June 2014
# License: BSD 3 clause
import numpy as np
from scipy import linalg
from sklearn.utils import array2d, as_float_array
from sklearn.utils.extmath import svd_flip
from sklearn.utils.testing import assert_array_almost_equal
@kastnerkyle
kastnerkyle / streaming_variance.py
Last active August 29, 2015 14:03
Naive examples of streaming variance calculations
# (C) Kyle Kastner, June 2014
# License: BSD 3 clause
import numpy as np
# Using data from http://www.mathsisfun.com/data/standard-deviation.html
X = np.array([600, 470, 170, 430, 300])
# Showing steps from basic to Welford's and batch
# See http://cpsc.yale.edu/sites/default/files/files/tr222.pdf
@kastnerkyle
kastnerkyle / single_file_cp.py
Created September 8, 2014 00:15
The CP algorithm from scikit-tensor in a single file. Should be identical (or damn close)!
# License: GPL
# Based on scikit-tensor by mnick
# https://github.com/mnick/scikit-tensor
"""Tensor factorization."""
import numpy as np
from scipy import linalg
def _matricize(X, axis):
@kastnerkyle
kastnerkyle / covariance_test.py
Last active August 29, 2015 14:10
Test code for combining covariances
import numpy as np
from numpy.testing import assert_almost_equal
A = np.arange(200).reshape(40, 5).astype('float32')
B = np.arange(100).reshape(20, 5) + 10.
C = np.vstack((A, B))
mA = np.mean(A, axis=0)
mB = np.mean(B, axis=0)
mC = np.mean(C, axis=0)
@kastnerkyle
kastnerkyle / simple_filtering.py
Last active August 29, 2015 14:18
Super crude filtering
# Author: Kyle Kastner
# License: BSD 3-clause
import numpy as np
from scipy.signal import lfilter
import matplotlib.pyplot as plt
sine = np.sin(np.linspace(-4 * np.pi, 4 * np.pi, 10000))
noise = 0.2 * np.random.randn(len(sine))
s = sine + noise
# Original code from tinrtgu on Kaggle under WTFPL license
# Relicensed to BSD 3-clause (it does say do what you want...)
# Authors: Kyle Kastner
# License: BSD 3-clause
# Reference links:
# Adaptive learning: http://static.googleusercontent.com/media/research.google.com/en//pubs/archive/41159.pdf
# Criteo scalable response prediction: http://people.csail.mit.edu/romer/papers/TISTRespPredAds.pdf
# Vowpal Wabbit (hashing trick): https://github.com/JohnLangford/vowpal_wabbit/
# Hashing Trick: http://arxiv.org/pdf/0902.2206.pdf
@kastnerkyle
kastnerkyle / example_hdf5.py
Created April 30, 2015 18:01
Example file for building hdf5 datasets
# -*- coding: utf 8 -*-
# Author: Kyle Kastner
# License: BSD 3-clause
from __future__ import division
import os
import numpy as np
import tables
import numbers
import fnmatch
@kastnerkyle
kastnerkyle / convert_mp3_to_wav.sh
Last active August 29, 2015 14:21
convert_mp3_to_wav.sh
ffmpeg -i 111.mp3 -acodec pcm_s16le -ac 1 -ar 16000 out.wav
find ../mp3_symlinks/ -name *.mp3 -exec sh -c 'echo $(basename {} .mp3)' \;
find ../mp3_symlinks/ -name *.mp3 -exec sh -c 'ffmpeg -i {} -acodec pcm_s16le -ac 1 -ar 16000 $(basename {} .mp3).wav' \;
@kastnerkyle
kastnerkyle / tricks.sh
Last active August 29, 2015 14:21
Useful Bash and other tricks
for f in *.ipynb ; do echo $f; aspell list < "$f" | sort | uniq -c ; done | less
2to3 -f imports folder/path | patch -p0