Home >Backend Development >Python Tutorial >How to store and read data as text in numpy

How to store and read data as text in numpy

不言
不言Original
2018-06-04 16:09:312156browse

This article mainly introduces the method of storing and reading data in text format in numpy. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

In addition to In addition to saving data to a file as a binary file, you can also choose to save the data to a text file. If I need disk storage, I usually choose text storage because there are more options for post-processing tools.

The text storage data file can use the savetxt function, and the corresponding file can be loaded using the loadtxt function. Unlike binary storage, savetxt's functionality does not append extensions automatically.

The following is a simple operation demonstration:

In [15]: arr1 =rand(5,4)
 
In [16]: arr1
Out[16]:
array([[0.21349492, 0.77993282, 0.37269246, 0.70599725],
  [ 0.74004045, 0.64697716, 0.49489394, 0.94005934],
  [ 0.89902693, 0.43021685, 0.29623512, 0.4259565 ],
  [ 0.00146385, 0.7619464 , 0.2764662 , 0.00896728],
  [ 0.17746182, 0.81107356, 0.13140944, 0.12808611]])
 
In [17]:np.savetxt('data.txt',arr1)

Through the above operations, The array information is stored in the data.txt file. It can be edited and modified through other text editors or other processing tools. The results of viewing the file directly in text form are as follows:

C:\Users\ThinkPad\Desktop>typedata.txt
2.134949194782667092e-017.799328187516920696e-01 3.726924550593806451e-01 7.059972531846898658e-01
7.400404474495648754e-016.469771552354630639e-01 4.948939386825553788e-01 9.400593405075502451e-01
8.990269288143762916e-014.302168497691762905e-01 2.962351210526772416e-01 4.259564974067475696e-01
1.463850064000737916e-037.619464016912527171e-01 2.764661957409741966e-01 8.967282719944846825e-03
1.774618247314488917e-018.110735600283927038e-01 1.314094418012348164e-01 1.280861102265743456e-01

Loading of the file:

In [22]: new_arr =np.loadtxt('data.txt')
 
In [23]: new_arr
Out[23]:
array([[0.21349492, 0.77993282, 0.37269246, 0.70599725],
  [ 0.74004045, 0.64697716, 0.49489394, 0.94005934],
  [ 0.89902693, 0.43021685, 0.29623512, 0.4259565 ],
  [ 0.00146385, 0.7619464 , 0.2764662 , 0.00896728],
  [ 0.17746182, 0.81107356, 0.13140944, 0.12808611]])

The stored data files can be reused to create array objects by loading. In order to verify the consistency of storage and reading, do the following checks:

In [25]: arr1 ==new_arr
Out[25]:
array([[True, True, True, True],
  [ True, True, True, True],
  [ True, True, True, True],
  [ True, True, True, True],
  [ True, True, True, True]], dtype=bool)

As can be seen from the above, the data read back has the same effect as the original.

Related recommendations:

numpy Examples of array splicing and merging on rows and columns respectively

numpy extension method for merging multi-dimensional matrices and lists

The above is the detailed content of How to store and read data as text in numpy. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn