SoFunction
Updated on 2024-11-15

Python implementation of npy/mat file saving and reading

In addition to the commonly used csv file and excel file, we can also use PY to save the data in npy file format and mat file format.

1. npy files

npy, the corresponding file format for numpy, uses the () method for its saving and the () method for its reading.

Specific examples are shown below:

import numpy as np
a = ('1, 2, 3;4, 5, 6')
print(a)
print(type(a))
print("=================================")
b = ([[1, 2, 3], [4, 5, 6]])
print(b)
print(type(b))

Save the file:

As shown, both matrices and numpy arrays are supported to be saved as npy file types.

('', a)
('', b)                  

Read file

data1 = ('')
data2 = ('')

print(data1)
print(type(data1))
print("=================================")
print(data2)
print(type(data2))

As shown in the figure npy data was read successfully and all are numpy array data type.

2. mat files

Saving as a mat file relies on the () method in the scipy library, and reading requires the () method.

When saving, not only do you need to pass in the variable, but you also need to pass in the type of that variable along with it in the form of a dictionary, which supports numpy arrays and matrices as well.

Specific examples are shown below:

import numpy as np
from scipy import io
a = ('1, 2, 3;4, 5, 6')
print(a)
print(type(a))
print("=================================")
b = ([[1, 2, 3], [4, 5, 6]])
print(b)
print(type(b))

('', {'matrix': a})
('', {'array': b})

retrieve data

data1 = ('')
print(data1)
print(type(data1))
print("=================================")
data2 = ('')
print(data2)
print(type(data2))

As shown in the figure, the data is successfully read. But the result of reading is a dictionary, if you need to read further into the data, you need to take it out according to the key name:

print(data1['matrix'])
print(type(data1['matrix']))
print("=================================")
print(data2['array'])
print(type(data2['array']))

The key on fetch is related to the type of variable on store, the data fetched are numpy arrays, no more matrix types.

replenishment

Read a mat file and save it as an npy file.

See the code for details, and note the h5py transpose issue

import numpy as np
from scipy import io

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

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

# You can use the keys method to see the name of the cell, but now you need to use list(()), which you can use to see the name of the cell.
# Also, to read, use data = ('name'), which can then be converted to an array using Numpy.
print(())
# You can use the values method to view information about each cell.
print(())

# You can view dimension information with shape
print(mat['your_dataset_name'].shape)
# Note that the shape information you see here is different from what you open in matlab
# The matrices here are the transpositions of the matrices when matlab is open #
# So we need to transpose it back #
mat_t = (mat['your_dataset_name'])
# mat_t is the format

# Then save it as an npy file
('', mat_t)

The npy file is simple to read

import numpy as np
matrix = ('')

Re-read npy file save as mat file

Method 1(existMATLABError encountered when double-clicking to open:Unable to read MAT-file *********.mat. Not a binary MAT-file. Try load -ASCII to read as text. ):

import numpy as np

matrix = ('')
f = ('', 'w')
f.create_dataset('dataname', data=matrix)
# There's no transposition of data here

Method 2 (using scipy):

from scipy import io

mat = ('rlt_gene_features.')
('gene_features.mat', {'gene_features': mat})

to this article on the Python npy/mat file save and read the article is introduced to this, more related Python npy mat file save and read content please search my previous posts or continue to browse the following related articles I hope you will support me in the future!