Skip to content

Commit

Permalink
Correct the deprecation of the random_integers numpy function. (sciki…
Browse files Browse the repository at this point in the history
  • Loading branch information
tguillemot authored and TomDLT committed Apr 26, 2016
1 parent 427179b commit 78a6748
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 27 deletions.
2 changes: 1 addition & 1 deletion benchmarks/bench_plot_fastkmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def compute_bench(samples_range, features_range):
print('Iteration %03d of %03d' % (it, max_it))
print('==============================')
print()
data = nr.random_integers(-50, 50, (n_samples, n_features))
data = nr.randint(-50, 51, (n_samples, n_features))

print('K-Means')
tstart = time()
Expand Down
9 changes: 4 additions & 5 deletions examples/cluster/plot_adjusted_for_chance_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,17 @@ def uniform_labelings_scores(score_func, n_samples, n_clusters_range,
When fixed_n_classes is not None the first labeling is considered a ground
truth class assignment with fixed number of classes.
"""
random_labels = np.random.RandomState(seed).random_integers
random_labels = np.random.RandomState(seed).randint
scores = np.zeros((len(n_clusters_range), n_runs))

if fixed_n_classes is not None:
labels_a = random_labels(low=0, high=fixed_n_classes - 1,
size=n_samples)
labels_a = random_labels(low=0, high=fixed_n_classes, size=n_samples)

for i, k in enumerate(n_clusters_range):
for j in range(n_runs):
if fixed_n_classes is None:
labels_a = random_labels(low=0, high=k - 1, size=n_samples)
labels_b = random_labels(low=0, high=k - 1, size=n_samples)
labels_a = random_labels(low=0, high=k, size=n_samples)
labels_b = random_labels(low=0, high=k, size=n_samples)
scores[i, j] = score_func(labels_a, labels_b)
return scores

Expand Down
10 changes: 4 additions & 6 deletions sklearn/cluster/k_means_.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,7 @@ def _init_centroids(X, k, init, random_state=None, x_squared_norms=None,
"Setting it to 3*k" % (init_size, k),
RuntimeWarning, stacklevel=2)
init_size = 3 * k
init_indices = random_state.random_integers(
0, n_samples - 1, init_size)
init_indices = random_state.randint(0, n_samples, init_size)
X = X[init_indices]
x_squared_norms = x_squared_norms[init_indices]
n_samples = X.shape[0]
Expand Down Expand Up @@ -1275,8 +1274,7 @@ def fit(self, X, y=None):
init_size = n_samples
self.init_size_ = init_size

validation_indices = random_state.random_integers(
0, n_samples - 1, init_size)
validation_indices = random_state.randint(0, n_samples, init_size)
X_valid = X[validation_indices]
x_squared_norms_valid = x_squared_norms[validation_indices]

Expand Down Expand Up @@ -1324,8 +1322,8 @@ def fit(self, X, y=None):
# criterion
for iteration_idx in range(n_iter):
# Sample a minibatch from the full dataset
minibatch_indices = random_state.random_integers(
0, n_samples - 1, self.batch_size)
minibatch_indices = random_state.randint(
0, n_samples, self.batch_size)

# Perform the actual update step on the minibatch data
batch_inertia, centers_squared_diff = _mini_batch_step(
Expand Down
2 changes: 1 addition & 1 deletion sklearn/cluster/tests/test_spectral.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def test_discretize(seed=8):
for n_samples in [50, 100, 150, 500]:
for n_class in range(2, 10):
# random class labels
y_true = random_state.random_integers(0, n_class, n_samples)
y_true = random_state.randint(0, n_class + 1, n_samples)
y_true = np.array(y_true, np.float)
# noise class assignment matrix
y_indicator = sparse.coo_matrix((np.ones(n_samples),
Expand Down
7 changes: 3 additions & 4 deletions sklearn/linear_model/randomized_l1.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def _resample_model(estimator_func, X, y, scaling=.5, n_resampling=200,
for active_set in Parallel(n_jobs=n_jobs, verbose=verbose,
pre_dispatch=pre_dispatch)(
delayed(estimator_func)(
X, y, weights=scaling * random_state.random_integers(
0, 1, size=(n_features,)),
X, y, weights=scaling * random_state.randint(
0, 2, size=(n_features,)),
mask=(random_state.rand(n_samples) < sample_fraction),
verbose=max(0, verbose - 1),
**params)
Expand Down Expand Up @@ -627,8 +627,7 @@ def lasso_stability_path(X, y, scaling=0.5, random_state=None,
paths = Parallel(n_jobs=n_jobs, verbose=verbose)(
delayed(_lasso_stability_path)(
X, y, mask=rng.rand(n_samples) < sample_fraction,
weights=1. - scaling * rng.random_integers(0, 1,
size=(n_features,)),
weights=1. - scaling * rng.randint(0, 2, size=(n_features,)),
eps=eps)
for k in range(n_resampling))

Expand Down
10 changes: 5 additions & 5 deletions sklearn/metrics/cluster/tests/test_supervised.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ def test_non_consicutive_labels():
def uniform_labelings_scores(score_func, n_samples, k_range, n_runs=10,
seed=42):
# Compute score for random uniform cluster labelings
random_labels = np.random.RandomState(seed).random_integers
random_labels = np.random.RandomState(seed).randint
scores = np.zeros((len(k_range), n_runs))
for i, k in enumerate(k_range):
for j in range(n_runs):
labels_a = random_labels(low=0, high=k - 1, size=n_samples)
labels_b = random_labels(low=0, high=k - 1, size=n_samples)
labels_a = random_labels(low=0, high=k, size=n_samples)
labels_b = random_labels(low=0, high=k, size=n_samples)
scores[i, j] = score_func(labels_a, labels_b)
return scores

Expand Down Expand Up @@ -195,8 +195,8 @@ def test_v_measure_and_mutual_information(seed=36):
# Check relation between v_measure, entropy and mutual information
for i in np.logspace(1, 4, 4).astype(np.int):
random_state = np.random.RandomState(seed)
labels_a, labels_b = random_state.random_integers(0, 10, i),\
random_state.random_integers(0, 10, i)
labels_a, labels_b = random_state.randint(0, 10, i),\
random_state.randint(0, 10, i)
assert_almost_equal(v_measure_score(labels_a, labels_b),
2.0 * mutual_info_score(labels_a, labels_b) /
(entropy(labels_a) + entropy(labels_b)), 0)
Expand Down
2 changes: 1 addition & 1 deletion sklearn/preprocessing/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ def test_standard_scaler_partial_fit_numerical_stability():
# Sparse input
size = (100, 3)
scale = 1e20
X = rng.random_integers(0, 1, size).astype(np.float64) * scale
X = rng.randint(0, 2, size).astype(np.float64) * scale
X_csr = sparse.csr_matrix(X)
X_csc = sparse.csc_matrix(X)

Expand Down
6 changes: 3 additions & 3 deletions sklearn/semi_supervised/label_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
>>> from sklearn.semi_supervised import LabelPropagation
>>> label_prop_model = LabelPropagation()
>>> iris = datasets.load_iris()
>>> random_unlabeled_points = np.where(np.random.random_integers(0, 1,
>>> random_unlabeled_points = np.where(np.random.randint(0, 2,
... size=len(iris.target)))
>>> labels = np.copy(iris.target)
>>> labels[random_unlabeled_points] = -1
Expand Down Expand Up @@ -323,7 +323,7 @@ class LabelPropagation(BaseLabelPropagation):
>>> from sklearn.semi_supervised import LabelPropagation
>>> label_prop_model = LabelPropagation()
>>> iris = datasets.load_iris()
>>> random_unlabeled_points = np.where(np.random.random_integers(0, 1,
>>> random_unlabeled_points = np.where(np.random.randint(0, 2,
... size=len(iris.target)))
>>> labels = np.copy(iris.target)
>>> labels[random_unlabeled_points] = -1
Expand Down Expand Up @@ -417,7 +417,7 @@ class LabelSpreading(BaseLabelPropagation):
>>> from sklearn.semi_supervised import LabelSpreading
>>> label_prop_model = LabelSpreading()
>>> iris = datasets.load_iris()
>>> random_unlabeled_points = np.where(np.random.random_integers(0, 1,
>>> random_unlabeled_points = np.where(np.random.randint(0, 2,
... size=len(iris.target)))
>>> labels = np.copy(iris.target)
>>> labels[random_unlabeled_points] = -1
Expand Down
2 changes: 1 addition & 1 deletion sklearn/utils/tests/test_sparsefuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_incr_mean_variance_axis():
rng = np.random.RandomState(0)
n_features = 50
n_samples = 10
data_chunks = [rng.random_integers(0, 1, size=n_features)
data_chunks = [rng.randint(0, 2, size=n_features)
for i in range(n_samples)]

# default params for incr_mean_variance
Expand Down

0 comments on commit 78a6748

Please sign in to comment.