Python access to npy format data examples

There are two main functions for data processing

(1): np.save("test.npy", data structure) —-save data

(2): data =np.load('test.npy”) —-Get data

Give 2 examples as follows (save the list)

1、

z =[[[1,2,3],['w']],[[1,2,3],['w']]]
np.save('test.npy', z)
x = np.load('test.npy')

x:-array([[list([1,2,3]),list(['w'])],[list([1,2,3]),list(['w'])]], dtype=object)

2、 Save dictionary

x
- {0:' wpy',1:'scg'}
np.save('test.npy',x)
x = np.load('test.npy')
x
- array({0:'wpy',1:'scg'}, dtype=object)

3、 After saving as a dictionary format and reading, you need to call the following statement first

data.item()

Convert data numpy.ndarray object to dict

Supplementary knowledge: python read mat or npy file and save mat file as npy file (or npy save as mat) method

Read mat files and save them as npy format files

See the code for details, pay attention to the transposition of h5py

import numpy as np
from scipy import io

mat = io.loadmat('yourfile.mat')
# If an error is reported:Please use HDF reader for matlab v7.3 files
# Change to the next way to read
import h5py
mat = h5py.File('yourfile.mat')

# There may be multiple cells in the mat file, each corresponding to a dataset

# You can use the keys method to view the name of the cell,Now use list(mat.keys()),
# In addition, use data to read= mat.get('first name'),Then you can use Numpy to convert to array
print(mat.keys())
# You can use the values method to view the information of each cell
print(mat.values())

# You can use shape to view dimension information
print(mat['your_dataset_name'].shape)
# Note that the shape information seen here is different from the one you opened in matlab
# The matrix here is the transpose of the matrix when matlab is opened
# So we need to transpose it back
mat_t = np.transpose(mat['your_dataset_name'])
# mat_t is numpy.ndarray format

# Save it as npy format file
np.save('yourfile.npy', mat_t)

The reading of npy files is very simple

import numpy as np
matrix = np.load(‘yourfile.npy’)

The npy file can be re-read and saved as a mat file

Method one (I encountered an error when double-clicking to open MATLAB: Unable to read MAT-file *********.mat. Not a binary MAT-file. Try load -ASCII to read as text. ):

import numpy as np

matrix = np.load('yourfile.npy')
f = h5py.File('yourfile.mat','w')
f.create_dataset('dataname', data=matrix)
# The data will not be transposed here

Method two (using scipy):

from scipy import io

mat = np.load('rlt_gene_features.npy-layer-3-train.npy')
io.savemat('gene_features.mat',{'gene_features': mat})

The above example of Python accessing npy format data is all the content shared by the editor. I hope to give you a reference.

Recommended Posts

Python access to npy format data examples
Python reads .nii format image examples
Python generates VOC format label examples
Python Romberg method to find integral examples
Python uses pandas to process Excel data
01. Introduction to Python
100 small Python examples
How to use the format function in python
02. Python data types
Python data model
Python data analysis
python data structure
Python data format-CSV
Detailed examples of using Python to calculate KS
Introduction to Python
How does Python store data to json files
Centos 6.4 python 2.6 upgrade to 2.7
Centos 6.4 python 2.6 upgrade to 2.7
Python data analysis-data update
Python data analysis-apply function
Python data analysis-data selection
Python basic data types
Python basic data types
Python data analysis-data establishment