You are reading the article How Savetxt Function Work In Numpy With Examples updated in September 2023 on the website Lifecanntwaitvn.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How Savetxt Function Work In Numpy With Examples
Introduction to NumPy savetxtPython provides different functions to the users. To work with arrays, the python library provides a NumPy function. Mainly NumPy savetxt function is used to save arrays in txt format with different delimiters. NumPy savetxt function works with 1D and 2D arrays, the numpy savetxt also saves array elements in csv file format. Arrays play a major role in data science where speed matters. NumPy is an acronym for numerical python. Basically, NumPy is an open source project. NumPy performs logical and mathematical operations of arrays. Therefore, processing and manipulating can be done efficiently.
Start Your Free Software Development Course
Syntax of NumPy savetxtGiven below is the syntax:
Explanation:
file_name: file_name is used for actual file name. It means that if a file ends with .txt then the file is automatically saved in text format. In the same manner, it works for different file extensions.
Arr: Arr means 1D or 2D array and it is used to store array data in a text file.
format: format parameter is used to define a sequence of patterns. It is used when we want to save data into the text file. If format is specified as single like “%d” that means it is applicable to all the elements. The 2D array specified is different for each column and it is an optional part.
delimiter: It is used either as a string or character to separate columns and it is an optional parameter of savetxt function.
newline: The newline parameter is used to separate string or character and it is an optional parameter of savetxt function.
header: String or character will be written at the beginning of the file with the help of header parameter and it is an optional parameter.
footer: String or character will be written at the end of file with the help of header parameter and it is an optional parameter.
How savetxt Function work in NumPy?
We must install Python on our system.
We must install numpy using the pip command.
We require basic knowledge about Python.
We require basic knowledge about the savetxt function with different parameters.
We require basic knowledge about arrays.
We can perform different operations using a numpy savetxt function.
Examples of NumPy savetxtGiven below are the examples of NumPy savetxt:
Example #1For 1D Array.
Code:
import numpy as np A = np.arange(0, 6, 1) print("The array of A:") print(A) c = np.savetxt('1D.txt', A, delimiter=', ') # save array into txt file X = open("1D.txt", 'r') # open file in read mode print("the file contains is:") print(X.read()) # open fileExplanation:
We import numpy functions and use them as np.
We declared variable A for array and assigned values.
We try to print the value of the variable.
Then we use the savetxt function to store array values into the txt file.
After that we open that file in read mode.
Finally we try to print txt file.
In the above example we implemented a 1D array using numpy savetxt function.
Illustrate the end result of the above declaration by using the use of the following snapshot.
Output:
Example #2For 2D Array.
Code:
import numpy as np a = np.arange(0, 5, 1) b = np.arange(5, 10, 1) c = np.arange(10, 15, 1) print("Array a is:") print(a) print("Array b:is:") print(b) print("Array c: is:") print(c) x = np.savetxt('demoa.txt', (a, b, c)) # Array with same dimension y = open("demoa.txt", 'r') # open file in read mode print("the file contains:") print(y.read())
We import numpy functions and use it as np.
We declared variable a, b and c for array and assigned values.
We try to print the value of the variable a, b and c.
Then we use the savetxt function to store array values into the chúng tôi file with same array dimension.
After that we open that file in read mode.
Finally we try to print chúng tôi files.
In the above example we implement a 2D array using numpy savetxt function.
Illustrate the end result of the above declaration by using the use of the following snapshot.
Output:
Example #31D array store into csv file with header and footer.
Code:
import numpy as np a = np.arange(0, 5, 1) print("Array a is:") print(a) x = np.savetxt('hf.csv', a, delimiter=',' , header='A sample 1D array :: Header', footer='This is footer of csv file') # savetxt function with header and footer. y = open("hf.csv", 'r') # open file in read mode print("the file contains following array values:") print(y.read())Explanation:
We import numpy functions and use them as np.
We declared variable a for array and assigned values.
We try to print the value of the variable a.
Then we use the savetxt function to store array values into the chúng tôi file with header and footer.
After that we open that file in read mode.
Finally we try to print the chúng tôi file.
In the above example we implemented a 1D array using numpy savetxt function with header and footer parameters.
Illustrate the end result of the above declaration by using the use of the following snapshot.
Output:
Example #41D Array with exponential notation.
Code:
import numpy as np a = b = c = np.arange(0.0,4.0,1.0) print(a) np.savetxt('demo.txt', a, delimiter=',') np.savetxt('demo.txt', (a,b,c)) np.savetxt('demo.txt', a, fmt='%1.4e') # exponential notation x = open("demo.out", 'r') print("the file contains following array values:") print(x.read())Explanation:
In the above example we have followed the same process but the only difference is that in this program we have added exponential notation.
Illustrate the end result of the above declaration by using the use of the following snapshot.
Output:
ConclusionFrom the above article we saw basic syntax of numpy savetxt functions. We also saw how we can implement them in python with different examples. From this article we have seen how we can handle numpy savetxt functions in python.
Recommended ArticlesThis is a guide to NumPy savetxt. Here we discuss the introduction, how savetxt function work in NumPy? along with examples respectively. You may also have a look at the following articles to learn more-
You're reading How Savetxt Function Work In Numpy With Examples
Update the detailed information about How Savetxt Function Work In Numpy With Examples on the Lifecanntwaitvn.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!