NumPy storing and reading data in a literal way

Source: Internet
Author: User
This article mainly introduces the method of storing and reading data in the way of text in NumPy, has certain reference value, now share to everybody, need friend can refer to

In addition to the ability to save data in a file as a binary file, NumPy can also choose to save the data to a text file. If I have disk storage needs, I usually choose the storage of the text, because later processing tools will have more choices.

The text storage data file can take the function of savetxt, and the loading of the corresponding file can take Loadtxt function. Unlike binary storage, the Savetxt feature does not automatically append extensions.

The following is a simple practice demonstration:

In [all]: arr1 =rand (5,4) in [+]: Arr1out[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)

With the above operation, the information of the array is stored in the Data.txt file. You can edit the changes by using another text editor or other processing tools. To view the file directly as text, the results are as follows:

C:\users\thinkpad\desktop>typedata.txt2.134949194782667092e-017.799328187516920696e-01 3.726924550593806451e-01 7.059972531846898658e-017.400404474495648754e-016.469771552354630639e-01 4.948939386825553788e-01 9.400593405075502451e-018.990269288143762916e-014.302168497691762905e-01 2.962351210526772416e-01 4.259564974067475696e-011.463850064000737916e-037.619464016912527171e-01 2.764661957409741966e-01 8.967282719944846825e-031.774618247314488917e-018.110735600283927038e-01 1.314094418012348164e-01 1.280861102265743456e-01

Loading of files:

in [+]: New_arr =np.loadtxt (' data.txt ') in []: New_arrout[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]])

Stored data files can be re-created by loading the array object, in order to verify the consistency of storage and read, do a check as follows:

in [+]: arr1 ==new_arrout[25]:array ([[True, True, true, True], [true, True, true, true  ],  [True, True, true, Tru E], [True, True, true, true  ],  [True, True, true, True]], Dtype=bool)

As can be seen from the above, the data read back with the original has an equivalent effect.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.