Skip to content

Instantly share code, notes, and snippets.

@leonaburime
leonaburime / gradientDescent.py
Last active August 29, 2015 14:03
Gradient Descent Algorithm
from __future__ import division
import numpy as np
import math, pdb
from sklearn import linear_model
#http://stackoverflow.com/questions/17784587/gradient-descent-using-python-and-numpy
def genData(numPoints, bias, variance):
@leonaburime
leonaburime / kMeans.py
Last active August 29, 2015 14:03
kMeans Algorithm
import random, pdb
from pprint import pprint
import pandas as pd, numpy as np
import sklearn
from sklearn.cluster import KMeans
labels = {0 : 'apple', 1 : 'banana'}
input = [
@leonaburime
leonaburime / kNN.py
Last active August 29, 2015 14:03
k-Nearest Neighbors Algorithm
from __future__ import division
from pprint import pprint
import numpy as np
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
#References - http://saravananthirumuruganathan.wordpress.com/2010/05/17/a-detailed-introduction-to-k-nearest-neighbor-knn-algorithm/
#http://www.saedsayad.com/k_nearest_neighbors.htm
input = [
@leonaburime
leonaburime / linearRegression.py
Created June 27, 2014 06:16
Linear Regression for single variable. For multivariable check out my gradientDescent.py
import numpy as np
import dataMunge, pdb
from sklearn import linear_model
input = [
[95, 85],
[85, 95],
[80, 70],
[70, 65],
[60, 70]
@leonaburime
leonaburime / neuralNetwork.py
Last active August 29, 2015 14:03
Short implementation of a feedforward neural network with backpropagation
import numpy as np
import math
from pprint import pprint
import pdb
#Linearly separable
_or = {
'X': [[0,0],[0,1],[1,0],[1,1]],
@leonaburime
leonaburime / kNearestNeighbor.java
Created July 20, 2014 00:51
k Neareast-Neighbor implementation in Java. Closely mirrors the the python implementation.
package com.example.kNearestNeighbor;
//To use third party libraries simply download the jar
//and put it in the Program Files/jre/jdk#.#(version number)/lib/ext for example
//Can get the jar file for guava at https://code.google.com/p/guava-libraries/
import com.google.common.base.Functions;
import com.google.common.collect.Ordering;
//Can get the apache commons math file at
@leonaburime
leonaburime / NeuralNetwork.java
Created July 22, 2014 16:31
Neural Network implementation in Java. Uses Apache Common Math library.
package NeuralNetwork;
import org.apache.commons.lang3.*;
import org.apache.commons.math3.linear.*;
import org.apache.commons.math3.stat.StatUtils;
import javax.swing.*;
import java.util.*;
/**
@leonaburime
leonaburime / KMeans.java
Created July 22, 2014 21:20
KMeans algorithm implementation in Java. Imported Apache Commons 'lang' and 'math' library.
package KMeans;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.math3.linear.MatrixUtils;
import org.apache.commons.math3.linear.RealMatrix;
import org.apache.commons.math3.linear.RealVector;
import org.apache.commons.math3.stat.StatUtils;
import java.lang.reflect.Array;
import java.util.*;
@leonaburime
leonaburime / naiveBayes.py
Last active August 29, 2015 14:04
Naive Bayes implementation in Python. Will need to import datafile.py for this program.
from __future__ import division
import pandas as pd, numpy as np,datafile,math,pdb,itertools
from pprint import pprint
from collections import Counter
#Will need to import datafile.py and correct dataset for this program
class naiveBayes:
def __init__(self,name='play', testSize=0):
@leonaburime
leonaburime / datafile.py
Created July 22, 2014 22:54
File used to read in data -possibly into dataframes - for various sample python programs.
import urllib2, pandas as pd
d = {
'mushroom' :{
'features': [
'class','cap-shape', 'cap-surface', 'cap-color',
'bruises?','odor','gill-attachment',