Halloween Sale: Flat 70% + Addl. 25% Off + 30 Days Extra Lab on all Courses | Use Coupon HS25 in Checkout | Offer Expires In

  Enroll Now

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.


Please follow the below steps:

Clone git repository

This tutorial needs housing.csv file. You can get it from CloudxLab ML GitHub repository. Go to the 'Console' and type the following command: git clone https://github.com/cloudxlab/ml ~/ml Please do this in the 'Console' and not 'Notebook' tab.

Alternatively, you can download this file to your laptop/desktop and upload it to your home directory using Jupyter file manager: https://github.com/cloudxlab/ml/blob/master/machine_learning/datasets/housing/housing.csv

This step is important without which your data will not be available for next steps. You can verify that the file exists in the right path in Jupyter Notebook by

!ls ../ml/machine_learning/datasets/housing/housing.csv

No hints are availble for this assesment

Answer is not availble for this assesment

Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...