import numpy as np
class Matrix(np.ndarray):
def __new__(cls, input_array):
return np.asarray(input_array).view(cls)
def __array_finalize__(self, obj) -> None:
if obj is None: return
# This attribute should be maintained!
default_attributes = {"attr": 1}
self.__dict__.update(default_attributes) # another way to set attributes
def __mul__(self, B):
return self.__class__(self.__array__() @ B.__array__())
__mul__
describes how the class should handle the *
operator with another Matrix B
A = np.arange(9).reshape((3,3))
B = A + 10
AM = Matrix(A)
BM = Matrix(B)
>>> AM
Matrix([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> BM
Matrix([[10, 11, 12],
[13, 14, 15],
[16, 17, 18]])
>>> AM * BM
Matrix([[ 45, 48, 51],
[162, 174, 186],
[279, 300, 321]])
>>> A @ B
array([[ 45, 48, 51],
[162, 174, 186],
[279, 300, 321]])
Hi, this might be a stupid question but
I've got a with shape (313596, 3)
a = array([[0, 1, 0],
[0, 1, 0],
[0, 1, 0],
...,
[1, 1, 0],
[1, 1, 0],
[1, 1, 0]])
and then b with shape (313596, 25, 25, 3)
b = array([[[[0.98823529, 0.98823529, 0.98823529],
[0.98431373, 0.98431373, 0.98431373],
[0.99215686, 0.99215686, 0.99215686],
...,
I want to do [a[i] * b[i] for i in range(313596)]
but with numpy so that the end result is (313596, 25, 25, 3)
I'm not sure how I can do this, I tried using dot
and multiply
but they didn't work, I somehow need to do do something like a dot product treating each of these sub (25,25,3)
matrices as elements and then each multiplication in the dot product is a hadamard
np.dot(np.transpose(a), b) #-> shapes (3,313596) and (313596,25,25,3) not aligned: 313596 (dim 1) != 25 (dim 2)
np.matmul
Please can somebody help me out here, I've seen tensordot
and einsum
, not sure if they're my answer... I'm getting a bit frustrated trying to figure this out, maybe somebody could point me in the right direction?
In [28]: a = np.random.randn(313, 3)
In [29]: b = np.random.randn(313, 25, 25, 3)
In [30]: expected = np.stack([a[i] * b[i] for i in range(313)])
In [31]: expected.shape
Out[31]: (313, 25, 25, 3)
In [32]: actual = a[:, None, None, :] * b
In [33]: np.all(actual == expected)
Out[33]: True
In [34]: %timeit expected = np.stack([a[i] * b[i] for i in range(313)])
10.1 ms ± 325 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [35]: %timeit actual = a[:, None, None, :] * b
4.44 ms ± 182 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
import numpy as np
def test__without_fun_call__bool_indexing():
a = np.array([[1., 2.], [3., 4.]])
lines_to_zero = np.array([True, False])
a[lines_to_zero, :] = 0.
assert np.allclose(a, [[0., 0.], [3., 4.]]) # OK
def set_to_zero(x):
x[...] = 0.
def test__with_fun_call__bool_indexing():
a = np.array([[1., 2.], [3., 4.]])
lines_to_zero = np.array([True, False])
set_to_zero(a[lines_to_zero, :])
assert np.allclose(a, [[0., 0.], [3., 4.]]) # FAIL
def test__with_fun_call__slice_indexing():
a = np.array([[1., 2.], [3., 4.]])
set_to_zero(a[:1, :])
assert np.allclose(a, [[0., 0.], [3., 4.]]) # OK
np.dot
.... the far I can go is here https://github.com/numpy/numpy/blob/872373b030eb6c181ab7f29c0928ccd8b0f74528/numpy/core/multiarray.py#L732 ... from here how can I find the c source file that correspond? ....either pointing out the file that correspond or explain how @array_function_from_c_func_and_dispatcher(_multiarray_umath.dot)
mechanism work would be appreciated...
np.dot
-> np.dot.__wrapped__
-> <builtin function dot> -> _multiarray_umath
array_module_methods
-> array_matrix_product
-> PyArray_MatrixProduct2
. From there it depends on the shape of the arguments.
@
is np.matmul, not np.dot. They have different semantics for ndim>2