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 itertools | |
from sklearn.metrics.pairwise import euclidean_distances | |
def tsp(points): | |
distances = euclidean_distances(points) | |
A = {(frozenset([0, j+1]), j+1): dist for j, dist in enumerate(distances[0][1:])} | |
A[frozenset([0]), 0] = 0 | |
n = len(points) |
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 numba | |
@numba.jit | |
def bellman_ford(graph, start_vertex): | |
n = len(graph.nodes) | |
A = np.zeros((n+1, n)) | |
A[0, :] = np.inf | |
A[0, start_vertex] = 0 | |
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 numba | |
@numba.jit | |
def floyd_marshall(graph): | |
n = len(graph.nodes) | |
A = np.full((n, n, n), fill_value=np.inf) | |
# base cases | |
for edge in graph.edges: |
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 functools | |
def knapsack(capacity, items): | |
n = len(items) | |
A = np.zeros((n, capacity)) | |
for i in range(n): | |
value, weight = items[i] |
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 UnionFind: | |
def __init__(self, ids): | |
self._id = {i: i for i in ids} # pointer to the leader | |
self.sizes = {i: 1 for i in ids} | |
self.n_components = len(set(self._id)) | |
def _root(self, i): | |
j = i | |
while (j != self._id[j]): | |
self._id[j] = self._id[self._id[j]] |
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 SCC(): | |
def __init__(self, graph): | |
self.graph = graph | |
self.time = 0 | |
self.visited = [] | |
self.finish_times = {} | |
def transpose_graph(self): | |
g_rev = {} |
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 | |
from copy import deepcopy | |
def random_contraction(graph, n_repeat=None): | |
""" | |
graph: undirected graph as a dict of lists | |
(adjacency list representation) | |
n_repeat: Number of repeated independent trials | |
""" | |
if n_repeat is None: |
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
from embedder.regression import Embedder | |
from embedder.assessment import visualize | |
import matplotlib.pyplot as plt | |
embedder = Embedder(embedding_dict) | |
embedder.fit(X_encoded, y) | |
embeddings = embedder.get_embeddings() | |
print(type(embeddings)) # a dictionary |
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
from embedder.regression import Embedder | |
from sklearn.pipeline import Pipeline | |
from xgboost import XGBRegressor | |
embedder = Embedder(embedding_dict) | |
embedder.fit(X_encoded, y) | |
X_embedded = embedder.transform(X) | |
# X_embedded = embedder.fit_transform(X, y) |
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 pandas as pd | |
from embedder.regression import Embedder | |
from embedder.preprocessing import (categorize, | |
pick_emb_dim, encode_categorical) | |
rossman = pd.read_csv('rossman.csv') | |
y = rossman['Sales'] | |
X = rossman.drop('Sales', axis=1) | |
cat_vars = categorize(rossman) |
NewerOlder