<file_sep>from util import load_data
import matplotlib.pyplot as plt
from skimage import data
from skimage.viewer import ImageViewer


if __name__ == "__main__":
	data_in, data_targ, data_ids, unlabeled_in, test_in = load_data()
	image = data_in[0,:].reshape(32, 32)
	viewer = ImageViewer(image)
	viewer.show()<file_sep>from sklearn import svm
from sklearn.cross_validation import train_test_split, cross_val_score
from util import *
import numpy as np

NUM_TEST = 1253

def run_SVC(train_in, valid_in, train_targ, valid_targ):
    H_c = 3e2
    H_kernel = 'poly'
    H_degree = 2
    H_coef0 = 0.0
    H_g = 0.001

    classifier = svm.SVC(kernel=H_kernel, C=H_c, coef0=H_coef0, degree=H_degree, gamma=H_g)
    classifier.fit(train_in, train_targ)
    return classifier.score(valid_in, valid_targ), classifier

def run_nu_SVC(train_in, valid_in, train_targ, valid_targ):
    H_kernel = 'poly'
    H_degree = 2
    H_coef0 = 0.0
    H_nu = 0.27

    classifier = svm.NuSVC(nu=H_nu, kernel=H_kernel, coef0=H_coef0, degree=H_degree)
    classifier.fit(train_in, train_targ)
    return classifier.score(valid_in, valid_targ), classifier

if __name__ == "__main__":
    data_in, data_targ, data_ids, unlabeled_in, test_in = load_data()
    N, M = data_in.shape

    full_data = np.append(np.append(data_in, data_targ, axis=1), data_ids, axis=1)
    pruned_in, pruned_targ, pruned_ids = prune_individuals(full_data)
    num_iters = 35
    rates_svc = np.array([])
    svc_models = np.array([])
    rates_nusvc = np.array([])
    nusvc_models = np.array([])

    for i in xrange(num_iters):
        train_in, valid_in, train_targ, valid_targ = train_test_split(pruned_in,
            pruned_targ, test_size=0.28, stratify=pruned_ids)

        curr_svc_rate, svc = run_SVC(train_in, valid_in, train_targ, valid_targ)
        curr_nu_svc_rate, nu_svc = run_nu_SVC(train_in, valid_in, train_targ, valid_targ)

        rates_svc = np.append(rates_svc, curr_svc_rate)
        rates_nusvc = np.append(rates_nusvc, curr_nu_svc_rate)

        svc_models = np.append(svc_models, svc)
        nusvc_models = np.append(nusvc_models, nu_svc)

        print "SVC Success Rate = {} ; NuSVC Success Rate = {}".format(rates_svc[i], rates_nusvc[i])

    best_svc = svc_models[np.argmax(rates_svc)]
    best_nu_svc = nusvc_models[np.argmax(rates_nusvc)]

    print "Best SVC's accuracy = {}".format(np.max(rates_svc))
    print "Best NuSVC's accuracy = {}".format(np.max(rates_nusvc))
    print "Average SVC Success Rate = {}".format(np.mean(rates_svc))
    print "Average NuSVC Success Rate = {}".format(np.mean(rates_nusvc))

    max_accuracies = [np.max(rates_svc), np.max(rates_nusvc)]
    test_classifier = [best_svc, best_nu_svc][np.argmax(max_accuracies)]
    test_targ = best_svc.predict(test_in).astype(int)

    with open("submission.csv", "w+") as f:
        f.write("Id,Prediction\n")
        for i in xrange(NUM_TEST):
            if i > test_targ.size - 1:
                f.write("{},0\n".format(i+1))
            else:
                f.write("{},{}\n".format(i+1, test_targ[i]))
        f.close()
<file_sep>from sklearn import svm
from sklearn.cross_validation import train_test_split, cross_val_score
from util import *
from scipy.stats.mstats import mode
import numpy as np
import cPickle as pickle

NUM_TEST = 1253

def run_SVC(train_in, valid_in, train_targ, valid_targ):
    H_c = 3e2
    H_kernel = 'linear'
    H_degree = 2
    H_coef0 = 3e2
    H_g = 0.001

    classifier = svm.SVC(kernel=H_kernel, C=H_c, coef0=H_coef0, degree=H_degree, gamma=H_g)
    classifier.fit(train_in, train_targ)
    return classifier.score(valid_in, valid_targ), classifier

def run_nu_SVC(train_in, valid_in, train_targ, valid_targ):
    H_kernel = 'linear'
    H_degree = 2
    H_coef0 = 3e2
    H_nu = 0.27

    classifier = svm.NuSVC(nu=H_nu, kernel=H_kernel, coef0=H_coef0, degree=H_degree)
    classifier.fit(train_in, train_targ)
    return classifier.score(valid_in, valid_targ), classifier

