亚洲欧洲国产欧美一区精品,激情五月亚洲色五月,最新精品国偷自产在线婷婷,欧美婷婷丁香五月天社区

      python

      當(dāng)前位置:中華考試網(wǎng) >> python >> python編程基礎(chǔ) >> 文章內(nèi)容

      Python實(shí)例:numpy.load()的使用

      來(lái)源:中華考試網(wǎng)  [2020年10月31日]  【

        編程函數(shù)千千萬(wàn),當(dāng)然新的最有意思。今天小編為大家?guī)Я薾umpy.load()使用的詳細(xì)講解。

        numpy.load()函數(shù)從具有npy擴(kuò)展名(.npy)的磁盤文件返回輸入數(shù)組。

        用法:

        

      numpy.load(file, mmap_mode=None, allow_pickle=True, fix_imports=True, encoding=’ASCII’)

        參數(shù):

        file :file-like對(duì)象,字符串或pathlib.Path。要讀取的文件。 File-like對(duì)象必須支持seek()和read()方法。

        mmap_mode :如果不為None,則使用給定模式memory-map文件(有關(guān)詳細(xì)信息,請(qǐng)參見numpy.memmap

        模式說(shuō)明)。

        allow_pickle :允許加載存儲(chǔ)在npy文件中的腌制對(duì)象數(shù)組。

        fix_imports :僅在在Python 3上加載Python 2生成的腌制文件時(shí)有用,該文件包括包含對(duì)象數(shù)組的npy /npz文件。

        encoding :僅當(dāng)在Python 3中加載Python 2生成的腌制文件時(shí)有用,該文件包含包含對(duì)象數(shù)組的npy /npz文件。

        Returns :數(shù)據(jù)存儲(chǔ)在文件中。對(duì)于.npz文件,必須關(guān)閉NpzFile類的返回實(shí)例,以避免泄漏文件描述符。

        代碼1:

        

      # Python program explaining

        # load() function

        import numpy as geek

        a = geek.array(([i + j for i in range(3)

        for j in range(3)]))

        # a is printed.

        print("a is:")

        print(a)

        geek.save('geekfile', a)

        print("the array is saved in the file geekfile.npy")

        # the array is saved in the file geekfile.npy

        b = geek.load('geekfile.npy')

        # the array is loaded into b

        print("b is:")

        print(b)

        # b is printed from geekfile.npy

        print("b is printed from geekfile.npy")

        輸出:

        

      a is:

        [0, 1, 2, 1, 2, 3, 2, 3, 4]

        the array is saved in the file geekfile.npy

        b is:

        [0, 1, 2, 1, 2, 3, 2, 3, 4]

        b is printed from geekfile.npy

        代碼2:

        

      # Python program explaining

        # load() function

        import numpy as geek

        # a and b are numpy arrays.

        a = geek.array(([i + j for i in range(3)

        for j in range(3)]))

        b = geek.array([i + 1 for i in range(3)])

        # a and b are printed.

        print("a is:")

        print(a)

        print("b is:")

        print(b)

        # a and b are stored in geekfile.npz

        geek.savez('geekfile.npz', a = a, b = b)

        print("a and b are stored in geekfile.npz")

        # compressed file is loaded

        c = geek.load('geekfile.npz')

        print("after loading...")

        print("a is:", c['a'])

        print("b is:", c['b'])

        輸出:

        

      a is:

        [0 1 2 1 2 3 2 3 4]

        b is:

        [1 2 3]

        a and b are stored in geekfile.npz

        after loading...

        a is:[0 1 2 1 2 3 2 3 4]

        b is:[1 2 3]

      python課程免費(fèi)試聽預(yù)約

      • 地區(qū):
      • 姓名:
      • 手機(jī):
      責(zé)編:fushihao
      • 會(huì)計(jì)考試
      • 建筑工程
      • 職業(yè)資格
      • 醫(yī)藥考試
      • 外語(yǔ)考試
      • 學(xué)歷考試