Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions _doc/examples/plot_constraint_kmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
x = [km.cluster_centers_[i, 0], km.cluster_centers_[i, 0]]
y = [km.cluster_centers_[i, 1], km.cluster_centers_[i, 1]]
ax.plot(x, y, colors[i] + '+')
ax.set_title('KMeans 4 clusters\n%r' % hist)
ax.set_title(f'KMeans 4 clusters\n{hist!r}')
ax.legend()

#####################################
Expand Down Expand Up @@ -94,9 +94,9 @@
x = [km2.cluster_centers_[i, 0], km2.cluster_centers_[i, 0]]
y = [km2.cluster_centers_[i, 1], km2.cluster_centers_[i, 1]]
ax[1].plot(x, y, colors[i] + '+')
ax[0].set_title('ConstraintKMeans 4 clusters (gains)\n%r' % hist1)
ax[0].set_title(f'ConstraintKMeans 4 clusters (gains)\n{hist1!r}')
ax[0].legend()
ax[1].set_title('ConstraintKMeans 4 clusters (distances)\n%r' % hist2)
ax[1].set_title(f'ConstraintKMeans 4 clusters (distances)\n{hist2!r}')
ax[1].legend()


Expand Down Expand Up @@ -129,7 +129,7 @@ def plot_delaunay(ax, edges, points):
x = [km.cluster_centers_[i, 0], km.cluster_centers_[i, 0]]
y = [km.cluster_centers_[i, 1], km.cluster_centers_[i, 1]]
ax[0].plot(x, y, colors[i] + '+')
ax[0].set_title("ConstraintKMeans 4 clusters\nstrategy='weights'\n%r" % hist)
ax[0].set_title(f"ConstraintKMeans 4 clusters\nstrategy='weights'\n{hist!r}")
ax[0].legend()

cls = km.cluster_centers_iter_
Expand Down
3 changes: 1 addition & 2 deletions _unittests/ut_documentation/test_nb_search_keras.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def test_notebook_search_images(self):
except (SyntaxError, ModuleNotFoundError, AttributeError,
ImportError) as e:
warnings.warn(
"tensorflow is probably not available yet on python 3.7: "
"{0}".format(e))
f"tensorflow is probably not available yet on python 3.7: {e}")
return

self.assertTrue(mlinsights is not None)
Expand Down
4 changes: 2 additions & 2 deletions _unittests/ut_plotting/test_plot_gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_plot_gallery_url(self):
try:
fig, ax = plot_gallery_images(files, return_figure=True)
except http.client.RemoteDisconnected as e:
warnings.warn("Unable to fetch image {0}'".format(e))
warnings.warn(f"Unable to fetch image {e}'")
return
img = os.path.join(temp, "gallery.png")
fig.savefig(img)
Expand All @@ -68,7 +68,7 @@ def test_plot_gallery_url(self):
ax = plot_gallery_images(files, return_figure=False, ax=ax)
self.assertNotEmpty(ax)
except http.client.RemoteDisconnected as e:
warnings.warn("Unable to fetch image {0}'".format(e))
warnings.warn(f"Unable to fetch image {e}'")
return


Expand Down
2 changes: 1 addition & 1 deletion _unittests/ut_search_rank/test_LONG_search_images_keras.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_search_predictions_keras(self):
except (SyntaxError, ModuleNotFoundError, AttributeError,
ImportError) as e:
warnings.warn(
"Issue with tensorflow or keras: {0}".format(e))
f"Issue with tensorflow or keras: {e}")
return
from keras.preprocessing.image import ImageDataGenerator # pylint: disable=E0401,E0611
from keras.preprocessing.image import img_to_array, load_img # pylint: disable=E0401,E0611
Expand Down
2 changes: 1 addition & 1 deletion _unittests/ut_search_rank/test_LONG_search_images_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_search_predictions_torch(self):
import torchvision.models as tmodels # pylint: disable=E0401,C0415
except (SyntaxError, ModuleNotFoundError) as e:
warnings.warn(
"torch is not available: {0}".format(e))
f"torch is not available: {e}")
return
from torchvision import datasets, transforms # pylint: disable=E0401
from torch.utils.data import DataLoader # pylint: disable=E0401
Expand Down
6 changes: 3 additions & 3 deletions mlinsights/helpers/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def format_value(v):
@return a string
"""
return ("'{0}'".format(v.replace("'", "\\'"))
if isinstance(v, str) else "{0}".format(v))
if isinstance(v, str) else f"{v}")


def format_parameters(pdict):
Expand All @@ -33,7 +33,7 @@ def format_parameters(pdict):
"""
res = []
for k, v in sorted(pdict.items()):
res.append('{0}={1}'.format(k, format_value(v)))
res.append(f'{k}={format_value(v)}')
return ", ".join(res)