if __name__ == "__main__":
    print "Loading data."
    data_in, data_targ, data_ids, unlabeled_in, test_in = load_data()
    N, M = data_in.shape
    
    gabor_data = pickle.load(open("gabor.p","rb"))
    gabor_test = pickle.load(open("gabor_test.p", "rb"))
    full_data = np.append(np.append(gabor_data, data_targ, axis=1), data_ids, axis=1)
    pruned_in, pruned_targ, pruned_ids = prune_individuals(full_data)
    num_experts = 10
    i = 0
    rates_svc = np.array([])
    svc_models = np.array([])
    rates_nusvc = np.array([])
    nusvc_models = np.array([])
    predictions = np.empty([NUM_TEST, 0])

    print "Finding {} experts.".format(num_experts)
    while i < num_experts:
        print "Training."
        train_in, valid_in, train_targ, valid_targ = train_test_split(pruned_in,
            pruned_targ, test_size=0.28, stratify=pruned_ids)

        curr_nu_svc_rate, nu_svc = run_nu_SVC(train_in, valid_in, train_targ, valid_targ)

        print "Score = {}".format(curr_nu_svc_rate)
        if curr_nu_svc_rate > 0.80:
            rates_nusvc = np.append(rates_nusvc, curr_nu_svc_rate)
            nusvc_models = np.append(nusvc_models, nu_svc)
            #nu_svc_predictions = np.append(nu_svc_predictions, nu_svc_prediction)
            i += 1
            print"{}.".format(i)
            print "Found a NuSVC Expert."
    
    for expert in nusvc_models:
        test_targ = expert.predict(gabor_test)
        predictions = np.append(predictions, test_targ.reshape(NUM_TEST, 1), axis=1)

    final_test_targ, _ = mode(predictions, axis=1)

    with open("submission.csv", "w+") as f:
        f.write("Id,Prediction\n")
        for i in xrange(NUM_TEST):
                f.write("{},{}\n".format(i+1, int(final_test_targ.flatten()[i])))
        f.close()
<file_sep>from sklearn.neighbors import KNeighborsClassifier
from sklearn.cross_validation import train_test_split
from util import *
import numpy as np

if __name__ == "__main__":
    k_vals = [3, 5, 7, 9, 11, 13, 14, 16, 21]
    data_in, data_targ, data_ids, unlabeled_in, test_in = load_data()
    
    N, M = data_in.shape
    full_data = np.append(np.append(data_in, data_targ, axis=1), data_ids, axis=1)

    pruned_in, pruned_targ, pruned_ids = prune_individuals(full_data)

    for k in k_vals:
        kNN = KNeighborsClassifier(n_neighbors=k)
        train_in, valid_in, train_targ, valid_targ = train_test_split(pruned_in, 
                pruned_targ, test_size=0.33, stratify=pruned_ids)
        kNN.fit(train_in, train_targ.flatten())
        print "k = {} ; classification rate = {}".format(k, kNN.score(valid_in, valid_targ))
<file_sep>import scipy.io as io
import sklearn as sk
import numpy as np
from sklearn.cross_validation import train_test_split

LABELED = "../data/labeled_images.mat"
UNLABELED = "../data/unlabeled_images.mat"
PUBLIC_TEST = "../data/public_test_images.mat"
HIDDEN_TEST = "../data/hidden_test_images.mat"
CSV_FILE_NAME = "submission.csv"

# This global variable represents the number of classes
# in this classification task.
K = 7

def load_data():
    '''
    Load all data and return it as a tuple of arrays.
    input:
        labeled_path - the path (relative or absolute) to labeled data.
                       this data MUST be stored in a .mat file.
        unlabeled_path - the path (relative or absolute) to unlabeled data.
                         this data MUST be stored in a .mat file.
        test_path - the path (relative or absolute) to test data.
                    this data MUST be stored in a .mat file.
    output:
        train_in - an NxM matrix of all training data.
        train_targ - an Nx1 matrix of labels corresponding to training data.
        train_ids - an Nx1 matrix of ids corresponding to training data.
        unlabeled_in - a KxM matrix of additional unlabeled training data.
        test_in - a JxM matrix of test data.
    '''
    # First, load the raw data into dictionaries.
    # These dictionaries all contain keys that allow us to index
    # into the data and metadata.
    loaded_labeled = io.loadmat(LABELED)
    loaded_unlabeled = io.loadmat(UNLABELED)
    loaded_pub_test = io.loadmat(PUBLIC_TEST)
    loaded_hid_test = io.loadmat(HIDDEN_TEST)

    train_in = reshape_data(loaded_labeled['tr_images'])
    train_targ = loaded_labeled['tr_labels']
    train_ids = loaded_labeled['tr_identity']

    pub_test_in = reshape_data(loaded_pub_test['public_test_images'])
    hid_test_in = reshape_data(loaded_hid_test['hidden_test_images'])

    test_in = np.vstack((pub_test_in, hid_test_in))

    unlabeled_in = reshape_data(loaded_unlabeled['unlabeled_images'])
   
    return train_in, train_targ, train_ids, unlabeled_in, test_in

def reshape_data(data_in):
    '''
    Return a z by x*y matrix of data points, where x & y are
    the dimensions of each ith datapoint, and z is the number of examples.
    input:
        data_in - an x by y by z matrix of datapoints.
    output:
        reshaped_data_in - a z by x*y matrix of datapoints.
    '''
    x, y, z = data_in.shape
    N, M = z, x *y
    return data_in.flatten().reshape(M, N).T

def concatenate_data_info(data_in, data_targ, data_ids):
    '''
    Concatenate two rows to data_in: the targets for each row,
    stored in data_targ; the ids for each row, stored in data_ids.
    Return the resulting matrix.
    input:
        data_in - an NxM matrix of data points per row.
        data_targ - an Nx1 matrix of data point targets.
        data_ids - an Nx1 matrix of data point identities.
    output:
        full_data - an NxM+2 matrix of data and its corresponding
                    target and identity.
    '''
    data_in_targ = np.append(data_in, data_targ, axis=1)
    return np.append(data_in_targ, data_ids, axis=1)

def prune_individuals(full_data):
    '''
    Replace all unique identifiers with a single identifier value.
    This is done to enable stratifying based on identifier.
    input:
        full_data - an NxM+2 matrix, where each row is a datapoint
                    of M features. the M+1th column is the label
                    for that datapoint and the M+2th column is the id.
    output:
        pruned_in - an NxM matrix of data points.
        pruned_targ - an Nx1 matrix of labels corresponding to pruned_in.
        pruned_ids - an Nx1 matrix of ids corresponding to pruned_in
    '''
    N, M = full_data.shape
    all_ids = full_data[:, M-1]
    new_ids = np.array([])
    for i in xrange(all_ids.size):
        if np.sum(all_ids == all_ids[i]) == 1:
            new_ids = np.append(new_ids, 42069)
        else:
            new_ids = np.append(new_ids, all_ids[i])
    pruned_full_data = np.append(full_data[:, :M-1], new_ids.reshape(-1, 1), axis=1)

    pruned_in = pruned_full_data[:, :M-2]
    pruned_targ = pruned_full_data[:, M-2]
    pruned_ids = pruned_full_data[:, M-1]

    return pruned_in, pruned_targ, pruned_ids

def write_prediction_to_csv(test_targ):
    '''
    Write a file to disk that formats the prediction stored in
    test_targ.
    input:
        test_targ - a Nx1 array of predictions for test data.
    '''
    N, _ = test_targ.shape
    with open(CSV_FILE_NAME, "w+") as f:
        f.write("Id,Prediction\n")
        for i in xrange(N):
            if i > N - 1:
                f.write("{},0\n".format(i+1))
            else:
                f.write("{},{}\n".format(i+1, int(test_targ.flatten()[i])))
        f.close()

def encode_prediction(model, data_in):
    '''
    Return a NxK matrix, where N is the number of examples and K
    is the number of classes. This matrix represents the predictions
    yielded by the input model in a 1-of-K format: that is, each row
    has a 1 in the kth position, representing the prediction for the
    corresponding row in data_in.
    input:
        model - a model that has been fit to training data.
                must be a scikit-learn model, i.e. have a "prediciton"
                method.
        data_in - an NxM matrix, where each row is a data point of M features.
    output:
        encoded - an NxK matrix, where each row has a 1 in the kth position.
                  this indications that example i is of class k.
    '''
    N, M = data_in.shape
    prediction = model.predict(data_in)
    encoded = np.zeros((N, K+1))
    encoded[np.arange(N), prediction.astype(int)] = 1
    return encoded
<file_sep>from sklearn.ensemble import AdaBoostClassifier
from sklearn.cross_validation import train_test_split
from util import *
import numpy as np

def run_AdaBoost(train_in, valid_in, train_targ, valid_targ):
    classifier = AdaBoostClassifier(learning_rate=0.8)
    classifier.fit(train_in, train_targ)
    print classifier.score(valid_in, valid_targ)

if __name__ == "__main__":
    data_in, data_targ, data_ids, unlabeled_in, test_in = load_data()
    N, M = data_in.shape

    full_data = np.append(np.append(data_in, data_targ, axis=1), data_ids, axis=1)
    pruned_in, pruned_targ, pruned_ids = prune_individuals(full_data)

    train_in, valid_in, train_targ, valid_targ = train_test_split(pruned_in,
            pruned_targ, test_size=0.33, stratify=pruned_ids)

    run_AdaBoost(train_in, valid_in, train_targ, valid_targ)
<file_sep>import matplotlib.pyplot as plt
import numpy as np
from scipy import ndimage as ndi

from skimage import data
from skimage.util import img_as_float
from skimage.filters import gabor_kernel
from skimage.viewer import ImageViewer

from util import load_data

import cPickle as pickle


def compute_gabor(image):
	images = []
	for theta in np.arange(0.1, np.pi, np.pi / 8):
		for lmda in np.arange(0.1, np.pi, np.pi/4): 

			imaginary, real = gabor_filter(image, .5,theta=theta,bandwidth=lmda)
			images.append(real)

	return images

def compute_feats(image, kernels):
	feats = []
	for k, kernel in enumerate(kernels):
		filtered = ndi.convolve(image, kernel, mode='wrap')
		feats.append(filtered.flatten())
	return np.vstack(feats).reshape(1,len(kernels)*image.size)

def gabor_features(data_in):
	#create gabor kernels
	kernels = []
	for theta in range(8):
		theta = theta / 8.0 * np.pi
		for sigma in (1, 3, 5):
			for frequency in (.10, 0.25, 1.0):
				kernel = np.real(gabor_kernel(frequency, theta=theta, sigma_x=sigma, sigma_y=sigma))
				kernels.append(kernel)

	#iterate over data points, and compute the result of applying each gabor kernel
	m,n = data_in.shape
	gabor_features = []
	for i in range(data_in.shape[0]):
		gabor_features.append(compute_feats(data_in[i].reshape(32,32), kernels))
	
	return np.vstack(gabor_features)

if __name__ == "__main__":
    data_in, data_targ, data_ids, unlabeled_in, test_in = load_data()
    print "Applying Gabor filters to input data."
    gabor_feats = gabor_features(data_in)
    print "Apply Gabor filters to test data."
    gabor_test = gabor_features(test_in)
    print "Dumping input data."
    pickle.dump(gabor_feats, open("gabor.p", "wb"))
    print "Dumping test data."
    pickle.dump(gabor_test, open("gabor_test.p", "wb"))
<file_sep>from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import train_test_split
from util import *
import numpy as np

if __name__ == "__main__":
    data_in, data_targ, data_ids, unlabeled_in, test_in = load_data()

    N, M = data_in.shape
    full_data = np.append(np.append(data_in, data_targ, axis=1), data_ids, axis=1)

    pruned_in, pruned_targ, pruned_ids = prune_individuals(full_data)

    logit = LogisticRegression()
    
    train_in, valid_in, train_targ, valid_targ = train_test_split(pruned_in,
            pruned_targ, test_size=0.33, stratify=pruned_ids)
    logit.fit(train_in, train_targ.flatten())
    print "Validation MCE = {}".format(logit.score(valid_in, valid_targ))

<file_sep>from sklearn import svm
from sklearn.decomposition import PCA
from sklearn.cross_validation import train_test_split
from util import *
import numpy as np

if __name__ == "__main__":
    data_in, data_targ, data_ids, unlabeled_in, test_in = load_data()
    N, M = data_in.shape

    full_data = np.append(np.append(data_in, data_targ, axis=1), data_ids, axis=1)
    pruned_in, pruned_targ, pruned_ids = prune_individuals(full_data)

    pca = PCA(n_components=200)
    print "Initializing PCA with {} components.".format(pca.n_components)
    svc = svm.SVC(C=4.0, kernel='linear', degree=2)
    print "Initializing SVM with a {} kernel of degree {}.".format(svc.kernel, svc.degree)

    print "Transforming input data."
    decomposed_in = pca.fit_transform(pruned_in)
    num_iters = 10
    rates = np.array([])
    for i in xrange(num_iters):
        print "ITERATION {}".format(i)
        train_in, valid_in, train_targ, valid_targ = train_test_split(decomposed_in,
                pruned_targ, test_size=0.33, stratify=pruned_ids)
        print "Fitting SVM."
        svc.fit(train_in, train_targ)
        rates = np.append(rates, svc.score(valid_in, valid_targ))
        print "Success rate = {}".format(rates[i])

    print "Average Success Rate = {}".format(np.mean(rates))
