将函数应用于两个形状相同的 numpy 矩阵
Apply a function to two equally shaped numpy matrices
我正在寻找在两个矩阵之间应用统计函数的最优雅的方法(某种映射/一二一操作)。这是我应用于两个 20 项长分布的函数
stats.pearsonr(data["histograms"][0][15][2], regions_hist[0][15][2])[0]
> 0.42524395175128987
两个矩阵的形状相同
data["histograms"][0].shape
> (16, 3, 20)
regions_hist[0].shape
> (16, 3, 20)
我正在寻找的方法是
Correlations = FancyMapping(data["histograms"][0],regions_hist[0])
Correlations.shape # matrix with the 16*3 correlations between items of both
>(16,3)
你有什么优雅的解决方案吗(整形方面对我的问题很重要)?
您的示例似乎已经在文档中,请参阅 https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.vectorize.html 的底部。所以
import scipy.stats
pearsonr = np.vectorize(scipy.stats.pearsonr, signature='(n),(n)->(),()')
correlations = pearsonr(data["histograms"][0],regions_hist[0])
应该可以:)
我正在寻找在两个矩阵之间应用统计函数的最优雅的方法(某种映射/一二一操作)。这是我应用于两个 20 项长分布的函数
stats.pearsonr(data["histograms"][0][15][2], regions_hist[0][15][2])[0]
> 0.42524395175128987
两个矩阵的形状相同
data["histograms"][0].shape
> (16, 3, 20)
regions_hist[0].shape
> (16, 3, 20)
我正在寻找的方法是
Correlations = FancyMapping(data["histograms"][0],regions_hist[0])
Correlations.shape # matrix with the 16*3 correlations between items of both
>(16,3)
你有什么优雅的解决方案吗(整形方面对我的问题很重要)?
您的示例似乎已经在文档中,请参阅 https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.vectorize.html 的底部。所以
import scipy.stats
pearsonr = np.vectorize(scipy.stats.pearsonr, signature='(n),(n)->(),()')
correlations = pearsonr(data["histograms"][0],regions_hist[0])
应该可以:)