Updated June 6, 2023
Introduction to NumPy genfromtxt
The NumPy genfromtxt is one of the various functions supported by the Python numpy library that reads the table data, generates it into an array of data, and displays it as output. The numpy genfromtxt function operates two loops where one loop reads and converts the lines from the file into a string sequence, and another loop will reverse that string sequence into suitable data types to read without generating an error. The genfromtxt function commonly uses data containing various data types, including float, string, and object.
Syntax of NumPy genfromtxt
The basic syntax of the NumPy genfromtxt function:
numpy.genfromtxt(fname, dtype= , comments: , delimiter = , skiprows = , skip_header = , autostrip =)
- numpy.genfromtxt represents the function that reads the input data or file, which contains various data types, into an array format.
- fname is the filename of the input data or file we pass through the genfromtxt function. It can be given in a filename, list, or path to read.
- dtype is the data type declaration when we want the output array of the genfromtxt function in that particular data type. If we declare the dtype as ‘None’ it will automatically generate a data type depending on the values of that column.
- comments indicate the starting characters in a line of the given input data. We can discard the values or feelings that come after our declared comment.
- delimiter is the string value we declared to split the input values. For CSV files, we give delimiter as ‘,’ (comma) to separate the values in the file.
- skiprows is used to skip particular rows or a set of rows in the given input file.
- skip_header is used when we want to skip the input file’s starting rows or lines.
- autostrip, it can be declared to automatically remove the white spaces present in between the values in our data file.
Examples of NumPy genfromtxt
Given below are the examples of NumPy genfromtxt:
Example #1
Let’s see a basic example of how the numpy round function works.
Code:
import numpy as np
from io import StringIO
data = u"2, 4, 6\n3, 6, 9"
inp_data = np.genfromtxt(StringIO(data), delimiter=",")
inp_data
Output:
Code:
import numpy as np
from io import StringIO
data = u"2, 4, 6\n3, 6, 9\n5,10,15"
inp_data = np.genfromtxt(StringIO(data), delimiter=",")
inp_data
Output:
In the above example, we have called the numpy and io libraries for generating the output array from the genfromtxt function. The genfromtxt part processes the data provided in string format using the stringio function since the data is in Unicode string format. The delimiter used is a comma which will separate the values by identifying the comma. The output array generated is in the resultant array of the genfromtxt function.
You can provide the input in various formats, such as a list of strings, a single string, a generator, or an open object file with a read function. The genfromtxt function assumes and prints the output array depending on the input type.
For example, when we provide a single string genfromtxt function considers it as the file name in a local or remote location. Each string will be considered a distinct line in the file when submitting a list of strings. We can also pass the url location to the genfromtxt function, which will download and open the file from that url location.
Example #2
In this example, we will see how we can use the delimiter option depending on the width of the values in our input file to separate the data for our output array.
Code:
data2 = u" 4 4 8\n 2 4 88\n875407 5"
inp_data2 = np.genfromtxt(StringIO(data2), delimiter=3)
print(inp_data2)
data3 = u"987654321\n 1 3 5\n 8954 7"
inp_data3 = np.genfromtxt(StringIO(data3), delimiter=(4, 3, 2))
print(inp_data3)
Output:
We have declared two input data, data1 & data2, with integers with different widths, and we haven’t separated by a comma or characters, so for such cases, we use the integer values in delimiter to separate the values in the file for our desired output array.
The two input data have different values-width in a single line, so by using an integer delimiter as 3, we have separated the columns into three sets of values. We can utilize this method if our input file has a consistent width of deals throughout the entire file. Suppose our values in the file don’t follow the same width throughout. In that case, we can use the sequence of integer values in our delimiter to separate the values respectively, as we did in the data3 input, where we used the delimiter of sequences 4, 3, and 2 to separate the values.
Example #3
Here’s another example that demonstrates the use of the autostrip parameter. It removes any empty white spaces between the values.
Code:
data = u"5, xyz , 2\n 6, efg, 7"
# Executing Without autostrip
np.genfromtxt(StringIO(data), delimiter=",", dtype="|U6")
Output:
Code:
# ExecutingWith autostrip
np.genfromtxt(StringIO(data), delimiter=",", dtype="|U6", autostrip=True)
Output:
In this example, we have used two data as input, and we have printed two outputs, with one passing the autostrip parameter and another without passing through the autostrip parameter. There is a clear distinction between the two outputs – one with empty spaces between the values and another with the spaces removed. The empty spaces will lead to unnecessary complications while working in a file, so including the auto strip parameter in our genfromtxt function is helpful.
Example #4
In this example, we have used the skip header parameter in our input data to skip a specific number of values from our input data.
Code:
data = u"\n".join(str(i) for i in range(7))
np.genfromtxt(StringIO(data), delimiter=",")
Output:
Code:
# Executing With skip header
np.genfromtxt(StringIO(data), delimiter=",", skip_header=4)
Output:
Conclusion
In this article, we have seen NumPy genfromtxt function in detail using various examples to understand the numpy genfromtxt function and its uses clearly. We have also seen how to read input data files with multiple data types using the genfromtxt function. We have also seen the different parameters and techniques involved in using delimiters with examples.
Recommended Articles
We hope that this EDUCBA information on “NumPy genfromtxt” was beneficial to you. You can view EDUCBA’s recommended articles for more information.