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 store data as text in numpy

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

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to store data in text in numpy". In daily operation, I believe that many people have doubts about how to store data in text in numpy. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to store data in text in numpy". Next, please follow the editor to study!

In Numpy, 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 have a need for disk storage, I usually choose the storage of text, because there will be more choices for post-processing tools.

Text storage data files can use the function of savetxt, and the loading of corresponding files can use the function of loadtxt. Unlike binary storage, savetxt does not automatically append extensions.

Next, do a simple demonstration of the operation:

In [15]: arr1 = rand (5Power4) In [16]: 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)

Through the above operation, the array information is stored in the data.txt file. You can edit and modify it through other text editors or other processing tools. View the file directly in text form and 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 [22]: new_arr = np.loadtxt ('data.txt') In [23]: 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]))

The stored data file can be re-used to create array objects by loading. To verify the consistency between storage and reading, check as follows:

In [25]: arr1 = = new_arrOut [25]: array ([[True, True], [True, True], dtype=bool) this ends the study of "how to store data in numpy by text", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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