Skip to content

Instantly share code, notes, and snippets.

@yunwilliamyu
Created January 2, 2017 19:25
Show Gist options
  • Save yunwilliamyu/b768f63178de7aad8325847580e4740d to your computer and use it in GitHub Desktop.
Save yunwilliamyu/b768f63178de7aad8325847580e4740d to your computer and use it in GitHub Desktop.
Numpy 1.11.0 vs 1.8.2 matrix nonzero fix
import numpy as np
A = np.matrix([[1, 2], [3, 4]])
A[0,:].nonzero() # Returns (matrix([[0, 0]]), matrix([[0, 1]])) if np.__version__ == "1.8.2"
# Returns (array([0, 0]), array([0, 1])) if np.__version__ == "1.11.0"
np.asarray(A[0,:]).nonzero() # Returns (array([0, 0]), array([0, 1])) for both versions, so is backwards compatible
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment