簡體   English   中英

Numpy和Matplotlib-AttributeError:“ numpy.ndarray”對象沒有屬性“ replace”

[英]Numpy and Matplotlib - AttributeError: 'numpy.ndarray' object has no attribute 'replace'

我正在嘗試使用數據列的日志值在python使用matplotlib生成圖,但我一直遇到此錯誤,

追溯(最近一次通話):
在第158行中輸入文件“ /home/PycharmProjects/proj1/test.py”

 graph(file_path) 

圖中的文件“ /home/PycharmProjects/proj1/test.py”,第90行

 y = np.array(np.log2(y1).replace(-np.inf, 0)) 

AttributeError:'numpy.ndarray'對象沒有屬性'replace'

下面是代碼,

def graph(file_path):
    dataset1 = pandas.read_csv(file_path)
    data1 = dataset1.iloc[:, 5]
    x, y1 = get_pdf(data1)
    y = np.array(np.log2(y1).replace(-np.inf, 0))

    plt.figure()
    plt.plot(x, y, color= 'g', label = 'Test')

    plt.legend()
    output_image = "fig1.png"
    plt.savefig(output_image)
    plt.close()
    plt.figure()

我非常感謝您提供一些幫助來解決此問題。 謝謝。

隨着log20產生警告和-inf:

In [537]: x = np.arange(5.)
In [538]: np.log2(x)
/usr/local/bin/ipython3:1: RuntimeWarning: divide by zero encountered in log2
  #!/usr/bin/python3
Out[538]: array([     -inf, 0.       , 1.       , 1.5849625, 2.       ])

但是log2是一個ufunc,並帶有whereout參數,可用於繞過此警告:

In [539]: out = np.zeros_like(x)
In [540]: np.log2(x, out=out, where=x>0)
Out[540]: array([0.       , 0.       , 1.       , 1.5849625, 2.       ])
In [541]: out
Out[541]: array([0.       , 0.       , 1.       , 1.5849625, 2.       ])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM