- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下几点:
import numpy as np
points = np.array([[49.8, 66.35],
[49.79, 66.35],
[49.79, 66.35],
[44.65, 67.25],
[44.65, 67.25],
[44.65, 67.25],
[44.48, 67.24],
[44.63, 67.21],
[44.68, 67.2],
[49.69, 66.21],
[49.85, 66.17],
[50.51, 66.04],
[49.8, 66.35]])
当我绘制它们时,我得到这个形状:
import matplotlib.pyplot as plt
x = [a[0] for a in points ]
y = [a[1] for a in points ]
plt.plot(x,y)
正如您从点列表中看到的,其中一些是多余的(即查看点 1 和 2(从 0 开始))。
为了只保留非冗余点,我恢复了这个问题的答案: Removing duplicate columns and rows from a NumPy 2D array
def unique_2D(a):
order = np.lexsort(a.T)
a = a[order]
diff = np.diff(a, axis=0)
ui = np.ones(len(a), 'bool')
ui[1:] = (diff != 0).any(axis=1)
return a[ui]
我将此函数应用于我的点,我得到:
non_redundant_points = unique_2D(points)
这是保留点的打印列表:
[[ 50.51 66.04]
[ 49.85 66.17]
[ 49.69 66.21]
[ 49.79 66.35]
[ 49.8 66.35]
[ 44.68 67.2 ]
[ 44.63 67.21]
[ 44.48 67.24]
[ 44.65 67.25]]
但是,现在我面临以下问题:当我绘制它们时,顺序不知何故没有保留......
x_nr = [a[0] for a in non_redundant_points ]
y_nr = [a[1] for a in non_redundant_points ]
plt.plot(x_nr,y_nr)
你知道我该如何解决这个问题吗?
为了更轻松地复制和粘贴,以下是完整代码:
import numpy as np
import matplotlib.pyplot as plt
points = np.array([[49.8, 66.35],
[49.79, 66.35],
[49.79, 66.35],
[44.65, 67.25],
[44.65, 67.25],
[44.65, 67.25],
[44.48, 67.24],
[44.63, 67.21],
[44.68, 67.2],
[49.69, 66.21],
[49.85, 66.17],
[50.51, 66.04],
[49.8, 66.35]])
x = [a[0] for a in points ]
y = [a[1] for a in points ]
plt.plot(x,y)
def unique_2D(a):
order = np.lexsort(a.T)
a = a[order]
diff = np.diff(a, axis=0)
ui = np.ones(len(a), 'bool')
ui[1:] = (diff != 0).any(axis=1)
return a[ui]
x_nr = [a[0] for a in non_redundant_points ]
y_nr = [a[1] for a in non_redundant_points ]
plt.plot(x_nr,y_nr)
最佳答案
您可以使用np.unique
来获取唯一元素,并使用return_index=True
来获取原始数组的索引。然后您可以使用它们对返回的唯一数组进行排序以获得原始索引顺序
points = np.array([[49.8, 66.35],
[49.79, 66.35],
[49.79, 66.35], ... ] # Your original input array
points, idx = np.unique(points, axis=0, return_index=True)
print (idx)
# [ 6 7 3 8 9 1 0 10 11]
arr = points[np.argsort(idx), :]
print (arr)
# [[49.8 66.35]
# [49.79 66.35]
# [44.65 67.25]
# [44.48 67.24]
# [44.63 67.21]
# [44.68 67.2 ]
# [49.69 66.21]
# [49.85 66.17]
# [50.51 66.04]]
绘制它们
plt.plot(arr[:, 0], arr[:, 1])
关于python - 在不丢失顺序的情况下消除冗余的 2D 点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60289064/
我有下面提供的“示例代码”,我觉得这很多余,并且想请您与我一起解释是否有任何更少冗余的方法来处理以下代码。 public interface Bars { FOO1 FOO1 = (FOO1)
我们刚刚测试了一个由 2 个服务器组成的 AppFabric 集群,我们在其中删除了“领导”服务器。第二个服务器对它的任何请求超时并出现错误: Microsoft.ApplicationServer.
我正在设计一个关系数据库 - 底层是 MySQL - DBDesigner 4 . 我有 3 个表:module、page 和 lang。每个模块属于一个页面,每个页面都有特定的语言: 外键 pag
我的 Fraction 程序运行流畅,但 NetBeans IDE 告诉我以下 if 是多余的: public boolean equals(Object other) { Fraction bo
下面的代码显然是多余的,但根据我的经验,我经常使用这种模式。有没有更好的方法在 python 中执行此操作? if re.search("at (\d{1,2}):\d{2}", p): a=
我有一个用具体示例说明的一般性问题。当所有组件对象都已测试时,您建议对复合对象进行多少测试? 作为具体示例,请考虑下面的 NullTerminatedStringReader。它从字节缓冲区中读取一个
if ( a > b) { return true; } return false; 对于上面的代码,Netbeans 给出了 “Redundant if statement” 警告并建议将其更改
当我添加一些约束时,例如: create table Test( IDTest int primary key, Credit int not null constraint Credit
此代码适用于 Microchip 的 PIC32MX 微处理器。他们的编译器本质上是 GCC 3.4。 我倾向于使用 GCC 的 __packed__ attribute将位域打包到一个 union
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我正在为我的应用程序使用apns通知,为此,我将apns设备 token 存储在我的sql数据库中。 问题在于,每次用户删除或安装该应用程序时,它都会生成一个新的设备 token ,并将其存储在数据库
Action Keyname Type Unique Packed Column Cardinality Collation Null Comment Edit Dro
我正在开发一个使用 ARM Cortex-M0 处理器的项目。在这个项目中,我需要提供计时器支持(CMSDK (SSE-200)计时器)。 因此,在 vector 表中,在 TIMER0_IRQn 表
有没有写的理由 corsFilter.setAllowedOrigins(new HashSet(Arrays.asList("*"))); 其中allowedOrigins在ReSTLet框架中的定
我正在创建一个包含 4 个链接的 HTML/CSS 页面, Home.html Details.html ContactMe.html AboutUs.html 我想在所有关联的 HTML 页面中将其
我试图理解并使用其他人编写的代码,但由于我对 typedef 经验不多。 , 我有时会感到困惑。 有两个不同的头文件,一个继承另一个,并且在两个文件上声明相同的typedef。 为什么会出现冗余,如何
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我有一个用于改造的 POJO 类。 public class AppData(){ String a; String b; String c;
我想知道: 编写这段代码: DataRow[] g = new DataRow[1] ; var t=new StringBuilder().AppendFormat("{0}", g[0]["a
只是一个性能问题...... 假设我有 5 个类,每个类都引用了 System.Data 和一个自己开发的库。这 5 个类是类库的一部分,最终将被构建并发布到一些 Web 应用程序作为引用。 通过将引
我是一名优秀的程序员,十分优秀!