3 ms·
But I don't think this will work on numpy matrix.
by maple3142 6y ago
But I don't think this will work on numpy matrix.
- adamjb 6y agoShould it? >>> import numpy as np >>> np.array("asdgh")**2 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: ufunc 'square' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
- maple3142 6y agoMatrix, not array. >>> import numpy as np >>> np.mat(((1,0),(0,1)))**2 matrix([[1, 0], [0, 1]])
- mkl 6y agoThat's a particularly confusing example to choose, because matrix squaring produces the same result as array (element-wise) squaring: >>> np.array([[1, 0], [0, 1]])**2 array([[1, 0], [0, 1]]) Here: >>> np.array([[1, 2], [3, 4]])**2 array([[ 1, 4], [ 9, 16]]) >>> np.mat([[1, 2], [3, 4]])**2 matrix([[ 7, 10], [15, 22]]) The real problem with the np.array("asdgh")**2 example is squaring doesn't make sense for character data. Matrix vs. array squaring has other issues too, as matrix squaring only works with square matrices: >>> np.mat([1, 2])**2 [...] LinAlgError: Last 2 dimensions of the array must be square >>> np.array([1, 2])**2 array([1, 4])