Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MNT towards removing assert_equal, etc #14222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jul 1, 2019
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 sklearn/cluster/tests/test_affinity_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from sklearn.exceptions import ConvergenceWarning
from sklearn.utils.testing import (
assert_equal, assert_array_equal, assert_raises,
assert_array_equal, assert_raises,
assert_warns, assert_warns_message, assert_no_warnings)

from sklearn.cluster.affinity_propagation_ import AffinityPropagation
Expand Down Expand Up @@ -37,7 +37,7 @@ def test_affinity_propagation():

n_clusters_ = len(cluster_centers_indices)

assert_equal(n_clusters, n_clusters_)
assert n_clusters == n_clusters_

af = AffinityPropagation(preference=preference, affinity="precomputed")
labels_precomputed = af.fit(S).labels_
Expand All @@ -50,8 +50,8 @@ def test_affinity_propagation():
cluster_centers_indices = af.cluster_centers_indices_

n_clusters_ = len(cluster_centers_indices)
assert_equal(np.unique(labels).size, n_clusters_)
assert_equal(n_clusters, n_clusters_)
assert np.unique(labels).size == n_clusters_
assert n_clusters == n_clusters_

# Test also with no copy
_, labels_no_copy = affinity_propagation(S, preference=preference,
Expand Down
31 changes: 15 additions & 16 deletions sklearn/cluster/tests/test_bicluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from sklearn.model_selection import ParameterGrid

from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_almost_equal
from sklearn.utils.testing import assert_array_equal
from sklearn.utils.testing import assert_array_almost_equal
Expand Down Expand Up @@ -58,8 +57,8 @@ def _test_shape_indices(model):
for i in range(model.n_clusters):
m, n = model.get_shape(i)
i_ind, j_ind = model.get_indices(i)
assert_equal(len(i_ind), m)
assert_equal(len(j_ind), n)
assert len(i_ind) == m
assert len(j_ind) == n


def test_spectral_coclustering():
Expand All @@ -82,11 +81,11 @@ def test_spectral_coclustering():
**kwargs)
model.fit(mat)

assert_equal(model.rows_.shape, (3, 30))
assert model.rows_.shape == (3, 30)
assert_array_equal(model.rows_.sum(axis=0), np.ones(30))
assert_array_equal(model.columns_.sum(axis=0), np.ones(30))
assert_equal(consensus_score(model.biclusters_,
(rows, cols)), 1)
assert consensus_score(model.biclusters_,
(rows, cols)) == 1

_test_shape_indices(model)

Expand Down Expand Up @@ -120,14 +119,14 @@ def test_spectral_biclustering():
else:
model.fit(mat)

assert_equal(model.rows_.shape, (9, 30))
assert_equal(model.columns_.shape, (9, 30))
assert model.rows_.shape == (9, 30)
assert model.columns_.shape == (9, 30)
assert_array_equal(model.rows_.sum(axis=0),
np.repeat(3, 30))
assert_array_equal(model.columns_.sum(axis=0),
np.repeat(3, 30))
assert_equal(consensus_score(model.biclusters_,
(rows, cols)), 1)
assert consensus_score(model.biclusters_,
(rows, cols)) == 1

_test_shape_indices(model)

Expand Down Expand Up @@ -216,20 +215,20 @@ def test_perfect_checkerboard():
S, rows, cols = make_checkerboard((30, 30), 3, noise=0,
random_state=0)
model.fit(S)
assert_equal(consensus_score(model.biclusters_,
(rows, cols)), 1)
assert consensus_score(model.biclusters_,
(rows, cols)) == 1

S, rows, cols = make_checkerboard((40, 30), 3, noise=0,
random_state=0)
model.fit(S)
assert_equal(consensus_score(model.biclusters_,
(rows, cols)), 1)
assert consensus_score(model.biclusters_,
(rows, cols)) == 1

S, rows, cols = make_checkerboard((30, 40), 3, noise=0,
random_state=0)
model.fit(S)
assert_equal(consensus_score(model.biclusters_,
(rows, cols)), 1)
assert consensus_score(model.biclusters_,
(rows, cols)) == 1


def test_errors():
Expand Down
15 changes: 6 additions & 9 deletions sklearn/cluster/tests/test_birch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
from sklearn.linear_model import ElasticNet
from sklearn.metrics import pairwise_distances_argmin, v_measure_score

from sklearn.utils.testing import assert_greater_equal
from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_greater
from sklearn.utils.testing import assert_almost_equal
from sklearn.utils.testing import assert_array_equal
from sklearn.utils.testing import assert_array_almost_equal
Expand All @@ -31,8 +28,8 @@ def test_n_samples_leaves_roots():
n_samples_root = sum([sc.n_samples_ for sc in brc.root_.subclusters_])
n_samples_leaves = sum([sc.n_samples_ for leaf in brc._get_leaves()
for sc in leaf.subclusters_])
assert_equal(n_samples_leaves, X.shape[0])
assert_equal(n_samples_root, X.shape[0])
assert n_samples_leaves == X.shape[0]
assert n_samples_root == X.shape[0]


