Skip to content

Commit

Permalink
apply linter
Browse files Browse the repository at this point in the history
  • Loading branch information
glemaitre committed May 1, 2024
1 parent 99daa0e commit 360c560
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 60 deletions.
25 changes: 15 additions & 10 deletions examples/plot_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
import numpy as np
from matplotlib import pyplot as plt

from skltemplate import TemplateClassifier

X = [[0, 0], [1, 1]]
Expand All @@ -22,19 +23,23 @@
X_1 = X_test[y_pred == 1]


p0 = plt.scatter(0, 0, c='red', s=100)
p1 = plt.scatter(1, 1, c='blue', s=100)
p0 = plt.scatter(0, 0, c="red", s=100)
p1 = plt.scatter(1, 1, c="blue", s=100)

ax0 = plt.scatter(X_0[:, 0], X_0[:, 1], c='crimson', s=50)
ax1 = plt.scatter(X_1[:, 0], X_1[:, 1], c='deepskyblue', s=50)
ax0 = plt.scatter(X_0[:, 0], X_0[:, 1], c="crimson", s=50)
ax1 = plt.scatter(X_1[:, 0], X_1[:, 1], c="deepskyblue", s=50)

leg = plt.legend([p0, p1, ax0, ax1],
['Point 0', 'Point 1', 'Class 0', 'Class 1'],
loc='upper left', fancybox=True, scatterpoints=1)
leg = plt.legend(
[p0, p1, ax0, ax1],
["Point 0", "Point 1", "Class 0", "Class 1"],
loc="upper left",
fancybox=True,
scatterpoints=1,
)
leg.get_frame().set_alpha(0.5)

plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.xlim([-.5, 1.5])
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.xlim([-0.5, 1.5])

plt.show()
3 changes: 2 additions & 1 deletion examples/plot_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
"""
import numpy as np
from matplotlib import pyplot as plt

from skltemplate import TemplateEstimator

X = np.arange(100).reshape(100, 1)
y = np.zeros((100, ))
y = np.zeros((100,))
estimator = TemplateEstimator()
estimator.fit(X, y)
plt.plot(estimator.predict(X))
Expand Down
13 changes: 7 additions & 6 deletions examples/plot_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@
"""
import numpy as np
from matplotlib import pyplot as plt

from skltemplate import TemplateTransformer

X = np.arange(50, dtype=np.float64).reshape(-1, 1)
X /= 50
estimator = TemplateTransformer()
X_transformed = estimator.fit_transform(X)

plt.plot(X.flatten(), label='Original Data')
plt.plot(X_transformed.flatten(), label='Transformed Data')
plt.title('Plots of original and transformed data')
plt.plot(X.flatten(), label="Original Data")
plt.plot(X_transformed.flatten(), label="Transformed Data")
plt.title("Plots of original and transformed data")

plt.legend(loc='best')
plt.legend(loc="best")
plt.grid(True)
plt.xlabel('Index')
plt.ylabel('Value of Data')
plt.xlabel("Index")
plt.ylabel("Value of Data")

plt.show()
13 changes: 7 additions & 6 deletions skltemplate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from ._template import TemplateEstimator
from ._template import TemplateClassifier
from ._template import TemplateTransformer

from ._template import TemplateClassifier, TemplateEstimator, TemplateTransformer
from ._version import __version__

__all__ = ['TemplateEstimator', 'TemplateClassifier', 'TemplateTransformer',
'__version__']
__all__ = [
"TemplateEstimator",
"TemplateClassifier",
"TemplateTransformer",
"__version__",
]
34 changes: 18 additions & 16 deletions skltemplate/_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"""
import numpy as np
from sklearn.base import BaseEstimator, ClassifierMixin, TransformerMixin
from sklearn.utils.validation import check_X_y, check_array, check_is_fitted
from sklearn.utils.multiclass import unique_labels
from sklearn.metrics import euclidean_distances
from sklearn.utils.multiclass import unique_labels
from sklearn.utils.validation import check_array, check_is_fitted, check_X_y


class TemplateEstimator(BaseEstimator):
""" A template estimator to be used as a reference implementation.
"""A template estimator to be used as a reference implementation.
For more information regarding how to build your own estimator, read more
in the :ref:`User Guide <user_guide>`.
Expand All @@ -29,7 +29,8 @@ class TemplateEstimator(BaseEstimator):
>>> estimator.fit(X, y)
TemplateEstimator()
"""
def __init__(self, demo_param='demo_param'):

def __init__(self, demo_param="demo_param"):
self.demo_param = demo_param

def fit(self, X, y):
Expand All @@ -54,7 +55,7 @@ def fit(self, X, y):
return self

