Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
Fix sample_weights bug and update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
adithyabsk committed Aug 23, 2018
1 parent 8371b13 commit 4294fa5
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 20 deletions.
2 changes: 1 addition & 1 deletion doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Foreshadow: Simple Machine Learning Scaffolding
import numpy as np
import pandas as pd
from sklearn.datasets import boston_housing
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
import foreshadow as fs
Expand Down
2 changes: 1 addition & 1 deletion doc/users.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Also import sklearn, pandas, and numpy for the demo
import pandas as pd
from sklearn.datasets import boston_housing
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
Now load in the boston housing dataset from sklearn into pandas dataframes. This
Expand Down
6 changes: 2 additions & 4 deletions foreshadow/estimators/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
AutoEstimator and its selection
"""

import inspect
import operator
import warnings

Expand Down Expand Up @@ -215,12 +214,11 @@ def predict_proba(self, X): # pragma: no cover
X = check_df(X)
return self.estimator.predict_proba(X)

def score(self, X, y):
def score(self, X, y, sample_weight=None):
"""Uses the trained estimator to compute the evaluation score defined
by the estimator
Note: sample weight is not implemented as tpot does not accept it in
its score field
Note: sample weights are not supported
Args:
X (pandas.DataFrame or numpy.ndarray or list): The input feature(s)
Expand Down
11 changes: 7 additions & 4 deletions foreshadow/estimators/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Wrapped Estimator
"""

import inspect

from sklearn.base import BaseEstimator, TransformerMixin

from ..utils import check_df
Expand Down Expand Up @@ -62,22 +64,23 @@ def predict_proba(self, X):
X = check_df(X)
return self.estimator.predict_proba(X)

def score(self, X, y, sample_weight=None):
def score(self, X, y):
"""Uses the trained estimator to compute the evaluation score defined
by the estimator
Note: sample weights are not supported
Args:
X (:obj:`pandas.DataFrame` or :obj:`numpy.ndarray` or list):
The input feature(s)
y (:obj:`pandas.DataFrame` or :obj:`numpy.ndarray` or list):
The response feature(s)
sample_weight (:obj:`numpy.ndarray`, optional):
The weights to be used when scoring each sample
Returns:
float: A computed prediction fitness score
"""
X = check_df(X)
y = check_df(y)
y = self.preprocessor.transform(y)
return self.estimator.score(X, y, sample_weight)

return self.estimator.score(X, y)
2 changes: 2 additions & 0 deletions foreshadow/tests/test_estimators/test_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def test_invalid_kwargs_not_dict():
def test_override_kwarg_dict():
from foreshadow.estimators import AutoEstimator

# if this is erroring make sure that auto_sklearn is installed

ae = AutoEstimator(
problem_type="regression",
auto="autosklearn",
Expand Down
13 changes: 3 additions & 10 deletions foreshadow/tests/test_estimators/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def test_metaestimator_predict():
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

from foreshadow.estimators.meta import MetaEstimator
from foreshadow.estimators import MetaEstimator

np.random.seed(0)

Expand All @@ -34,7 +34,7 @@ def test_metaestimator_predict_proba():
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

from foreshadow.estimators.meta import MetaEstimator
from foreshadow.estimators import MetaEstimator

np.random.seed(0)

Expand All @@ -58,7 +58,7 @@ def test_metaestimator_score():
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

from foreshadow.estimators.meta import MetaEstimator
from foreshadow.estimators import MetaEstimator

np.random.seed(0)

Expand All @@ -75,10 +75,3 @@ def test_metaestimator_score():
assert np.allclose(
me.score(X_test, y_test), est.score(X_test, scaler.transform(y_test))
)

# test score parameter
sample_weight = np.random.randint(1, 10, y_test.size)
assert np.allclose(
me.score(X_test, y_test, sample_weight=sample_weight),
est.score(X_test, scaler.transform(y_test), sample_weight=sample_weight),
)

0 comments on commit 4294fa5

Please sign in to comment.