-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
An apparently decent cut of tree (non-overlapping) solver for wise-pizza
- Loading branch information
1 parent
b5ff932
commit 56fd3ad
Showing
6 changed files
with
198 additions
and
64 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from typing import List | ||
from abc import ABC, abstractmethod | ||
|
||
import numpy as np | ||
|
||
|
||
class Fitter(ABC): | ||
@abstractmethod | ||
def fit(self, X, y, sample_weight=None): | ||
pass | ||
|
||
@abstractmethod | ||
def predict(self, X): | ||
pass | ||
|
||
def fit_predict(self, X, y, sample_weight=None): | ||
self.fit(X, y, sample_weight) | ||
return self.predict(X) | ||
|
||
def error(self, X, y, sample_weight=None): | ||
err = y - self.predict(X) | ||
if sample_weight is not None: | ||
err *= sample_weight | ||
return np.nansum(err**2) | ||
|
||
|
||
class AverageFitter(Fitter): | ||
def __init__(self): | ||
self.avg = None | ||
|
||
def fit(self, X, y, sample_weight=None): | ||
y = np.array(y) | ||
sample_weight = np.array(sample_weight) | ||
if sample_weight is None: | ||
self.avg = np.nanmean(y) | ||
else: | ||
self.avg = np.nansum(y * sample_weight) / np.nansum(sample_weight) | ||
|
||
def predict(self, X): | ||
return np.full(X.shape[0], self.avg) |
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
Oops, something went wrong.