Skip to content

Instantly share code, notes, and snippets.

View bistaumanga's full-sized avatar

bistaumanga bistaumanga

View GitHub Profile
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bistaumanga
bistaumanga / lof.py
Last active December 25, 2015 01:48
local outler factor
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 25 22:19:09 2014
@author: bistaumanga
Local Outlier factor implementation in python
Implementations are in two version for calculating knn:
1. Naive method
@bistaumanga
bistaumanga / PR.py
Last active December 29, 2015 10:29
Sainte Laguie Method (modified) to calculate Proportionate Representation seats in Nepal (CA election 2070). Just copy The data from http://election.gov.np/CA2070/CAResults/reportBody.php?selectedMenu1=2&rand=1385472280 to Votes.csv (table only) and run the script.....
# -*- coding: utf-8 -*-
#############################################################################
####### usage : > python PR.py -t 0.01 ###################################
####### OR, > python PR.py --thresh=0.01 ####################################
import numpy as np
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-t", "--thresh", dest="threshold", help="Threshold for PR")
options, args = parser.parse_args()
@bistaumanga
bistaumanga / hist.py
Created August 22, 2013 16:32
Histogram Equalization in python
import numpy as np
def imhist(im):
# calculates normalized histogram of an image
m, n = im.shape
h = [0.0] * 256
for i in range(m):
for j in range(n):
h[im[i, j]]+=1
return np.array(h)/(m*n)
@bistaumanga
bistaumanga / pca.py
Last active May 14, 2021 08:32
Principal component analysis in python.
import numpy as np
import pylab as plt
'''
Performs the Principal Coponent analysis of the Matrix X
Matrix must be n * m dimensions
where n is # features
m is # examples
'''
def PCA(X, varRetained = 0.95, show = False):
@bistaumanga
bistaumanga / phase_vocoder.py
Created August 19, 2013 13:12
Phase vocoder algorithm for time scaling and pitch scaling of audio signals
import numpy as np
from scipy.io import wavfile
from numpy.fft import fft, ifft, fftshift
def hamming(M):
n = np.arange(M)
return 0.54 - 0.46 * np.cos( 2 * np.pi * n / (M - 1))
def hann(M):
n = np.arange(M)
@bistaumanga
bistaumanga / gmm.py
Last active January 2, 2023 04:09
Gaussian Mixture Model using Expectation Maximization algorithm in python
# -*- coding: utf-8 -*-
"""
Created on Sat May 3 10:21:21 2014
@author: umb
"""
import numpy as np
class GMM:
@bistaumanga
bistaumanga / DTW_python.py
Created July 17, 2013 19:33
Dynamic Time warping implemented in python
'''Implementation and Demostration of Dynamic Time Warping
Requires : python 2.7.x, Numpy 1.7.1, Matplotlib, 1.2.1'''
from math import *
import numpy as np
import sys
def DTW(A, B, window = sys.maxint, d = lambda x,y: abs(x-y)):
# create the cost matrix
A, B = np.array(A), np.array(B)
@bistaumanga
bistaumanga / kMeans.py
Last active December 7, 2020 10:16
KMeans Clustering Implemented in python with numpy
'''Implementation and of K Means Clustering
Requires : python 2.7.x, Numpy 1.7.1+'''
import numpy as np
def kMeans(X, K, maxIters = 10, plot_progress = None):
centroids = X[np.random.choice(np.arange(len(X)), K), :]
for i in range(maxIters):
# Cluster Assignment step
C = np.array([np.argmin([np.dot(x_i-y_k, x_i-y_k) for y_k in centroids]) for x_i in X])
@bistaumanga
bistaumanga / myFFT.py
Last active October 19, 2022 14:59
Fast Fourier Transform Library in Python
#################################################################################
#
# This is FFT library implemented in python.
# The Algorithm used is FFT, decimation in time, radix -2 algorithm
# can compute FFT of 1-d and 2-d lists, 1-d for 1-d signals , and 2-d for images
#
# by : Umanga Bista @ bistaumanga@gmail.com
#
#################################################################################