Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 变量相互作用的计算(矩阵中向量的点积)_Python_Numpy_Vector_Dot Product_Numpy Einsum - Fatal编程技术网

Python 变量相互作用的计算(矩阵中向量的点积)

Python 变量相互作用的计算(矩阵中向量的点积),python,numpy,vector,dot-product,numpy-einsum,Python,Numpy,Vector,Dot Product,Numpy Einsum,如果我将一个向量乘以它自身的tansposed,即np.dot(x.T,x),我将得到一个二次型矩阵 如果我有一个矩阵Xmat(k,n),我如何有效地计算行点积并只选择上三角元素 那么,自动取款机。我有以下解决方案: def compute_interaction(x): xx = np.reshape(x, (1, x.size)) return np.concatenate((x, np.dot(xx.T, xx)[np.triu_indices(xx.size)])) 然

如果我将一个向量乘以它自身的tansposed,即np.dot(x.T,x),我将得到一个二次型矩阵

如果我有一个矩阵
Xmat
(k,n),我如何有效地计算行点积并只选择上三角元素

那么,自动取款机。我有以下解决方案:

def compute_interaction(x):
    xx = np.reshape(x, (1, x.size))
    return np.concatenate((x, np.dot(xx.T, xx)[np.triu_indices(xx.size)]))
然后
compute\u交互(np.asarray([2,5]))
yield
array([2,5,4,10,25])

当我有一个矩阵时,我使用

np.apply_along_axis(compute_interaction, axis=1, arr = np.asarray([[2,5], [3,4], [8,9]]))
这就产生了我想要的:

array([[ 2,  5,  4, 10, 25],
       [ 3,  4,  9, 12, 16],
       [ 8,  9, 64, 72, 81]])
除了使用
沿_轴应用
计算外,还有其他方法吗?可能使用
np.einsum

方法#1

具有
np.triu_指数的一种解决方案是-

r,c = np.triu_indices(arr.shape[1])
out = np.concatenate((arr,arr[:,r]*arr[:,c]),axis=1)
方法#2

使用
切片
-

def pairwise_col_mult(a):
    n = a.shape[1]
    N = n*(n+1)//2
    idx = n + np.concatenate(( [0], np.arange(n,0,-1).cumsum() ))
    start, stop = idx[:-1], idx[1:]
    out = np.empty((a.shape[0],n+N),dtype=a.dtype)
    out[:,:n] = a
    for j,i in enumerate(range(n)):
        out[:,start[j]:stop[j]] = a[:,[i]] * a[:,i:]
    return out
时间安排-

In [254]: arr = np.random.randint(0,9,(10000,100))

In [255]: %%timeit
     ...: r,c = np.triu_indices(arr.shape[1])
     ...: out = np.concatenate((arr,arr[:,r]*arr[:,c]),axis=1)
1 loop, best of 3: 577 ms per loop

In [256]: %timeit pairwise_col_mult(arr)
1 loop, best of 3: 233 ms per loop
值得一提的是,
apply_沿_轴
只是:

In [168]: np.array([compute_interaction(row) for row in arr])
Out[168]: 
array([[ 2,  5,  4, 10, 25],
       [ 3,  4,  9, 12, 16],
       [ 8,  9, 64, 72, 81]])

apply…
只是一个方便的工具,可以使多个轴上的迭代更清晰(但不是更快)。

在您的实际用例中,
n
k
的典型值是什么?假设k是数千或数百万,n最多100。这非常一致,谢谢!还在弄清楚为什么会这样:-)
In [168]: np.array([compute_interaction(row) for row in arr])
Out[168]: 
array([[ 2,  5,  4, 10, 25],
       [ 3,  4,  9, 12, 16],
       [ 8,  9, 64, 72, 81]])