日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python3-numpy IO load()、save()、savez()、loadtxt()、savetxt()、tofile()、fromfile()

發(fā)布時間:2024/9/27 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3-numpy IO load()、save()、savez()、loadtxt()、savetxt()、tofile()、fromfile() 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Numpy 可以讀寫磁盤上的文本數(shù)據(jù)或二進制數(shù)據(jù)。
NumPy 為 ndarray 對象引入了一個簡單的文件格式:npy
npy 文件用于存儲重建 ndarray 所需的數(shù)據(jù)、圖形、dtype 和其他信息。
常用的 IO 函數(shù)有:

  • load() 和 save() 函數(shù)是讀寫文件數(shù)組數(shù)據(jù)的兩個主要函數(shù),默認情況下,數(shù)組是以未壓縮的原始二進制格式保存在擴展名為 .npy 的文件中。
  • loadtxt() 和 savetxt() 函數(shù)處理正常的文本文件(.txt 等)

1、save()、load()

numpy.save() 函數(shù)將數(shù)組保存到以 .npy 為擴展名的文件中。

numpy.save(file, arr, allow_pickle=True, fix_imports=True)

參數(shù)說明:

  • file:要保存的文件,擴展名為 .npy,如果文件路徑末尾沒有擴展名 .npy,該擴展名會被自動加上。
  • arr: 要保存的數(shù)組
  • allow_pickle: 可選,布爾值,允許使用 Python pickles 保存對象數(shù)組,Python 中的 pickle 用于在保存到磁盤文件或從磁盤文件讀取之前,對對象進行序列化和反序列化。
  • fix_imports: 可選,為了方便 Pyhton2 中讀取 Python3 保存的數(shù)據(jù)。

numpy.load(file, mmap_mode=None, allow_pickle=True, fix_imports=True, encoding=’ASCII’)
encoding :僅當在Python 3中加載Python 2生成的文件時有用

a = np.array([1, 2, 3, 4, 5])# 保存到 outfile.npy 文件上 np.save('outfile.npy', a) # 保存到 outfile2.npy 文件上,如果文件路徑末尾沒有擴展名 .npy,該擴展名會被自動加上 np.save('outfile2',a)""" 可以看出文件是亂碼的,因為它們是 Numpy 專用的二進制格式后的數(shù)據(jù)。 我們可以使用 load() 函數(shù)來讀取數(shù)據(jù)就可以正常顯示了: """ b = np.load('outfile.npy') print(b) # [1 2 3 4 5]

2、savez()

numpy.savez() 函數(shù)將多個數(shù)組保存到以 npz 為擴展名的文件中。

numpy.savez(file, *args, **kwds)

參數(shù)說明:

  • file:要保存的文件,擴展名為 .npz,如果文件路徑末尾沒有擴展名 .npz,該擴展名會被自動加上
  • args: 要保存的數(shù)組,可以使用關鍵字參數(shù)為數(shù)組起一個名字,非關鍵字參數(shù)傳遞的數(shù)組會自動起名為 arr_0, arr_1, …
  • kwds: 要保存的數(shù)組使用關鍵字名稱。
a = np.array([[1,2,3],[4,5,6]]) b = np.arange(0, 1.0, 0.1) c = np.sin(b) # c 使用了關鍵字參數(shù) sin_array np.savez("runoob.npz", a, b=b, sin_array = c) r = np.load("runoob.npz") print(r.files) # 查看各個數(shù)組名稱 print(r["arr_0"]) # 數(shù)組 a print(r["b"]) # 數(shù)組 b print(r["sin_array"]) # 數(shù)組 c """ ['b', 'sin_array', 'arr_0'] [[1 2 3][4 5 6]] [0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9] [0. 0.09983342 0.19866933 0.29552021 0.38941834 0.479425540.56464247 0.64421769 0.71735609 0.78332691] """

3、loadtxt()、savetxt()

savetxt() 函數(shù)是以簡單的文本文件格式存儲數(shù)據(jù),對應的使用 loadtxt() 函數(shù)來獲取數(shù)據(jù)。可以 用 .txt 或者 .csv 為擴展名

np.loadtxt(FILENAME, dtype=int, delimiter=’ ',skiprows=0, usecols=None,unpack=False)
np.savetxt(FILENAME, a, fmt="%d", delimiter=",")
參數(shù) delimiter 可以指定各種分隔符、針對特定列的轉換器函數(shù)、需要跳過的行數(shù)等。

a = np.array([[1, 2, 3, 4, 5], [7, 8, 9, 10, 11]]) # delimiter 用什么分割數(shù)據(jù) # fmt 保存的數(shù)據(jù)格式 # skiprows 跳過前幾行。一般跳過表頭 # dtype 指定數(shù)據(jù)類型 # usecols 取哪幾列 # unpack 轉置 效果 類似 numpy.transpose ndarray.T np.savetxt('out.txt', a, delimiter=",", fmt="%d,%d,%.3f,%.3f,%.3f") b = np.loadtxt('out.txt', delimiter=",", skiprows=0, dtype=np.int32, usecols=(0,1,2)) b1 = np.loadtxt('out.txt', delimiter=",", skiprows=0, dtype=np.int32, usecols=(0,1,2), unpack=True) print(b) print("*"*20) print(b1) print("*"*20) a = np.array([1, 2, 3, 4, 5]) np.savetxt('out1.txt', a, delimiter=",", fmt="%d") b = np.loadtxt('out1.txt', delimiter=",")print(b) """ [[1 2 3][7 8 9]] ******************** [[1 7][2 8][3 9]] ******************** [1. 2. 3. 4. 5.]out.txt: 1,2,3.000,4.000,5.000 7,8,9.000,10.000,11.000 out1.txt 1 2 3 4 5 """

4、tofile()、fromfile()

https://blog.csdn.net/kebu12345678/article/details/54837245/
https://www.cnblogs.com/yinyoupoet/p/13287353.html

  • tofile()將數(shù)組中的數(shù)據(jù)以二進制格式寫進文件
  • tofile()輸出的數(shù)據(jù)不保存數(shù)組形狀和元素類型等信息
  • fromfile()函數(shù)讀回數(shù)據(jù)時需要用戶指定元素類型,并對數(shù)組的形狀進行適當?shù)男薷?/strong>
a = np.arange(12).reshape(4,3) # a.tofile("raw10.raw") a.tofile("raw10.bin") print(a.dtype) # int32 print(a)b = np.fromfile("raw10.bin",dtype=np.int64) # 按照 np.int64 讀入的數(shù)據(jù)是錯誤的 print(b) # [ 4294967296 12884901890 21474836484 30064771078 38654705672 47244640266]b = np.fromfile("raw10.bin",dtype=np.int32) # 按照 np.int32 讀入的數(shù)據(jù)是一維的 print(b) # [ 0 1 2 3 4 5 6 7 8 9 10 11] b.shape = 4,3 print(b)print((a==b).all()) # True """ int32 [[ 0 1 2][ 3 4 5][ 6 7 8][ 9 10 11]] [ 4294967296 12884901890 21474836484 30064771078 38654705672 47244640266] [ 0 1 2 3 4 5 6 7 8 9 10 11] [[ 0 1 2][ 3 4 5][ 6 7 8][ 9 10 11]] True """

https://www.runoob.com/numpy/numpy-io.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結

以上是生活随笔為你收集整理的python3-numpy IO load()、save()、savez()、loadtxt()、savetxt()、tofile()、fromfile()的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。