- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要将矩阵的对角线元素设置为 Inf。
一个简单的方法是使用np.fill_diagonal
。
np.fill_diagonal(my_matrix, float('inf')
但是,fill_diagonal
会修改输入矩阵,而不是返回填充对角线的新矩阵。这对我不起作用。我需要填充对角线而不修改原始矩阵。
当然,我可以克隆原始矩阵,所以我将始终保留原始矩阵的副本。但是我不太喜欢这个解决方案,因为我会经常更新原始矩阵,因此每次需要对角线为 inf 时我都必须复制它。
是否有一个函数可以执行与 fill_diagonal
相同的操作,但不修改输入矩阵?像这样的东西:
new_matrix = np.fill_diagonal(original_matrix, float('inf')
为什么我需要这个:
我的矩阵是点之间的距离矩阵,我想在每一步计算两个最近的点。当然这个矩阵的对角线是0(因为一个点到它自身的距离是0)。因此,为了确保不采取相同的点,我的解决方案是将对角线设置为 Inf。
但是,一旦找到这两个点,我需要计算这两点与其余点之间的距离的平均值,因此我实际上需要对角线为 0 而不是 Inf。
目前我正在做的是:
计算这两点与其余点之间的平均距离。
# fill diagonal with Inf to avoid taking the diagonals
np.fill_diagonal(data, float('inf'))
# find the minimum distance
idx = np.argmin(data)
# fill the diagonals back to 0
np.fill_diagonal(data, 0.0)
# get the coordinates of the minimum distance
row, col = np.unravel_index(idx,data.shape)
# compute the new node as the average distance between the two points
new_node = np.mean((data[:,row],data[:,col]),0)
# replace the first node (row) with the new node
data[:,row] = new_node
data[row,:] = new_node.T
# delete the second node (col) from the matrix
data = np.delete(data, col, 0) # delete row
data = np.delete(data, col, 1) # delete column
但是我不喜欢将对角线设置为 Inf 然后再设置回 0 的想法,我更愿意将一个函数传递给 argmax ,该函数返回对角线填充 Inf 的数据,而不实际修改矩阵数据。
类似于:
idx = np.argmin(return_filled_diagonals(data, float('Inf'))
# here I can operate with data as usual since it has not been modified.
最佳答案
方法#1
您正在寻找的魔法就在 NumPy strides
中这使我们能够看到没有对角线元素的数组 View ,因此不再占用内存空间。这是获得这样的 View 的实现 -
def nodiag_view(a):
m = a.shape[0]
p,q = a.strides
return np.lib.stride_tricks.as_strided(a[:,1:], (m-1,m), (p+q,q))
让我们看一下示例运行来验证它是否是一个 View -
In [124]: a # Input array
Out[124]:
array([[ 0, 61, 43, 26, 21],
[20, 0, 78, 29, 64],
[34, 49, 0, 64, 60],
[36, 96, 67, 0, 75],
[36, 85, 40, 74, 0]])
# Get the no-diag view
In [125]: a_nodiag = nodiag_view(a)
# Lets's verify by changing elements in the view and that should change
# elements in the original array too
In [126]: a_nodiag[:] = 999
In [127]: a
Out[127]:
array([[ 0, 999, 999, 999, 999],
[999, 0, 999, 999, 999],
[999, 999, 0, 999, 999],
[999, 999, 999, 0, 999],
[999, 999, 999, 999, 0]])
最后,让我们看看如何设置它来解决您的整个问题 -
def argmin_without_diag(a):
a_nodiag = nodiag_view(a)
idx_nodiag = np.argmin(a_nodiag)
m = a.shape[0]
idx = idx_nodiag + np.unravel_index(idx_nodiag, (m-1,m))[0]+1
return np.unravel_index(idx, a.shape)
示例运行 -
In [142]: a
Out[142]:
array([[ 0, 60, 79, 55, 77],
[62, 0, 86, 84, 25],
[32, 96, 0, 74, 89],
[24, 33, 64, 0, 93],
[14, 74, 30, 44, 0]])
In [143]: argmin_without_diag(a)
Out[143]: (4, 0)
<小时/>
方法#2
如果你同时担心内存和性能,可以暂时将对角线设置为infnite
,然后获取argmin
索引,然后放回原始对角线值。因此,实际上输入数组没有改变。实现看起来像这样 -
def argmin_without_diag_replacement(a):
# Store diagonal values
vals = a.ravel()[::a.shape[1]+1].copy()
# Set diag ones as infinites
a.ravel()[::a.shape[1]+1] = np.inf
# Get argmin index
idx = np.argmin(a)
# Put back the original diag values
a.ravel()[::a.shape[1]+1] = vals
return np.unravel_index(idx, a.shape)
因此,对于 (n x n)
形状的数组,临时数组将只有 n
个元素。
示例运行 -
In [237]: a
Out[237]:
array([[ 0., 95., 57., 75., 92.],
[ 37., 0., 69., 71., 62.],
[ 42., 72., 0., 30., 57.],
[ 41., 80., 94., 0., 26.],
[ 36., 45., 71., 76., 0.]])
In [238]: argmin_without_diag_replacement(a)
Out[238]: (3, 4)
运行时测试
In [271]: a = np.random.randint(11,99,(1000,1000)).astype(float)
In [272]: np.fill_diagonal(a,0)
In [273]: %timeit argmin_without_diag(a)
1000 loops, best of 3: 1.76 ms per loop
In [274]: %timeit argmin_without_diag_replacement(a)
1000 loops, best of 3: 688 µs per loop
关于没有 fill_diagonal 的 inf 的 Python 矩阵对角线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43758949/
python 3.7 在编写最大值的搜索代码时,我遇到了负无穷大的奇怪行为。 有人可以解释为什么这种行为吗? >>> inf = float('inf') >>> inf is inf True >>
我是 Julia 的新手 - Windows 上的 1.0.0 版。 documentation陈述如下 julia> Inf / Inf NaN 但是当我执行以下操作时,我得到了不同的结果 juli
我是 Julia 的新手 - Windows 上的 1.0.0 版。 documentation陈述如下 julia> Inf / Inf NaN 但是当我执行以下操作时,我得到了不同的结果 juli
我正在使用用 Objective-C 编写的第三个 CocoaPods 库来截取 UITextView 的屏幕截图。 iOS 8 没问题,但在我更改 iOS 9 和 Swift 2 的语法后,它会抛出
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我已经从 Intellij 为 Spark+ scala 代码创建了 jar 并尝试在开发集群中运行该 jar,但最终出现以下错误: Exception in thread "main" java.l
我有一个包含如下标记的 jsp 文件: 在我的应用中,部署结构是: WEB-INF |-lib |-some.jar |-META-INF
我正在尝试在大型数据集 (5000x300) 上运行 randomForest。不幸的是,我收到如下错误消息: > RF df2 df2 character(0) > df2 df2 numer
我想返回 +INF 但我只返回“inf”: int main(void) { double dubb = HUGE_VAL; printf("%f \n", dubb); } 我是不是
我不明白为什么在以下两种情况下除以 0 会产生不同的结果。amort 是一个计算常量摊销计划的函数。我们现在唯一关心的是 A 的最后一个元素恰好为 0。 amort = @(r,M) ((1+r).^
在 C 中,在使用 IEEE-754 float 的实现中,当我比较两个为 NaN 的 float 时,它返回 0 或“false”。但是为什么两个都为 inf 的 float 相等呢? 这个程序打印
以下代码生成结果-inf。 fn main() { println!("{}", (-10. / 0.)); } 但是,当我尝试下面的代码时,它没有打印出 true,而是给我一个错误。 fn
Numpy 的 log 方法为 log(0) 提供 -inf。这个值是可比较的: >>> np.log(0) == np.log(0) True 现在在单元测试中以下工作正常: self.assert
在下一种情况下哪种方法更好: 我需要获取一些按分数排序的元素,我可以使用这两种方法: 1. zrange myZset 1 5 WITHSCORES 2. zrangebyscore myZset
我正在尝试解释 Web 应用程序的基础知识。我在 META-INF 和 WEB-INF 上遇到了这个问题。这些目录是如何获得这些名称的? 最佳答案 Jar 文件实际上是 ZIP 文件,带有额外的信息和
我正在使用修改后的kanderson-well beats委托版本来修改AVKit需要的请求。所有代码将在帖子的底部。修改请求、创建字幕播放列表并将它们添加到主播放列表中是很好的,因为它们在请求AVM
当您有符合 IEEE754 标准的浮点实现时,与 NaN 的任何比较都是 false,即使是 NaN == NaN,但是+inf == +inf 是 true,为什么? 从我的角度来看,说 +inf
R 中是否有一个函数可以确定值是否为 NA , NaN , Inf , -Inf ,否则不是一个格式良好的数字? 最佳答案 你要is.finite > is.finite(NA) [1] FALSE
我们有一个应用程序,我们正在使用 InstallShield LE(Visual Studio 2010 附带的那个)来管理应用程序的安装。 作为安装的一部分,我需要安装一个 INF 文件,以便应用程
我正在将应用程序移植到 tomcat,我怀疑以下类加载可能是个问题... 如果我在 WEB-INF/classes 中有一个类需要一个在 WEB-INF/lib 中的类,他们找不到它。 如果我在 WE
我是一名优秀的程序员,十分优秀!