def test_partial_fit():
Expand Down Expand Up @@ -76,8 +73,8 @@ def test_n_clusters():
X, y = make_blobs(n_samples=100, centers=10)
brc1 = Birch(n_clusters=10)
brc1.fit(X)
assert_greater(len(brc1.subcluster_centers_), 10)
assert_equal(len(np.unique(brc1.labels_)), 10)
assert len(brc1.subcluster_centers_) > 10
assert len(np.unique(brc1.labels_)) == 10

# Test that n_clusters = Agglomerative Clustering gives
# the same results.
Expand Down Expand Up @@ -114,7 +111,7 @@ def test_sparse_X():

def check_branching_factor(node, branching_factor):
subclusters = node.subclusters_
assert_greater_equal(branching_factor, len(subclusters))
assert branching_factor >= len(subclusters)
for cluster in subclusters:
if cluster.child_:
check_branching_factor(cluster.child_, branching_factor)
Expand Down Expand Up @@ -146,7 +143,7 @@ def check_threshold(birch_instance, threshold):
while current_leaf:
subclusters = current_leaf.subclusters_
for sc in subclusters:
assert_greater_equal(threshold, sc.radius)
assert threshold >= sc.radius
current_leaf = current_leaf.next_leaf_


Expand Down
41 changes: 19 additions & 22 deletions sklearn/cluster/tests/test_dbscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@

import pytest

from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_array_equal
from sklearn.utils.testing import assert_raises
from sklearn.utils.testing import assert_in
from sklearn.utils.testing import assert_not_in
from sklearn.neighbors import NearestNeighbors
from sklearn.cluster.dbscan_ import DBSCAN
from sklearn.cluster.dbscan_ import dbscan
Expand All @@ -41,13 +38,13 @@ def test_dbscan_similarity():
# number of clusters, ignoring noise if present
n_clusters_1 = len(set(labels)) - (1 if -1 in labels else 0)

assert_equal(n_clusters_1, n_clusters)
assert n_clusters_1 == n_clusters

db = DBSCAN(metric="precomputed", eps=eps, min_samples=min_samples)
labels = db.fit(D).labels_

n_clusters_2 = len(set(labels)) - int(-1 in labels)
assert_equal(n_clusters_2, n_clusters)
assert n_clusters_2 == n_clusters


def test_dbscan_feature():
Expand All @@ -64,13 +61,13 @@ def test_dbscan_feature():

# number of clusters, ignoring noise if present
n_clusters_1 = len(set(labels)) - int(-1 in labels)
assert_equal(n_clusters_1, n_clusters)
assert n_clusters_1 == n_clusters

db = DBSCAN(metric=metric, eps=eps, min_samples=min_samples)
labels = db.fit(X).labels_

n_clusters_2 = len(set(labels)) - int(-1 in labels)
assert_equal(n_clusters_2, n_clusters)
assert n_clusters_2 == n_clusters


def test_dbscan_sparse():
Expand Down Expand Up @@ -123,7 +120,7 @@ def test_dbscan_no_core_samples():
db = DBSCAN(min_samples=6).fit(X_)
assert_array_equal(db.components_, np.empty((0, X_.shape[1])))
assert_array_equal(db.labels_, -1)
assert_equal(db.core_sample_indices_.shape, (0,))
assert db.core_sample_indices_.shape == (0,)


def test_dbscan_callable():
Expand All @@ -142,14 +139,14 @@ def test_dbscan_callable():

# number of clusters, ignoring noise if present
n_clusters_1 = len(set(labels)) - int(-1 in labels)
assert_equal(n_clusters_1, n_clusters)
assert n_clusters_1 == n_clusters

db = DBSCAN(metric=metric, eps=eps, min_samples=min_samples,
algorithm='ball_tree')
labels = db.fit(X).labels_

n_clusters_2 = len(set(labels)) - int(-1 in labels)
assert_equal(n_clusters_2, n_clusters)
assert n_clusters_2 == n_clusters


def test_dbscan_metric_params():
Expand Down Expand Up @@ -191,32 +188,32 @@ def test_dbscan_balltree():

# number of clusters, ignoring noise if present
n_clusters_1 = len(set(labels)) - int(-1 in labels)
assert_equal(n_clusters_1, n_clusters)
assert n_clusters_1 == n_clusters

db = DBSCAN(p=2.0, eps=eps, min_samples=min_samples, algorithm='ball_tree')
labels = db.fit(X).labels_

n_clusters_2 = len(set(labels)) - int(-1 in labels)
assert_equal(n_clusters_2, n_clusters)
assert n_clusters_2 == n_clusters

db = DBSCAN(p=2.0, eps=eps, min_samples=min_samples, algorithm='kd_tree')
labels = db.fit(X).labels_

