国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Python | NumPy | numpy.core._exceptions.MemoryError: Unable to allocate 1.04 MiB for an array

這篇具有很好參考價值的文章主要介紹了Python | NumPy | numpy.core._exceptions.MemoryError: Unable to allocate 1.04 MiB for an array。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

本文簡要概括出現(xiàn)類似于 numpy.core._exceptions.MemoryError: Unable to allocate 1.04 MiB for an array with shape (370, 370) and data type float64 報錯信息的原因及解決方法。

更新:2023 / 2 / 4



報錯

numpy.core._exceptions.MemoryError: Unable to allocate 1.04 MiB for an array with shape (370, 370) and data type float64

原因

主要原因是電腦 RAM 內(nèi)存不足,因為需要處理的數(shù)據(jù)量太大,GPU性能不夠,存在內(nèi)存溢出現(xiàn)象 12


解決方案

參考這里 34


修改 float 精度

在代碼中使用的是 flaot 64 類型,然而實際上未必需要這么大的精度,這時候可以使用 numpy 中的 float32,float16 等,這樣可以降低很多的內(nèi)存需求。


python 庫、Pandas 和 Numpy 庫更新為 64 位

python 原始的數(shù)據(jù)類型是 32 bit,但是最大只能使用 2G 內(nèi)存,超過 2G 報錯 MemoryError。
如果你的 Python 用的是 32 bit 的,那么你的 pandasNumpy 也只能是 32 bit 的,那么當(dāng)你的內(nèi)存使用超過 2G 時,就會自動終止內(nèi)存。而 64bit python 則無此限制。
因此,可以先檢查自己安裝的 python 是多少位的 5,如果是 32 bit,那么就重裝 64 bitPython。


如果你的 python 本來安裝的就是 64 位的,可以采用下面的方法


擴充虛擬內(nèi)存

在運行代碼的過程中發(fā)現(xiàn),內(nèi)存其實只用到了一半不到,但是為什么會出現(xiàn) Memory 的錯誤呢?
進過百度發(fā)現(xiàn)說是內(nèi)存被限制了,所以考慮采用擴大虛擬內(nèi)存的方法。

對于 windows 系統(tǒng),擴大虛擬內(nèi)存的方法:

  • 打開 控制面板;
  • 點擊 系統(tǒng) 這一項;
  • 點擊 高級系統(tǒng)設(shè)置 這一項;
  • 點擊 性能 模塊的 設(shè)置 按鈕;
  • 選擇 高級面板,點擊更改 虛擬內(nèi)存 模塊;
  • 記得不要選中 自動管理所有驅(qū)動器的分頁文件大小,然后選擇一個驅(qū)動器,也就是一個盤,選中自定義大小,手動輸入初始大小和最大值,但是不要太大。
  • 都設(shè)置好之后,記得點擊 設(shè)置, 然后再確定,否則無效,最后 重啟電腦 就可以了。

修改 pycharm 的運行內(nèi)存 6,

  • Help -> Find Action -> (type VM Options) ->(Click) Edit Custom VM Options
  • 打開 pycharm64.exe.vmoptions 進行編輯
  • 修改 -Xmx750m-Xmx4096m
  • 分配 4G 內(nèi)存,視情況而定。
  • 保存并重啟 pycharm

更改 Python 讀取大文件的方法

出現(xiàn) memoryError 錯誤和文件讀取太慢的問題,后來找到了兩種比較快 Large File Reading 的方法,這里將介紹這兩種讀取方法。


Preliminary

我們談到 文本處理 時,我們通常是指處理的內(nèi)容。Python 將文本文件的內(nèi)容讀入可以操作的字符串變量非常容易。

文件對象提供了三個 方法,.read().readline().readlines()。每種方法可以接受一個變量以限制每次讀取的數(shù)據(jù)量,但它們通常不使用變量。

.read() 每次讀取整個文件,它通常用于將文件內(nèi)容放到一個字符串變量中。然而 .read() 生成文件內(nèi)容最直接的字符串表示,但對于連續(xù)的面向行的處理,它卻是不必要的,并且如果文件大于可用內(nèi)存,則不可能實現(xiàn)這種處理。

下面是 .read() 方法示例:

try:
    f = open('/path/to/file', 'r')
    print f.read()
finally:
    if f:
        f.close()

調(diào)用 read() 會一次性讀取文件的全部內(nèi)容,如果文件有 10G,內(nèi)存就爆了,所以,要保險起見,可以反復(fù)調(diào)用 read(size) 方法,每次最多讀取 size 個字節(jié)的內(nèi)容。

