PyCharm Community 3.1.1 and Numpy, “'matrix

2019-07-15 18:12发布

问题:

I have the following code:

import numpy as np

if __name__ == "__main__":
    m = np.matrix([[1, 0, 0],
                   [0, 1, 0],
                   [0, 0, 1]])
    print(m)

The code runs as expected, but PyCharm seems to think that 'matrix' is not callable. See screenshot.

Since the code runs, clearly 'matrix' is callable. So what's PyCharm complaining about? Am I in the wrong here or is PyCharm? How do I suppress this error?

回答1:

A simple workaround, at least until the bug is fixed, is to use np.mat(...) instead of np.matrix(...).

However, note that np.mat will avoid making copies if the input is already a matrix and so you can't use it to do things like make defensive copies.