gpt4 book ai didi

python - Matplotlib 干扰 NumPy(在 Windows 上)

转载 作者:行者123 更新时间:2023-12-03 15:16:37 25 4
gpt4 key购买 nike

下面描述的问题已被复制

说你有以下

import numpy as np
import matplotlib.pyplot as plt

x = np.random.randint(5, size=(100, 12), dtype=np.int64)
# [THERE IS ACTUALLY NO NEED TO SET THE DATA TYPE
# `x = np.random.rand(100, 12)` yields the same problem]
并且你想计算 x的排名。
>>> np.linalg.matrix_rank(x)
12
一切安好。让我们重新启动一个 从头开始新 session ,这次的底层代码是
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(1010) # <-----
x = np.random.randint(5, size=(100, 12), dtype=np.int64)
x_vals = y_vals = np.arange(0, .5, .05)

plt.plot(x_vals, y_vals, linestyle='--')

print(np.linalg.matrix_rank(x))
这打印 0 (!!)。更令人惊讶的是,这背后的原因是赋予 linestyle 的值。 (!!)。我的意思是,有 linestyle='-' (固体)使一切恢复正常。
这显然是一种不受欢迎的行为(我确实花了几个小时来精确定位)......但仍然:
如何 ?

这发生在使用 Python3.7.3 的 Windows 10 下
numpy==1.19.2      # since 1.19.0 actually
matplotlib==3.3.3 # between 3.1.3 and 3.3.3 for what I can tell
Linux下没问题(同环境)

其他精度
  • 并非所有 x 都会发生这种情况的形状。很难说清楚。然而,这也不是随机的,并且单调地链接到 x的(垂直和/或水平)形状。
  • 这个问题是转置不变的。
  • x与之前的自身相比完全相同 plt.plot被调用(使用 joblib.hash 进行比较)

  • enter image description here

    这个问题更多的是留下痕迹而不是得到答案。这是 好奇怪那个 我不得不把它写在某个地方 .我换了 linestyle ...
    问题的标题足够明确,可以让人们在这里遇到与我相同的问题。

    另一个屏幕截图:
    enter image description here
    GIF 的代码如下。
    import numpy as np
    import matplotlib.pyplot as plt
    import joblib as jl

    linestyles = [
    'solid', '-',
    'dotted', # '.', => ValueError: '.'
    'dashed', '--',
    'dashdot', '-.',
    ':', '', ' '
    ]

    for ls in linestyles:
    print(26*'*', f"linestyle='{ls}'")

    np.random.seed(1010)
    x = np.random.rand(9, 5)
    h0 = jl.hash(x)

    x_vals = y_vals = np.arange(0, .5, .05)
    plt.plot(x_vals, y_vals, linestyle=ls)
    # plt.show()

    h1 = jl.hash(x)
    mr = np.linalg.matrix_rank(x)
    print(
    '\t', mr, (not mr)*'<---------------[!!!]'
    )
    print('\t', 'Has not changed:', h0 == h1)

    最佳答案

    调查
    正如 @user2357112supportsMonica 所指出的,故事最深的纤维与 numpy.linalg.svd 有关,由于某些原因未能收敛。

    import numpy as np
    import matplotlib.pyplot as plt
    import joblib as jl

    linestyles = [
    ('-.', '-.'),
    (':', ':'),
    ('solid', 'solid'), # Same as (0, ()) or '-'
    ('dotted', 'dotted'), # Same as (0, (1, 1)) or '.'
    ('dashed', 'dashed'), # Same as '--'
    ('dashdot', 'dashdot'),

    ('loosely dotted', (0, (1, 10))),
    ('dotted', (0, (1, 1))),
    ('densely dotted', (0, (1, 1))),

    ('loosely dashed', (0, (5, 10))),
    ('dashed', (0, (5, 5))),
    ('densely dashed', (0, (5, 1))),

    ('loosely dashdotted', (0, (3, 10, 1, 10))),
    ('dashdotted', (0, (3, 5, 1, 5))),
    ('densely dashdotted', (0, (3, 1, 1, 1))),

    ('dashdotdotted', (0, (3, 5, 1, 5, 1, 5))),
    ('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))),
    ('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))
    ]

    x_vals = y_vals = np.arange(0, .5, .05)

    for name, tuple_ in linestyles:
    print(f"linestyle='{name}'")

    np.random.seed(1010)
    x = np.random.rand(9, 5)
    h0 = jl.hash(x)

    plt.plot(x_vals, y_vals, linestyle=tuple_)
    # plt.show()

    try:
    _ = np.linalg.svd(x)
    except np.linalg.LinAlgError as err:
    print(
    4*' ', err, '|', 'Has not changed:',
    h0 == jl.hash(x)
    )
    Shell的输出
    ======== RESTART: D:\matplotlib-interfering-with-numpy-on-windows.py ========
    linestyle='-.'
    SVD did not converge | Has not changed: True
    linestyle=':'
    SVD did not converge | Has not changed: True
    linestyle='solid'
    linestyle='dotted'
    SVD did not converge | Has not changed: True
    linestyle='dashed'
    SVD did not converge | Has not changed: True
    linestyle='dashdot'
    SVD did not converge | Has not changed: True
    linestyle='loosely dotted'
    linestyle='dotted'
    linestyle='densely dotted'
    linestyle='loosely dashed'
    linestyle='dashed'
    linestyle='densely dashed'
    linestyle='loosely dashdotted'
    linestyle='dashdotted'
    linestyle='densely dashdotted'
    linestyle='dashdotdotted'
    linestyle='loosely dashdotdotted'
    linestyle='densely dashdotdotted'
    追溯
    未捕获的回溯是:
    Traceback (most recent call last):
    File "D:\matplotlib-interfering-with-numpy-on-windows.py", line 25, in <module>
    mr = np.linalg.svd(x)
    File "<__array_function__ internals>", line 6, in svd
    File "███████████\lib\site-packages\numpy\linalg\linalg.py", line 1661, in svd
    u, s, vh = gufunc(a, signature=signature, extobj=extobj)
    File "███████████\lib\site-packages\numpy\linalg\linalg.py", line 97, in _raise_linalgerror_svd_nonconvergence
    raise LinAlgError("SVD did not converge")
    numpy.linalg.LinAlgError: SVD did not converge
    挖掘驱动器到 ███████████\lib\site-packages\numpy\linalg\_umath_linalg.cp37-win_amd64.pyd .很难进一步挖掘。

    关于python - Matplotlib 干扰 NumPy(在 Windows 上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64903221/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com