Expand All @@ -52,5 +52,5 @@ def format_function_call(name, pdict):
d = dict(i=2, x=6.7, s="r")
print(format_function_call("fct", d))
"""
res = '{0}({1})'.format(name, format_parameters(pdict))
res = f'{name}({format_parameters(pdict)})'
return "\n".join(textwrap.wrap(res, width=70, subsequent_indent=' '))
11 changes: 5 additions & 6 deletions mlinsights/helpers/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class PassThrough:
pass
else:
raise TypeError( # pragma: no cover
"pipe is not a scikit-learn object: {}\n{}".format(type(pipe), pipe))
f"pipe is not a scikit-learn object: {type(pipe)}\n{pipe}")


class BaseEstimatorDebugInformation:
Expand Down Expand Up @@ -112,8 +112,7 @@ def to_str(self, nrows=5):
"""
Tries to produce a readable message.
"""
rows = ['BaseEstimatorDebugInformation({})'.format(
self.model.__class__.__name__)]
rows = [f'BaseEstimatorDebugInformation({self.model.__class__.__name__})']
for k in sorted(self.inputs):
if k in self.outputs:
rows.append(' ' + k + '(')
Expand All @@ -126,7 +125,7 @@ def to_str(self, nrows=5):
rows.append(' )')
else:
raise KeyError( # pragma: no cover
"Unable to find output for method '{}'.".format(k))
f"Unable to find output for method '{k}'.")
return "\n".join(rows)

def display(self, data, nrows):
Expand All @@ -139,9 +138,9 @@ def display(self, data, nrows):
rows = rows[:nrows]
rows.append('...')
if hasattr(data, 'shape'):
rows.insert(0, "shape=%r type=%r" % (data.shape, type(data)))
rows.insert(0, f"shape={data.shape!r} type={type(data)!r}")
else:
rows.insert(0, "type=%r" % type(data)) # pragma: no cover
rows.insert(0, f"type={type(data)!r}") # pragma: no cover
return "\n".join(rows)


Expand Down
4 changes: 2 additions & 2 deletions mlinsights/metrics/correlations.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ def non_linear_correlations(df, model, draws=5, minmax=False):
xj_test = df_test[:, j:j + 1]
if len(xj_test) == 0 or len(xi_test) == 0:
raise ValueError( # pragma: no cover
"One column is empty i={0} j={1}.".format(i, j))
f"One column is empty i={i} j={j}.")
mod = clone(model)
try:
mod.fit(xi_train, xj_train.ravel())
except Exception as e: # pragma: no cover
raise ValueError(
"Unable to compute correlation for i={0} j={1}.".format(i, j)) from e
f"Unable to compute correlation for i={i} j={j}.") from e
v = mod.predict(xi_test)
c = (1 - numpy.var(v - xj_test.ravel()))
co = max(c, 0) ** 0.5
Expand Down
8 changes: 4 additions & 4 deletions mlinsights/mlbatch/cache_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def cache(self, params, value):
key = MLCache.as_key(params)
if key in self.cached:
raise KeyError( # pragma: no cover
"Key {0} already exists".format(params))
f"Key {params} already exists")
self.cached[key] = value
self.count_[key] = 0

Expand Down Expand Up @@ -77,7 +77,7 @@ def as_key(params):
elif isinstance(v, tuple):
if not all(map(lambda e: isinstance(e, (int, float, str)), v)):
raise TypeError( # pragma: no cover
"Unable to create a key with value '{0}':{1}".format(k, v))
f"Unable to create a key with value '{k}':{v}")
return str(v)
elif isinstance(v, numpy.ndarray):
# id(v) may have been better but
Expand All @@ -87,7 +87,7 @@ def as_key(params):
sv = ""
else:
raise TypeError( # pragma: no cover
"Unable to create a key with value '{0}':{1}".format(k, v))
f"Unable to create a key with value '{k}':{v}")
els.append((k, sv))
return str(els)

Expand Down Expand Up @@ -122,7 +122,7 @@ def create_cache(name):
global _caches # pylint: disable=W0603,W0602
if name in _caches:
raise RuntimeError( # pragma: no cover
"cache '{0}' already exists.".format(name))
f"cache '{name}' already exists.")

cache = MLCache(name)
_caches[name] = cache
Expand Down
2 changes: 1 addition & 1 deletion mlinsights/mlmodel/_kmeans_022.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _labels_inertia_precompute_dense(norm, X, sample_weight, centers, distances)
X=X, Y=centers, metric='manhattan')
else: # pragma no cover
raise NotImplementedError(
"Not implemented for norm '{}'.".format(norm))
f"Not implemented for norm '{norm}'.")
# cython k-means code assumes int32 inputs
labels = labels.astype(numpy.int32, copy=False)
if n_samples == distances.shape[0]:
Expand Down
13 changes: 6 additions & 7 deletions mlinsights/mlmodel/_kmeans_constraint_.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def linearize_matrix(mat, *adds):
res[i, k + 3] = am[a, b]
return res
raise NotImplementedError( # pragma: no cover
"This kind of sparse matrix is not handled: {0}".format(type(mat)))
f"This kind of sparse matrix is not handled: {type(mat)}")
else:
n = mat.shape[0]
c = mat.shape[1]
Expand Down Expand Up @@ -91,7 +91,7 @@ def constraint_kmeans(X, labels, sample_weight, centers, inertia,
"""
if labels.dtype != numpy.int32:
raise TypeError( # pragma: no cover
"Labels must be an array of int not '{0}'".format(labels.dtype))
f"Labels must be an array of int not '{labels.dtype}'")