n_clusters_3 = len(set(labels)) - int(-1 in labels)
assert_equal(n_clusters_3, n_clusters)
assert n_clusters_3 == n_clusters

db = DBSCAN(p=1.0, eps=eps, min_samples=min_samples, algorithm='ball_tree')
labels = db.fit(X).labels_

n_clusters_4 = len(set(labels)) - int(-1 in labels)
assert_equal(n_clusters_4, n_clusters)
assert n_clusters_4 == n_clusters

db = DBSCAN(leaf_size=20, eps=eps, min_samples=min_samples,
algorithm='ball_tree')
labels = db.fit(X).labels_

n_clusters_5 = len(set(labels)) - int(-1 in labels)
assert_equal(n_clusters_5, n_clusters)
assert n_clusters_5 == n_clusters


def test_input_validation():
Expand Down Expand Up @@ -247,18 +244,18 @@ def test_dbscan_badargs():
def test_pickle():
obj = DBSCAN()
s = pickle.dumps(obj)
assert_equal(type(pickle.loads(s)), obj.__class__)
assert type(pickle.loads(s)) == obj.__class__


def test_boundaries():
# ensure min_samples is inclusive of core point
core, _ = dbscan([[0], [1]], eps=2, min_samples=2)
assert_in(0, core)
assert 0 in core
# ensure eps is inclusive of circumference
core, _ = dbscan([[0], [1], [1]], eps=1, min_samples=2)
assert_in(0, core)
assert 0 in core
core, _ = dbscan([[0], [1], [1]], eps=.99, min_samples=2)
assert_not_in(0, core)
assert 0 not in core


def test_weighted_dbscan():
Expand Down Expand Up @@ -293,7 +290,7 @@ def test_weighted_dbscan():
rng = np.random.RandomState(42)
sample_weight = rng.randint(0, 5, X.shape[0])
core1, label1 = dbscan(X, sample_weight=sample_weight)
assert_equal(len(label1), len(X))
assert len(label1) == len(X)

X_repeated = np.repeat(X, sample_weight, axis=0)
core_repeated, label_repeated = dbscan(X_repeated)
Expand Down Expand Up @@ -364,11 +361,11 @@ def test_dbscan_precomputed_metric_with_degenerate_input_arrays():
# more details
X = np.eye(10)
labels = DBSCAN(eps=0.5, metric='precomputed').fit(X).labels_
assert_equal(len(set(labels)), 1)
assert len(set(labels)) == 1

X = np.zeros((10, 10))
labels = DBSCAN(eps=0.5, metric='precomputed').fit(X).labels_
assert_equal(len(set(labels)), 1)
assert len(set(labels)) == 1


def test_dbscan_precomputed_metric_with_initial_rows_zero():
Expand Down
11 changes: 5 additions & 6 deletions sklearn/cluster/tests/test_hierarchical.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

from sklearn.metrics.cluster.supervised import adjusted_rand_score
from sklearn.utils.testing import assert_raises
from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_almost_equal
from sklearn.utils.testing import assert_array_almost_equal
from sklearn.utils.testing import assert_raise_message
Expand Down Expand Up @@ -93,7 +92,7 @@ def test_unstructured_linkage_tree():
children, n_nodes, n_leaves, parent = assert_warns(
UserWarning, ward_tree, this_X.T, n_clusters=10)
n_nodes = 2 * X.shape[1] - 1
assert_equal(len(children) + n_leaves, n_nodes)
assert len(children) + n_leaves == n_nodes

for tree_builder in _TREE_BUILDERS.values():
for this_X in (X, X[0]):
Expand All @@ -102,7 +101,7 @@ def test_unstructured_linkage_tree():
UserWarning, tree_builder, this_X.T, n_clusters=10)

n_nodes = 2 * X.shape[1] - 1
assert_equal(len(children) + n_leaves, n_nodes)
assert len(children) + n_leaves == n_nodes


def test_height_linkage_tree():
Expand Down Expand Up @@ -538,7 +537,7 @@ def test_compute_full_tree():
agc.fit(X)
n_samples = X.shape[0]
n_nodes = agc.children_.shape[0]
assert_equal(n_nodes, n_samples - 1)
assert n_nodes == n_samples - 1

# When n_clusters is large, greater than max of 100 and 0.02 * n_samples.
# we should stop when there are n_clusters.
Expand All @@ -550,7 +549,7 @@ def test_compute_full_tree():
agc.fit(X)
n_samples = X.shape[0]
n_nodes = agc.children_.shape[0]
assert_equal(n_nodes, n_samples - n_clusters)
assert n_nodes == n_samples - n_clusters


def test_n_components():
Expand All @@ -562,7 +561,7 @@ def test_n_components():
connectivity = np.eye(5)

for linkage_func in _TREE_BUILDERS.values():
assert_equal(ignore_warnings(linkage_func)(X, connectivity)[1], 5)
assert ignore_warnings(linkage_func)(X, connectivity)[1] == 5


def test_agg_n_clusters():
Expand Down
Loading