Issue
So using the following example:
import numpy as np
X = np.array([[1,10,np.nan,0],[2,10,np.nan,0],[3,10,np.nan,0]])
I can drop the all-NaN or all-0 columns like so:
X = X[:,~np.all(np.isnan(X), axis=0)]
X = X[:,~np.all(X==0, axis=0)]
I would also like to drop the zero variance columns, is there a good way to do so using the same concise notation?
Thanks!
Solution
You can.
import numpy as np
X[:, np.var(X, axis=0) != 0]
array([[ 1., nan],
[ 2., nan],
[ 3., nan]])
This calculates the variance of each column (axis=0
) and then returns the ones not equal to 0.
Answered By - m13op22
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.