if strategy == 'weights':
return _constraint_kmeans_weights(
Expand Down Expand Up @@ -222,8 +222,7 @@ def _constraint_association(leftover, counters, labels, leftclose, distances_clo
return _constraint_association_gain(
leftover, counters, labels, leftclose, distances_close,
centers, X, x_squared_norms, limit, strategy, state=state)
raise ValueError("Unknwon strategy '{0}'.".format(
strategy)) # pragma: no cover
raise ValueError(f"Unknwon strategy '{strategy}'.") # pragma: no cover


def _compute_strategy_coefficient(distances, strategy, labels):
Expand All @@ -235,7 +234,7 @@ def _compute_strategy_coefficient(distances, strategy, labels):
dist = distances[ar, labels]
return distances - dist[:, numpy.newaxis]
raise ValueError( # pragma: no cover
"Unknwon strategy '{0}'.".format(strategy))
f"Unknwon strategy '{strategy}'.")


def _randomize_index(index, weights):
Expand Down Expand Up @@ -479,7 +478,7 @@ def loopf(h, sumi):
neg = (counters < ave).sum()
if neg > 0:
raise RuntimeError( # pragma: no cover
"The algorithm failed, counters={0}".format(counters))
f"The algorithm failed, counters={counters}")

_switch_clusters(labels, distances)
distances_close[:] = distances[numpy.arange(X.shape[0]), labels]
Expand Down Expand Up @@ -540,7 +539,7 @@ def _constraint_kmeans_weights(X, labels, sample_weight, centers, inertia, it,
if len(set(labels)) != centers.shape[0]:
if verbose and fLOG: # pragma: no cover
if isinstance(verbose, int) and verbose >= 10:
fLOG("CKMeans new weights: w=%r" % weights)
fLOG(f"CKMeans new weights: w={weights!r}")
else:
fLOG("CKMeans new weights")
weights[:] = 1
Expand Down
8 changes: 4 additions & 4 deletions mlinsights/mlmodel/categories_to_integers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def fit(self, X, y=None, **fit_params):
"""
if not isinstance(X, pandas.DataFrame):
raise TypeError( # pragma: no cover
"this transformer only accept Dataframes, not {0}".format(type(X)))
f"this transformer only accept Dataframes, not {type(X)}")
if self.columns:
columns = self.columns
else:
Expand All @@ -89,7 +89,7 @@ def fit(self, X, y=None, **fit_params):
nb = len(distinct)
if nb >= max_cat:
raise ValueError( # pragma: no cover
"Too many categories ({0}) for one column '{1}' max_cat={2}".format(nb, c, max_cat))
f"Too many categories ({nb}) for one column '{c}' max_cat={max_cat}")
self._categories[c] = dict((c, i)
for i, c in enumerate(list(sorted(distinct))))
self._schema = self._build_schema()
Expand All @@ -107,7 +107,7 @@ def _build_schema(self):
new_vector = {}
last = 0
for c, v in self._categories.items():
sch = [(_[1], "{0}={1}".format(c, _[1]))
sch = [(_[1], f"{c}={_[1]}")
for _ in sorted((n, d) for d, n in v.items())]
if self.remove:
sch = [d for d in sch if d[1] not in self.remove]
Expand All @@ -133,7 +133,7 @@ def transform(self, X, y=None, **fit_params):
"""
if not isinstance(X, pandas.DataFrame):
raise TypeError( # pragma: no cover
"X is not a dataframe: {0}".format(type(X)))
f"X is not a dataframe: {type(X)}")