另外,調(diào)用 readline() 可以每次讀取一行內(nèi)容,調(diào)用 readlines() 一次讀取所有內(nèi)容并按行返回list。因此,要根據(jù)需要決定怎么調(diào)用。

如果文件很小,read() 一次性讀取最方便;如果不能確定文件大小,反復(fù)調(diào)用 read(size) 比較保險;如果是配置文件,調(diào)用 readlines() 最方便:

for line in f.readlines():
    process(line) 									# <do something with line>

Read In Chunks

處理大文件是很容易想到的就是將大文件分割成若干小文件處理,處理完每個小文件后釋放該部分內(nèi)存。這里用了 iter & yield

def read_in_chunks(filePath, chunk_size=1024*1024):
    """
    Lazy function (generator) to read a file piece by piece.
    Default chunk size: 1M
    You can set your own chunk size 
    """
    file_object = open(filePath)
    while True:
        chunk_data = file_object.read(chunk_size)
        if not chunk_data:
            break
        yield chunk_data
if __name__ == "__main__":
    filePath = './path/filename'
    for chunk in read_in_chunks(filePath):
        process(chunk) 							   # <do something with chunk>

Using with open()

with 語句打開和關(guān)閉文件,包括拋出一個內(nèi)部塊異常。
for line in f 文件對象 f 視為一個迭代器,會自動的采用緩沖 IO 和內(nèi)存管理,所以你不必?fù)?dān)心大文件。

#If the file is line based
with open(...) as f:
    for line in f:
        process(line) # <do something with line>

參考鏈接


  1. Numpy.core._exceptions.MemoryError: Unable to allocate array with shape (51, 6, 64, 2) and data type float32 ??

  2. MemoryError: Unable to allocate MiB for an array with shape and data type, when using anymodel.fit() in sklearn ??

  3. 解決numpy.core._exceptions.MemoryError: Unable to allocate 1.04 MiB for an array ??

  4. Very large matrices using Python and NumPy ??

  5. 查看python是32位的還是64位的 ??

  6. MemoryError: Unable to allocate array with shape (61721, 16000) and data typ ??文章來源地址http://www.zghlxwxcb.cn/news/detail-407661.html

