[MRG + 1] Remove unused variables#12230
Conversation
|
This pull request introduces 2 alerts and fixes 22 when merging 7ad074d into e1c3c22 - view on LGTM.com new alerts:
fixed alerts:
Comment posted by LGTM.com |
|
|
||
| # Compute the label assignment on the init dataset | ||
| batch_inertia, centers_squared_diff = _mini_batch_step( | ||
| _mini_batch_step( |
There was a problem hiding this comment.
I mentioned this in the previous issue: is n_iter ever set to 0 for k-means, or is it strictly greater than 0? Because I would see how this assignment might be useful if the subsequent for loop does not assignment them.
There was a problem hiding this comment.
It looks contingent on n_samples, self. max_iter and self. batch_size
scikit-learn/sklearn/cluster/k_means_.py
Lines 1524 to 1525 in 2e2e69d
So it appears very unlikely that n_iter would ever be set to 0.
There was a problem hiding this comment.
Even if n_iter=0, the variables batch_inertia, centers_squared_diff don't seem to be used afterwards, so it should be indeed fine.
| n_samples = 1 | ||
| n_features = X.size | ||
| else: | ||
| n_samples, n_features = X.shape |
There was a problem hiding this comment.
If n_samples is not needed, then it's also not needed here.
| # (and less optimal) | ||
| all_best_covariances = np.zeros((n_best_tot, n_features, | ||
| n_features)) | ||
| n_best_tot = 10 |
There was a problem hiding this comment.
Doesn't this look odd? The comment says it's trying a smaller matrix, but it sets the n_best_tot after retrying the matrix allocation. It seems to me that those lines are swapped somehow.
There was a problem hiding this comment.
Good point. I'll try swapping the lines and see if anything breaks.
| "sparse input is not supported if this is not" | ||
| " the case." % name) | ||
| raise | ||
| except Exception as e: |
There was a problem hiding this comment.
Shouldn't it print the exception instead of ignoring it? It kinda feels odd to catch Exception and suppress it completely.
There was a problem hiding this comment.
There's a raise statement after the print statement below, so the exception will still be visible. It's still strange semantics that the exception does not wrap the print message though.
There was a problem hiding this comment.
There's a raise, sure. But it says that the estimator is not failing properly on sparse data, i.e. the message is being very specific, whereas it's catching Exception. So if the estimator fails because there's not enough memory, or because there's something wrong with the disk and the estimator reads from the disk while fitting, for whatever reason, we still show the same error message. It's just a bad idea to catch Exception, and pretend it's much more specific than Exception.
There was a problem hiding this comment.
Agreed that it's bad that except Exception can be catching anything (minus a TypeError or ValueError from above) while a specific print message is being shown (should probably be a warning).
I am not too familiar with this part of the codebase, but the scope of this PR (just removing unused variables) does not change the prior behavior. This block can be improved in a future PR.
In [5]: try:
...: 1 / 0
...: except Exception as e:
...: print('Something went wrong')
...: raise
...:
...:
Something went wrong
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-5-b91504a88660> in <module>()
1 try:
----> 2 1 / 0
3 except Exception as e:
4 print('Something went wrong')
5 raise
ZeroDivisionError: division by zero
In [6]: try:
...: 1 / 0
...: except Exception:
...: print('Something went wrong')
...: raise
...:
...:
Something went wrong
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-6-ee8d63f9937e> in <module>()
1 try:
----> 2 1 / 0
3 except Exception:
4 print('Something went wrong')
5 raise
ZeroDivisionError: division by zero
There was a problem hiding this comment.
@mroeschke , sorry, hadn't noticed the raise after the print. It's all good. Thanks!
| Returns the transformer. | ||
| """ | ||
| X = check_array(X, accept_sparse='csr') | ||
| check_array(X, accept_sparse='csr') |
There was a problem hiding this comment.
I was like "this definitely doesn't look right" but it actually surprisingly is. With estimator tags we could entirely remove this line.
There was a problem hiding this comment.
Not familiar with estimator tags, but are the tags in place so this line can be removed or should this be kept for now to still validate X?
There was a problem hiding this comment.
should be kept for now, sorry for being cryptic.
|
This pull request introduces 2 alerts and fixes 22 when merging 60f5be5 into 59b15c5 - view on LGTM.com new alerts:
fixed alerts:
Comment posted by LGTM.com |
|
This pull request introduces 2 alerts and fixes 22 when merging 9352f3f into bfab306 - view on LGTM.com new alerts:
fixed alerts:
Comment posted by LGTM.com |
|
Is the codecov check necessary to pass although this is just cleaning some variables? |
|
probably a false positive, I wouldn't worry about it. |
|
This pull request introduces 2 alerts and fixes 20 when merging 067247b into d4c9e84 - view on LGTM.com new alerts:
fixed alerts:
Comment posted by LGTM.com |
|
Please remove the now-unused imports |
|
This pull request fixes 20 alerts when merging ab1dc98 into 5bcd84b - view on LGTM.com fixed alerts:
Comment posted by LGTM.com |
|
Removed those unused imports @jnothman. Thanks |
|
|
||
| # Compute the label assignment on the init dataset | ||
| batch_inertia, centers_squared_diff = _mini_batch_step( | ||
| _mini_batch_step( |
There was a problem hiding this comment.
Even if n_iter=0, the variables batch_inertia, centers_squared_diff don't seem to be used afterwards, so it should be indeed fine.
|
|
||
| itn = 0 | ||
| istop = 0 | ||
| nstop = 0 |
There was a problem hiding this comment.
It's a backport from scipy, it's better to keep it without change, which will make comparing with the upstream version easier.
|
This pull request fixes 19 alerts when merging d9de4c0 into 5bcd84b - view on LGTM.com fixed alerts:
Comment posted by LGTM.com |
|
Looks good, thanks @mroeschke! |
This reverts commit 294884c.
This reverts commit 294884c.
Reference Issues/PRs
What does this implement/fix? Explain your changes.
Removes unused variable assignments as flagged by PyCharm.