python - 比较两个数组会引发警告。有什么解决方法吗?

标签 python python-3.x pandas numpy data-science

我有 2 个 np.array() 如下。当我使用“==”比较两者时,我得到一个输出但带有弃用警告。比较具有相同矩阵的 2 个数组时没有警告。

获得相同结果但没有警告的解决方法是什么?

非常感谢!

x = np.array([[0,1,2],[3,4,5]])
x

Out: array([[0, 1, 2],
       [3, 4, 5]])

y = np.array([[6,7],[8,9],[10,11]])
y

Out: array([[ 6,  7],
       [ 8,  9],
       [10, 11]])

x == y

Out: False

**C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
  """Entry point for launching an IPython kernel.**

截图:

最佳答案

这个错误告诉你你正在执行的比较没有意义,因为两个数组有不同的形状,因此它不能执行元素比较:

x==y

DeprecationWarning: elementwise comparison failed; this will raise an error in the future. x==y

正确的方法是使用 np.array_equal ,它检查形状和元素的相等性:

np.array_equal(x,y)
# False

关于python - 比较两个数组会引发警告。有什么解决方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63029166/

相关文章:

python - 在 Python 2.6 中解析 PCAP

python - 使用自定义签名 defs 保存 TF2 keras 模型

python - Makefile 在 Python3 中找不到模块

python-3.x - Asyncios await reader.read() 永远等待

python - Groupby 查找上一行之前的值

python - Pandas :对同一列中的情侣进行分组

python - 在大字符串中搜索文件路径。返回文件路径+文件名

python - 如何在 groupby 中实现类似于 `value_counts()` 的东西?

python - 在 Python3 中使用 %x 格式化不好吗?

python - 使用非空字符串值随机填充列的 NaN 值