Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read npy File data by python

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "python how to read npy file data" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "python how to read npy file data" article can help you solve the problem.

Note: .npy files are binary files specific to numpy.

1. Read and save import numpy as nparr = np.array ([1,2,3], [4,5,6]]) np.save ('weight.npy', arr) loadData = np.load (' weight.npy') print ("- type----") print (type (loadData)) print ("- shape----") print (loadData.shape) print ("- data----") print (loadData)

two。 Actual combat case

In the process of deep neural network training, it is usually necessary to read the pre-training weights, which are usually .npy files, such as vgg16.npy. This time, take the analysis of vgg16.npy as an example to illustrate.

Import numpy as np# pay attention to the encoding mode pre_train = np.load ("vgg16.npy", allow_pickle=True, encoding= "latin1") print ("- type-") print (type (pre_train)) print ("- shape-") print (pre_train.shape) print ("- data-") print (pre_train)

What is this? Why didn't shape? But you can see that the element in pre_train is supposed to be a dictionary, and we try to take it out.

Note: ndarray.item () is an element in the copied array and returns it.

Import numpy as nppre_train = np.load ("vgg16.npy", allow_pickle=True, encoding= "latin1") data_dic = pre_train.item () print ("- type-") print (type (data_dic)) print ("- conv1_1 data-") print (data_dic ['conv1_1']) # returns a list with two array Represents the weight w of conv1_1 and offset bprint ("- conv1_1 shape-") print ((data_dic ['conv1_1'] [0]) .shape)

Look at the results:

As you can see, this is the weight parameter of the first convolution layer, with an input channel of 3 and an output channel of 64.

Attached: an example of read and write operation of .npy file in python

Read and write binaries in numpy:

Save

Np.save (". / file name", array name): saves data in binary format

Load

Np.load (". / filename .npy"): the function reads data from a binary file

Savez

Np.savez ('. / file name', array name 1, array name 2,...) The savez function can save multiple arrays to a file

(1) save operation

Import numpy as npa=np.arange (5) np.save ('get.npy',a)

(2) load operation

Import numpy as npa=np.load ('load.npy') print (a)

(3) savez operation

Import numpy as npa=np.arange (3) b=np.arange (4) c=np.arange (5) np.savez ('array_save.npz',a,b,c) several arrays of ndarray types about "how python reads npy file data" ends here. Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report