Skip to content

Fixes #30400: update index finding with np.where(condition) #31115

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 4 commits into from
Apr 11, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def plot_species_distribution(
Z = np.ones((data.Ny, data.Nx), dtype=np.float64)

# We'll predict only for the land points.
idx = np.where(land_reference > -9999)
idx = (land_reference > -9999).nonzero()
coverages_land = data.coverages[:, idx[0], idx[1]].T

pred = clf.decision_function((coverages_land - mean) / std)
Expand Down
2 changes: 1 addition & 1 deletion examples/applications/plot_stock_market.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
)

# Plot the edges
start_idx, end_idx = np.where(non_zero)
start_idx, end_idx = non_zero.nonzero()
# a sequence of (*line0*, *line1*, *line2*), where::
# linen = (x0, y0), (x1, y1), ... (xm, ym)
segments = [
Expand Down
2 changes: 1 addition & 1 deletion examples/bicluster/plot_bicluster_newsgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def bicluster_ncut(i):

# words
out_of_cluster_docs = cocluster.row_labels_ != cluster
out_of_cluster_docs = np.where(out_of_cluster_docs)[0]
out_of_cluster_docs = out_of_cluster_docs.nonzero()[0]
word_col = X[:, cluster_words]
word_scores = np.array(
word_col[cluster_docs, :].sum(axis=0)
Expand Down
2 changes: 1 addition & 1 deletion examples/cluster/plot_hdbscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def plot(X, labels, probabilities=None, parameters=None, ground_truth=False, ax=
# Black used for noise.
col = [0, 0, 0, 1]

class_index = np.where(labels == k)[0]
class_index = (labels == k).nonzero()[0]
for ci in class_index:
ax.plot(
X[ci, 0],
Expand Down
2 changes: 1 addition & 1 deletion examples/decomposition/plot_sparse_coding.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def ricker_matrix(width, resolution, n_components):
dictionary=D, transform_algorithm="threshold", transform_alpha=20
)
x = coder.transform(y.reshape(1, -1))
_, idx = np.where(x != 0)
_, idx = (x != 0).nonzero()
x[0, idx], _, _, _ = np.linalg.lstsq(D[idx, :].T, y, rcond=None)
x = np.ravel(np.dot(x, D))
squared_error = np.sum((y - x) ** 2)
Expand Down
2 changes: 1 addition & 1 deletion examples/ensemble/plot_adaboost_twoclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

# Plot the training points
for i, n, c in zip(range(2), class_names, plot_colors):
idx = np.where(y == i)
idx = (y == i).nonzero()
plt.scatter(
X[idx, 0],
X[idx, 1],
Expand Down
2 changes: 1 addition & 1 deletion examples/linear_model/plot_sgd_iris.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

# Plot also the training points
for i, color in zip(clf.classes_, colors):
idx = np.where(y == i)
idx = (y == i).nonzero()
plt.scatter(
X[idx, 0],
X[idx, 1],
Expand Down
2 changes: 1 addition & 1 deletion examples/manifold/plot_mds.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
plt.legend(scatterpoints=1, loc="best", shadow=False)

# Plot the edges
start_idx, end_idx = np.where(X_mds)
start_idx, end_idx = X_mds.nonzero()
# a sequence of (*line0*, *line1*, *line2*), where::
# linen = (x0, y0), (x1, y1), ... (xm, ym)
segments = [
Expand Down
4 changes: 2 additions & 2 deletions examples/miscellaneous/plot_multilabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def plot_subfigure(X, Y, subplot, title, transform):
plt.subplot(2, 2, subplot)
plt.title(title)

zero_class = np.where(Y[:, 0])
one_class = np.where(Y[:, 1])
zero_class = (Y[:, 0]).nonzero()
one_class = (Y[:, 1]).nonzero()
plt.scatter(X[:, 0], X[:, 1], s=40, c="gray", edgecolors=(0, 0, 0))
plt.scatter(
X[zero_class, 0],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
sub.axis("off")

# labeling 5 points, remote from labeled set
(delete_index,) = np.where(unlabeled_indices == image_index)
(delete_index,) = (unlabeled_indices == image_index).nonzero()
delete_indices = np.concatenate((delete_indices, delete_index))

unlabeled_indices = np.delete(unlabeled_indices, delete_indices)
Expand Down
4 changes: 2 additions & 2 deletions examples/semi_supervised/plot_label_propagation_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
# when the label was unknown.
output_labels = label_spread.transduction_
output_label_array = np.asarray(output_labels)
outer_numbers = np.where(output_label_array == outer)[0]
inner_numbers = np.where(output_label_array == inner)[0]
outer_numbers = (output_label_array == outer).nonzero()[0]
inner_numbers = (output_label_array == inner).nonzero()[0]

plt.figure(figsize=(4, 4))
plt.scatter(
Expand Down
2 changes: 1 addition & 1 deletion examples/svm/plot_linearsvc_support_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
# decision_function = np.dot(X, clf.coef_[0]) + clf.intercept_[0]
# The support vectors are the samples that lie within the margin
# boundaries, whose size is conventionally constrained to 1
support_vector_indices = np.where(np.abs(decision_function) <= 1 + 1e-15)[0]
support_vector_indices = (np.abs(decision_function) <= 1 + 1e-15).nonzero()[0]
support_vectors = X[support_vector_indices]

plt.subplot(1, 2, i + 1)
Expand Down
2 changes: 1 addition & 1 deletion examples/tree/plot_iris_dtc.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

# Plot the training points
for i, color in zip(range(n_classes), plot_colors):
idx = np.where(y == i)
idx = np.asarray(y == i).nonzero()
plt.scatter(
X[idx, 0],
X[idx, 1],
Expand Down