Don’t Download! Read Datasets with URL in Python
For data science students, it happens a lot to work on public datasets. UCI is the most common machine learning repository that contains thousands of data. However, for an assignment, if you download the dataset locally, others cannot run your code on the other machines without downloading and replacing the local path. Moreover, for training a model on online platforms such as google collaboratory, you need to save the dataset on google drive, mount the google drive and use it. Such is also not runnable on the other collab notebooks without asking for permission to your google drive. On the other hand, if you read the dataset from the URL, you won’t have any of these problems.
In this tutorial, I explain how you can read five different types of data file format, which are:
.datafile format.csvfile format.arfffile format.zipfile format.rarfile format
How to read data or csv file formats from URL
Thank pandas, the first two cases are the easiest. To read the dataset, you only need to feed pandas.read_csv() the dataset URL. For example:
Example 1: Blood transfusion dataset with .data format.
[Embedded content: 9d2086de7f8cccd514bbdd7caa2d7386]
Example 2: Cervical Cancer dataset with .csv format. In this example, we already know that the dataset has missing values that are question marks. So, to read the dataset correctly, we include na_values=['?'].
[Embedded content: b21782837bc9d1b9d1dc44b6f5b43dc1]
For the other cases, the first step is to open the URL using urllib.request.urlopen(), then read the data file from the requested URL. However, the rest depends on the data file format.
How to read arff file formats from URL
To read .arff file formats as the dataframe, after opening the URL, read the URL and load .arff files using scipy.io.arff.loadarff(). In most cases, we need to decode it as well. Then we can convert it to the pandas dataframe by pd.DataFrame().
Example 3: Diabetic Retinopathy Debrecen dataset with .arff format. I obtained column names from the link to assign to the dataframe.
One common problem is that in some cases, the integer columns are read as an object data type, such that instead of 1 we have b'1'. To solve this problem, which also happened in this example, we go over all the columns, and if the data type is the object, we decode them again (line 18).
[Embedded content: 93e6f645f25cb7dc30efd43874553c2e]
How to read zip file formats from URL
If the data file format is .zip, it might contain several files, such as metadata or data files. To read such data files, we need to unzip them first, and the rest depends on what the data file format compresses in the zip file.
Example 4: Obesity Level dataset with .zip format, which contains a .csv file.
[Embedded content: 91a116e1796ca9a2ecd09c11ce5c3dbb]
How to read rar file formats from URL
I believe this is the most challenging file format. There are several Python libraries to extract and read rar files, such as rarfile, patool, pyunpack, etc. I selected rarfile because it is simpler to use.
Example 5: Chronic Kidney Disease dataset with .rar format, which contains several .arff files.
[Embedded content: 229a4a00d233e58783f655753611f49e]
Summary
The good news is, you can use a Python library contains functions for reading UCI datasets set easily. So far, it contains 36 datasets, it looks for your contributions to add more datasets. 😊
For the above examples, the easiest way to load the datasets is to install uci_dataset
pip install uci_dataset
After that, you can read all the above datasets by loading the functions from there.
Example 6: Read dataset from uci_dataset Python Library.
[Embedded content: aabdde903420b29992e6ae8e230c315e]
**Join Medium to read great tutorials and stroies! **
- I write Machine Learning, Deep Learning and Data Science tutorials. Upgrade to read more.*


