Introduction to NumPy genfromtxt
The NumPy genfromtxt is one of the various functions supported by python numpy library that reads the table data and generates it into an array of data and displays as output. The numpy genfromtxt function perform an operation in two loops where one loop reads and convert the lines from the file into a string sequence and another loop will convert that string sequence into suitable data types to read without generating an error. The genfromtxt function is generally used when the data has a different data types from float, string, object, etc.
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 which reads the input data or file which contains various data types into array format.
- fname is the filename of the input data or file we are passing through the genfromtxt function. It can be given in 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 data type depending on the values of that columns.
- comments it is used in indicating the starting characters in a line of the given input data. We can discard the values or characters that comes after our declared comment.
- delimiter its the string value we declared to split the input values. For csv file we give delimiter as ‘,’ (comma) to separate the values in the file.
- skiprows is used to skip a particular rows or a set of rows in the given input file.
- skip_header is used when we wanted to skip the starting rows or lines of the given input file.
- 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:
Lets see a basic example for understanding how the numpy round function works.
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
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
Here in the above example we have called the numpy and io libraries for generating the output array from the genfromtxt function. Data is manually given in string format and it is passed through the genfromtxt function using the stringio function since we have used the unicode of string in data. The delimiter used is comma which will separate the values by identifying the comma. The output array generated is in resultant array of the genfromtxt function.
The input format can be given as list of string or a string or a generator or open object file with a read function. Depending upon the input type the genfromtxt function assumes and prints the output array.
For example when we provide a single string genfromtxt function considers it as file name in local or remote location. When a string list is provided it considers each string as a separate line in the file. We can also pass the url location to the genfromtxt function which will download and open the file from that url location.
In this example we will see how we can use the delimiter option depending upon our width of the values in our input file to separate the data for our output array.
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)
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 has 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. This method can be used when our input file has same width in values throughout the entire file. If our values in the file doesn’t follow same width throughout we can use the sequence of integer values in our delimiter to separate the values respectively like we did in the data3 input where we have used the delimiter of sequence 4, 3, 2 to clearly separate the values.
Lets see another example where we use the autostrip parameter which can be used to remove the empty white spaces between the values.
data = u"5, xyz , 2\n 6, efg, 7" # Executing Without autostrip np.genfromtxt(StringIO(data), delimiter=",", dtype="|U6")
# ExecutingWith autostrip np.genfromtxt(StringIO(data), delimiter=",", dtype="|U6", autostrip=True)
In this example we have used two data as input and we have printed two outputs with one passing the autostrip parameter and another one without passing through the autostrip parameter. We can see the difference in both the outputs where one has the empty spacing in between the values and another where the spaces are removed. The empty spaces will lead to unnecessary complication while working in a file so it is useful to include the auto strip parameter in our genfromtxt function.
In this example we have used the skip header parameter in our input data to skip specific number of values from our input data.
data = u"\n".join(str(i) for i in range(7)) np.genfromtxt(StringIO(data), delimiter=",")
# Executing With skip header np.genfromtxt(StringIO(data), delimiter=",", skip_header=4)
In this article, we have seen NumPy genfromtxt function in detail using various examples to get a clear understanding on the numpy genfromtxt function and its uses. We have also seen how to read input data file with various data types using the genfromtxt function and we also seen the usage of different parameters and techniques involved in using delimiters with examples.
This is a guide to NumPy genfromtxt. Here we discuss the introduction to NumPy genfromtxt along with examples respectively. You may also have a look at the following articles to learn more –