Machine Learning Prerequisites (Numpy)

13 / 32

Numpy - Arrays - Loading a text file data using NumPy's loadtxt() function - Step 1

Although Pandas provides better ways and constructs to load a dataset from various sources like files, databases, etc. we should also know the NumPy constructs for the same.

There are two ways (constructs) in NumPy to load data from a text file:

(1) using loadtxt() function

(2) using genfromtxt() function

loadtxt() function provides less flexibility, whereas genfromtxt() function provides more flexibility.

For example, genfromtxt() function also handles the missing values kind of scenarios in the loaded dataset, whereas loadtxt() function doesn't.

Example of loadtxt()

import numpy as np
name_arr, address_arr, zipcode_arr = np.loadtxt('my_file.txt', skiprows=2, unpack=True)

The above piece of code will load the data from my_file.txt text file.

skiprows=2 means, skip the first two rows of the my_file.txt file while loading the data.

unpack=True means, unpack the columns of the dataset being loaded and return the data of each column separately in separate arrays ( name column data in name_arr array, address column data in address_arr array, zipcode column data in zipcode_arr array).

unpack=False means, return only a single array as output from the loadtxt() function.


No hints are availble for this assesment

Answer is not availble for this assesment

Loading comments...