0

I am getting warnings in my Python code running in a Jupyter Notebook, even though I have tried to suppress them using warnings.filterwarnings('ignore'). Specifically, I am getting the warning "FutureWarning: The default value of n_init will change from 10 to 'auto' in 1.4. Set the value of n_init explicitly to suppress the warning" and UserWarning.

How can I suppress these warnings in Jupyter Notebook other than set the value of n_init?

Here is the code I am using:

import warnings
warnings.filterwarnings('ignore')

#parameter selection
bow_mx_copy = bow_mx.toarray().copy()
bow_mx_copy[bow_mx_copy==0] = 1e-6 #set a very small value to avoid zero vector, because Agglomerative Clustering doens't support when use Cosine

dbs_params = {
    "n_clusters": range(2,10),
}
print("K-means, Euclidean Distance")
metric = "euclidean"
gridsearch = GridSearchCV(KMeans(), dbs_params, scoring = 'adjusted_rand_score',\
                          n_jobs=-1).fit(bow_mx_copy[:500,]) #use the first 500 samples for parameter selection
best_estimator = gridsearch.best_estimator_
clustering = best_estimator.fit(bow_mx_copy)
ed_labels = clustering.labels_
score = silhouette_score(bow_mx, clustering.labels_, metric=metric)
print(f"metric={metric}, \n best params={gridsearch.best_params_}, \n  best silhouette_score={score}")

I have also tried the following codes which is not working as well.

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

There is the little part of warnings I received (too long to paste, I removed dumplicates):

 warnings.warn(
/Users/haoren/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning
  warnings.warn(
/Users/haoren/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning
  warnings.warn(
/Users/haoren/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning
  warnings.warn(
/Users/haoren/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/model_selection/_validation.py:778: UserWarning: Scoring failed. The score on this train-test partition for these parameters will be set to nan. Details: 
Traceback (most recent call last):
  File "/Users/haoren/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/model_selection/_validation.py", line 765, in _score
    scores = scorer(estimator, X_test)
TypeError: _BaseScorer.__call__() missing 1 required positional argument: 'y_true'

  warnings.warn(
/Users/haoren/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning
  warnings.warn(
/Users/haoren/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/model_selection/_validation.py:778: UserWarning: Scoring failed. The score on this train-test partition for these parameters will be set to nan. Details: 
Traceback (most recent call last):
  File "/Users/haoren/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/model_selection/_validation.py", line 765, in _score
    scores = scorer(estimator, X_test)
TypeError: _BaseScorer.__call__() missing 1 required positional argument: 'y_true'

I have also tried the following codes which is not working as well.

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

What I don't understand is why python still returns the warning even after I set it to ignore warnings. And why is the warning here so long?

I have checked the similar question "Cannot supress Python Warnings with warnings.filterwarnings("ignore")" but it seems that not working for me. I am sure I run the ignore part before running the cell.

I would be very grateful for any answers and help from you.

Hao Ren
  • 11
  • 4
  • Why not just set n_init=10 explicitly and retain the same value as the default and get rid of those warnings? It would take less time than asking a question. – RK Replogle Apr 08 '23 at 21:21
  • Thank you for your comment! However, rather than hiding these warning messages, I would like to know why they still appear in this case after setting ignore warnings. Perhaps figuring out the reason behind it can avoid similar problems in the future that lead to wrong results (many times these mistakes are not easy to find). – Hao Ren Apr 09 '23 at 04:39

0 Answers0