if self.single:
b = not self.skip_errors
Expand Down
6 changes: 3 additions & 3 deletions mlinsights/mlmodel/classification_kmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def set_params(self, **values):
pc[k[2:]] = v
else:
raise ValueError( # pragma: no cover
"Unexpected parameter name '{0}'".format(k))
f"Unexpected parameter name '{k}'")
self.clus.set_params(**pc)
self.estimator.set_params(**pe)

Expand All @@ -165,8 +165,8 @@ def __repr__(self): # pylint: disable=W0222
Overloads `repr` as *scikit-learn* now relies
on the constructor signature.
"""
el = ', '.join(['%s=%r' % (k, v)
el = ', '.join([f'{k}={v!r}'
for k, v in self.get_params().items()])
text = "%s(%s)" % (self.__class__.__name__, el)
text = f"{self.__class__.__name__}({el})"
lines = textwrap.wrap(text, subsequent_indent=' ')
return "\n".join(lines)
2 changes: 1 addition & 1 deletion mlinsights/mlmodel/decision_tree_logreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def fit(self, X, y, sample_weight=None):
if self.strategy == 'perpendicular':
return self._fit_perpendicular(X, y, sample_weight)
raise ValueError(
"Unknown strategy '{}'.".format(self.strategy))
f"Unknown strategy '{self.strategy}'.")

def _fit_parallel(self, X, y, sample_weight):
"Implements the parallel strategy."
Expand Down
9 changes: 4 additions & 5 deletions mlinsights/mlmodel/extended_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def get_feature_names(self, input_features=None):
if self.kind == 'poly-slow':
return self._get_feature_names_poly(input_features)
raise ValueError( # pragma: no cover
"Unknown extended features '{}'.".format(self.kind))
f"Unknown extended features '{self.kind}'.")

def _get_feature_names_poly(self, input_features=None):
"""
Expand All @@ -72,8 +72,7 @@ def _get_feature_names_poly(self, input_features=None):
i for i in range(0, self.n_input_features_)]
elif len(input_features) != self.n_input_features_:
raise ValueError( # pragma: no cover
"input_features should contain {} strings.".format(
self.n_input_features_))
f"input_features should contain {self.n_input_features_} strings.")

names = ["1"] if self.poly_include_bias else []
n = self.n_input_features_
Expand Down Expand Up @@ -126,7 +125,7 @@ def fit(self, X, y=None):
elif self.kind == 'poly-slow':
return self._fit_poly(X, y)
raise ValueError( # pragma: no cover
"Unknown extended features '{}'.".format(self.kind))
f"Unknown extended features '{self.kind}'.")

def _fit_poly(self, X, y=None):
"""
Expand Down Expand Up @@ -155,7 +154,7 @@ def transform(self, X):
if self.kind == 'poly-slow':
return self._transform_poly_slow(X)
raise ValueError( # pragma: no cover
"Unknown extended features '{}'.".format(self.kind))
f"Unknown extended features '{self.kind}'.")

def _transform_poly(self, X):
"""
Expand Down
3 changes: 1 addition & 2 deletions mlinsights/mlmodel/kmeans_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ def __init__(self, n_clusters=8, init='k-means++', n_init=10, max_iter=500,
self.history = history
self.learning_rate = learning_rate
if strategy not in ConstraintKMeans._strategy_value:
raise ValueError('strategy must be in {0}'.format(
ConstraintKMeans._strategy_value))
raise ValueError(f'strategy must be in {ConstraintKMeans._strategy_value}')

def fit(self, X, y=None, sample_weight=None, fLOG=None):
"""
Expand Down
Loading