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 reward_function(params): | |
''' | |
Example of rewarding the agent to follow center line | |
''' | |
import math | |
# Read input parameters | |
track_width = params['track_width'] | |
distance_from_center = params['distance_from_center'] | |
steering = abs(params['steering_angle']) | |
all_wheels_on_track = params['all_wheels_on_track'] |
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 reward_function(params): | |
''' | |
Example of rewarding the agent to stay within boundary | |
''' | |
import math | |
# Read input parameters | |
track_width = params['track_width'] | |
distance_from_center = params['distance_from_center'] | |
steering = abs(params['steering_angle']) | |
all_wheels_on_track = params['all_wheels_on_track'] |
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
%%time | |
scores={} | |
for col in X_valid.columns: | |
X = X_valid.copy() | |
X[col] = np.random.choice(X[col], len(X)) | |
scores[col]=rmse(m.predict(X), y_valid)-base |
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 get_weights(*dims): return nn.Parameter(torch.randn(dims)/dims[0]) | |
def softmax(x): return torch.exp(x)/(torch.exp(x).sum(dim=1)[:,None]) | |
class LogReg(nn.Module): | |
def __init__(self): | |
super().__init__() | |
self.l1_w = get_weights(28*28, 10) # Layer 1 weights | |
self.l1_b = get_weights(10) # Layer 1 bias | |
def forward(self, x): |
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
# coding: utf-8 | |
# In[1]: | |
import numpy as np | |
import pandas as pd | |
from matplotlib import pyplot as plt | |
from sklearn.ensemble import RandomForestRegressor |
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 TreeEnsemble(): | |
def __init__(self, x, y, n_trees, sample_sz, min_leaf=5): | |
np.random.seed(42) | |
self.x,self.y,self.sample_sz,self.min_leaf = x,y,sample_sz,min_leaf | |
self.trees = [self.create_tree() for i in range(n_trees)] | |
def create_tree(self): | |
idxs = np.random.permutation(len(self.y))[:self.sample_sz] | |
return DecisionTree(self.x.iloc[idxs], self.y[idxs], | |
idxs=np.array(range(self.sample_sz)), min_leaf=self.min_leaf) |
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 find_varsplit(self): | |
for i in range(self.c): self.find_better_split(i) | |
if self.is_leaf: return | |
x = self.split_col | |
lhs = np.nonzero(x<=self.split)[0] | |
rhs = np.nonzero(x>self.split)[0] | |
self.lhs = DecisionTree(self.x, self.y, self.idxs[lhs]) | |
self.rhs = DecisionTree(self.x, self.y, self.idxs[rhs]) |
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 find_better_split(self, var_idx): | |
x,y = self.x.values[self.idxs,var_idx], self.y[self.idxs] | |
for i in range(self.n): | |
lhs = x<=x[i] | |
rhs = x>x[i] | |
if rhs.sum()<self.min_leaf or lhs.sum()<self.min_leaf: continue | |
lhs_std = y[lhs].std() | |
rhs_std = y[rhs].std() | |
curr_score = lhs_std*lhs.sum() + rhs_std*rhs.sum() |
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 DecisionTree(): | |
def __init__(self, x, y, idxs=None, min_leaf=5): | |
if idxs is None: idxs=np.arange(len(y)) | |
self.x,self.y,self.idxs,self.min_leaf = x,y,idxs,min_leaf | |
self.n,self.c = len(idxs), x.shape[1] | |
self.val = np.mean(y[idxs]) | |
self.score = float('inf') | |
self.find_varsplit() | |
# This just does one decision; we'll make it recursive later |
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 TreeEnsemble(): | |
def __init__(self, x, y, n_trees, sample_sz, min_leaf=5): | |
np.random.seed(42) | |
self.x,self.y,self.sample_sz,self.min_leaf = x,y,sample_sz,min_leaf | |
self.trees = [self.create_tree() for i in range(n_trees)] | |
def create_tree(self): | |
rnd_idxs = np.random.permutation(len(self.y))[:self.sample_sz] | |
return DecisionTree(self.x.iloc[rnd_idxs], self.y[rnd_idxs], min_leaf=self.min_leaf) | |
NewerOlder