|
In Python, Numpy is a library that is created in order to perform scientific computations in Python. It provides support for arrays and it also supports basic matrix computations ( matrix multiplication, additions, subtraction, etc). In the Numpy library, we have various sets of functions such as .array(), .arrange(), .zeros(), .ones(), etc. In this article, we will briefly explore Numpy.genfromtxt function. We will see its multiple use cases along with various examples and explanations. What is Numpy genfromtxt() function?In the Numpy library, numpy.genfromtxt function is used to read data from any text file and convert it into Numpy array. It is generally used in handling data with some missing or inconsistent values. This function has various fields of applications such as in the machine learning field, where missing or inconsistent data must be filled with some values to perform operations. It is also used in various other fields such as financial analysis, geospatial data processing, scientific research, etc. It makes the process of cleaning, processing, and transforming data from any file very easier.
Parameters and Their UsesIn this, we will discuss all the parameters of the above function. We will see all the parameters briefly.
Basic Usage of Numpy.genfromtxtIn this example, we will see a very basic use case of the above function. We will read data from a dummy file and display the output. gfg.txt (our dummy file)
Output reading data from text-file. Advance Usage of Numpy.genfromtxtIn this we will fill the absent or inconsistent values from the files while converting it into numpy array. After converting the data, we will display the data with the help of for-loop. Dummy File ( data.csv) data.csv ExampleWe first import data from our dummy file (data.csv). We can notice that there are some missing values in the data. For example age is missing in id 3, coding score is missing in 2 etc. We will fill those values with a default value 1, for column with float values, we will fill them with default value 1.0. We will also perform conversion on the values. If the value’s column type is ‘int’, we will perform explicit type casting and try to convert values in the column to ‘int‘ if the values do not convert it into ‘int’, we will fill those values with some default values. At the end, we will display the numpy array data through for-loop.
Output filled values in the vacant spaces ConclusionIn Python, numpy library is a famous library which is generally used to perform scientific computations and mathematical computations. In numpy library we have various functions such as .arrange(), .arrays(), .zeros(), .ones() etc. We have briefly discussed one of its function Numpy.genfromtxt. We have seen its parameters with their uses . It is generally used data preprocessing, scientific computations and many more. We have briefly discussed how we can fill the default values and perform conversion in place of missing values. |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 19 |