到了這里,關(guān)于Python | NumPy | numpy.core._exceptions.MemoryError: Unable to allocate 1.04 MiB for an array的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Python提示‘ModuleNotFoundError: No module named ‘numpy.core._multiarray_umath‘

    Python提示‘ModuleNotFoundError: No module named ‘numpy.core._multiarray_umath‘

    ???????在學(xué)習(xí)Python編程使用 matplotlib 時,總是提示: ModuleNotFoundError: No module named \\\'numpy.core._multiarray_umath\\\' 問題大致描述如下: ???????上述錯誤的原因是找不到 matplotlib 所在包,類似于java中的找不到依賴,基于此種原因,有兩個解決思路: 首先應(yīng)該確保已經(jīng)正確安裝呆 matpl

    2024年04月09日
    瀏覽(22)
  • 成功解決Unable to allocate xxx MiB for an array with shape (xxxx, xxxx)

    成功解決Unable to allocate xxx MiB for an array with shape (xxxx, xxxx)

    看了網(wǎng)上的一些解決方案,應(yīng)該是跟內(nèi)存有關(guān)。 1.修改 pycharm 的運行內(nèi)存(未解決) 打開pycharm64.exe.vmoptions進行編輯修改,把 -Xmx750m改為 -Xmx8192m,分配8G內(nèi)存,內(nèi)存分配視情況而定。保存并重啟pycharm。 2.修改虛擬內(nèi)存(解決) 視情況設(shè)置需要的虛擬內(nèi)存。

    2024年02月12日
    瀏覽(26)
  • docker 報錯 library initialization failed - unable to allocate file descriptor table - out of memory

    docker 報錯 library initialization failed - unable to allocate file descriptor table - out of memory

    docker容器,啟動的時候 容器日志報: library initialization failed - unable to allocate file descriptor table - out of memory docker啟動容器時,若未給容器配置ulimit,則從docker守護進程上設(shè)置的默認(rèn)ulimits繼承, 這個值太大? 會報這個錯 官方文檔: dockerd | Docker Docs ulimit nofile這個值太大報錯的

    2024年04月29日
    瀏覽(27)
  • docker 啟動報錯 library initialization failed - unable to allocate file descriptor table - out of memory

    docker 啟動報錯 library initialization failed - unable to allocate file descriptor table - out of memory 1.報錯日志 library initialization failed - unable to allocate file descriptor table - out of memory/cm-server/aiboxCloud-web/boot/entrypoint.sh: line 2: 6 Aborted (core dumped) java -Xms1024m -Xmx2048m -jar -XX:+PrintGCDateStamps -XX:+PrintGCDetai

    2024年02月11日
    瀏覽(27)
  • mac的M1電腦airtest報錯ImportError: Error importing numpy: you should not try to import numpy from

    mac的M1電腦airtest報錯ImportError: Error importing numpy: you should not try to import numpy from

    執(zhí)行的時候報錯: airtest 報錯ImportError: Error importing numpy: you should not try to import numpy from its source directory; please exit the numpy source tree, and relaunch your python interpreter from there. 還有報錯:ImportError: dlopen(/Users/linjing/Library/Python/3.9/lib/python/site-packages/cv2/cv2.abi3.so, 0x0002): tried: ‘/Users/li

    2024年04月11日
    瀏覽(17)
  • numpy 鄰接矩陣轉(zhuǎn)稀疏矩陣 array to scipy csr_matrix

    也就是說,一個dense的numpy矩陣,如何轉(zhuǎn)換成scipy包里面的sparse的csr矩陣看代碼: 輸出: print(csr_a.toarray()) print(csr_a.tocoo())

    2024年02月12日
    瀏覽(23)
  • 三分鐘學(xué)習(xí)一個python小知識4-----------我的對python中numpy的理解, 我列舉了關(guān)于numpy常用的10個例子來深入理解numpy

    三分鐘學(xué)習(xí)一個python小知識4-----------我的對python中numpy的理解, 我列舉了關(guān)于numpy常用的10個例子來深入理解numpy

    NumPy 是專門用于數(shù)值計算的Python庫。它提供了實現(xiàn)向量、矩陣和高維數(shù)組的高效數(shù)據(jù)結(jié)構(gòu)和函數(shù),以及用于數(shù)學(xué)計算的工具。NumPy使得Python語言與MATLAB和R語言相比,更適合探索性數(shù)據(jù)分析、科學(xué)計算和大規(guī)模數(shù)據(jù)處理等領(lǐng)域。 NumPy中的核心數(shù)據(jù)結(jié)構(gòu)是 ndarray (N維數(shù)組),它是

    2024年02月10日
    瀏覽(27)
  • SpringBoot啟動報錯Unable to start web server; nested exception...

    SpringBoot啟動報錯Unable to start web server; nested exception...

    在創(chuàng)建SpringBoot項目時,依賴加載,但是啟動報錯如下:Unable to start web server; nested exception… 網(wǎng)上找了很多,很多回答為版本不一致,但仔細(xì)檢查,版本對應(yīng)都修改好了,但是還是報這個錯誤,test卻能夠成功。 根據(jù)自測,是導(dǎo)入的依賴問題:注釋掉或者刪除即可 原因是在創(chuàng)建

    2024年02月12日
    瀏覽(20)
  • 關(guān)于selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain...的處理

    參考兩位大佬的文章:代碼實現(xiàn)對selenium的驅(qū)動器WebDrive的配置_疏狂難除的博客-CSDN博客 selenium打開瀏覽器報錯成功解決selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain...-CSDN博客 直接上報錯: 參考兩位的文章后感覺報錯原因是尋找路徑失敗或沖突的問題。 因為運行

    2024年02月04日
    瀏覽(17)
  • 深度學(xué)習(xí)解決Unable to allocate 33.6 GiB for an array with shape (60000, 224, 224, 3) and data type float32

    深度學(xué)習(xí)解決Unable to allocate 33.6 GiB for an array with shape (60000, 224, 224, 3) and data type float32

    深度學(xué)習(xí)時,常常要處理超大文件。因此,常常引起電腦故障。當(dāng)電腦的內(nèi)存16G,虛擬內(nèi)存16G,讀入34G的數(shù)組,發(fā)生錯誤:Unable to allocate 33.6 GiB for an array with shape (60000, 224, 224, 3) and data type float32。解決辦法: 在win10設(shè)置-查找-高級設(shè)置-性能選項-虛擬內(nèi)存-選自定義大小-按C盤

    2024年02月12日
    瀏覽(24)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包