Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class KNN(object): | |
POSSIBLE_K_VALUES = [k for k in range(1,9) if k%2 == 1] | |
def __init__(self, k=None, weights=None): | |
self._known_rows = [] | |
self.k = None | |
self.weights = None | |
def learn_from_row(self, row): | |
self._known_rows.append(row) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def goodness_measure_factory(self, missing_column_index, k): | |
CUTOFF_PERCENT = 0.9 | |
def goodness_measure(some_weights): | |
# Do the following num_tests times: Split the known rows into | |
# a training set and a test set. Then use the metric based | |
# on some_weights, train on the training set, and test on the | |
# testing set; record how successful you were in classifying as | |
# a percent of attempts, and average that over num_tests to | |
# get your goodness measure. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@classmethod | |
def get_random_weights(cls, row_length): | |
weights = [] | |
room_left = 1.0 | |
for j in range(row_length - 2): | |
weight_j = random.uniform(0, room_left) | |
weights.append(weight_j) | |
room_left -= weight_j | |
weights.append(room_left) | |
random.shuffle(weights) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@classmethod | |
def transition(cls, some_weights): | |
candidate = some_weights[:] | |
a,b = random.sample(range(len(candidate)), 2) | |
m = min(.05, 1 - candidate[a], candidate[b]) | |
candidate[a] += m | |
candidate[b] -= m | |
return candidate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SimpleMCMC(object): | |
def __init__(self, start_state, transition, goodness_measure): | |
self.present_state = start_state | |
self.transition = transition | |
self.goodness_measure = goodness_measure | |
self.present_goodness = self.goodness_measure(self.present_state) | |
def take_step(self): | |
# Use the transition function to find a candidate for the new state. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Compute the euclidean distance between vectors v and w. | |
euclid = lambda w,v : (sum((wi - vi)**2 for wi,vi in zip(w,v)))**.5 | |
# Quick-Sort the list l | |
qsort = lambda l : [x for x in l[1:] if x < l[0]] + [l[0]] + [x for x in l[1:] if x >= l[0]] | |
# Flatten the list l. E.g. [1, [2], [[3, [4]]]] -> [1,2,3,4]. Warning: O(n^2) | |
flatten = lambda l : sum(flatten(x) if isinstance(x, list) else [x] for x in l, []) | |
# compute the product of the elements in the list l. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import random | |
import time | |
import sys | |
def timeit(function, args, num_iterations): | |
start = time.time() | |
for i in range(num_iterations): | |
function(*args) | |
end = time.time() |
NewerOlder