def predict(self, X):
""" A reference implementation of a predicting function.
"""A reference implementation of a predicting function.
Parameters
----------
Expand All @@ -67,12 +68,12 @@ def predict(self, X):
Returns an array of ones.
"""
X = check_array(X, accept_sparse=True)
check_is_fitted(self, 'is_fitted_')
check_is_fitted(self, "is_fitted_")
return np.ones(X.shape[0], dtype=np.int64)


class TemplateClassifier(ClassifierMixin, BaseEstimator):
""" An example classifier which implements a 1-NN algorithm.
"""An example classifier which implements a 1-NN algorithm.
For more information regarding how to build your own classifier, read more
in the :ref:`User Guide <user_guide>`.
Expand All @@ -91,7 +92,8 @@ class TemplateClassifier(ClassifierMixin, BaseEstimator):
classes_ : ndarray, shape (n_classes,)
The classes seen at :meth:`fit`.
"""
def __init__(self, demo_param='demo'):

def __init__(self, demo_param="demo"):
self.demo_param = demo_param

def fit(self, X, y):
Expand Down Expand Up @@ -120,7 +122,7 @@ def fit(self, X, y):
return self

def predict(self, X):
""" A reference implementation of a prediction for a classifier.
"""A reference implementation of a prediction for a classifier.
Parameters
----------
Expand All @@ -134,7 +136,7 @@ def predict(self, X):
seen during fit.
"""
# Check is fit had been called
check_is_fitted(self, ['X_', 'y_'])
check_is_fitted(self, ["X_", "y_"])

# Input validation
X = check_array(X)
Expand All @@ -144,7 +146,7 @@ def predict(self, X):


class TemplateTransformer(TransformerMixin, BaseEstimator):
""" An example transformer that returns the element-wise square root.
"""An example transformer that returns the element-wise square root.
For more information regarding how to build your own transformer, read more
in the :ref:`User Guide <user_guide>`.
Expand All @@ -159,7 +161,8 @@ class TemplateTransformer(TransformerMixin, BaseEstimator):
n_features_ : int
The number of features of the data passed to :meth:`fit`.
"""
def __init__(self, demo_param='demo'):

def __init__(self, demo_param="demo"):
self.demo_param = demo_param

def fit(self, X, y=None):
Expand All @@ -186,7 +189,7 @@ def fit(self, X, y=None):
return self

def transform(self, X):
""" A reference implementation of a transform function.
"""A reference implementation of a transform function.
Parameters
----------
Expand All @@ -200,14 +203,13 @@ def transform(self, X):
in ``X``.
"""
# Check is fit had been called
check_is_fitted(self, 'n_features_')
check_is_fitted(self, "n_features_")

# Input validation
X = check_array(X, accept_sparse=True)

# Check that the input is of the same shape as the one passed
# during fit.
if X.shape[1] != self.n_features_:
raise ValueError('Shape of input is different from what was seen'
'in `fit`')
raise ValueError("Shape of input is different from what was seenin `fit`")
return np.sqrt(X)
2 changes: 1 addition & 1 deletion skltemplate/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.4.dev0"
__version__ = "0.0.4.dev0"
8 changes: 2 additions & 6 deletions skltemplate/tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import pytest

from sklearn.utils.estimator_checks import check_estimator

from skltemplate import TemplateEstimator
from skltemplate import TemplateClassifier
from skltemplate import TemplateTransformer
from skltemplate import TemplateClassifier, TemplateEstimator, TemplateTransformer


@pytest.mark.parametrize(
"estimator",
[TemplateEstimator(), TemplateTransformer(), TemplateClassifier()]
"estimator", [TemplateEstimator(), TemplateTransformer(), TemplateClassifier()]
)
def test_all_estimators(estimator):
return check_estimator(estimator)
25 changes: 11 additions & 14 deletions skltemplate/tests/test_template.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import pytest
import numpy as np

import pytest
from numpy.testing import assert_allclose, assert_array_equal
from sklearn.datasets import load_iris
from numpy.testing import assert_array_equal
from numpy.testing import assert_allclose

from skltemplate import TemplateEstimator
from skltemplate import TemplateTransformer
from skltemplate import TemplateClassifier
from skltemplate import TemplateClassifier, TemplateEstimator, TemplateTransformer


@pytest.fixture
def data():
return load_iris(return_X_y=True)


def test_template_estimator(data):
est = TemplateEstimator()
assert est.demo_param == 'demo_param'
assert est.demo_param == "demo_param"

est.fit(*data)
assert hasattr(est, 'is_fitted_')
assert hasattr(est, "is_fitted_")

X = data[0]
y_pred = est.predict(X)
Expand All @@ -38,7 +35,7 @@ def test_template_transformer_error(data):
def test_template_transformer(data):
X, y = data
trans = TemplateTransformer()
assert trans.demo_param == 'demo'
assert trans.demo_param == "demo"

trans.fit(X)
assert trans.n_features_ == X.shape[1]
Expand All @@ -53,12 +50,12 @@ def test_template_transformer(data):
def test_template_classifier(data):
X, y = data
clf = TemplateClassifier()
assert clf.demo_param == 'demo'
assert clf.demo_param == "demo"

clf.fit(X, y)
assert hasattr(clf, 'classes_')
assert hasattr(clf, 'X_')
assert hasattr(clf, 'y_')
assert hasattr(clf, "classes_")
assert hasattr(clf, "X_")
assert hasattr(clf, "y_")

y_pred = clf.predict(X)
assert y_pred.shape == (X.shape[0],)

0 comments on commit 360c560

Please sign in to comment.