Approach 2 : Using liac_arff and Pandas
We can use the liac_arff module alongside Pandas to import and convert an arff file into a Pandas DataFrame. Install the required modules first by executing the following command –
pip install liac-arff
Step – 1
After installing the required modules, we will import them.
Python3
import arff import pandas as pd |
Step – 2
After importing the required modules, we will use a variable in which we will import and store the arff file. We will use the loadarff() method of the ARFF module.
Python3
data, meta = arff.loadarff( '/content/cpu.arff' ) |
Here, the variable data has been used to load and open the ARFF file.
Step – 3
After that Convert the data to a Pandas DataFrame,
Python3
df = pd.DataFrame(data) |
Here, the data variable will be converted to a dataframe.
Step – 4
Finally, we will print the data frame to see if it is working properly or not.
Python3
print (df) |
Output:
MYCT MMIN MMAX CACH CHMIN CHMAX class
0 125.0 256.0 6000.0 256.0 16.0 128.0 198.0
1 29.0 8000.0 32000.0 32.0 8.0 32.0 269.0
2 29.0 8000.0 32000.0 32.0 8.0 32.0 220.0
3 29.0 8000.0 32000.0 32.0 8.0 32.0 172.0
4 29.0 8000.0 16000.0 32.0 8.0 16.0 132.0
.. ... ... ... ... ... ... ...
204 124.0 1000.0 8000.0 0.0 1.0 8.0 42.0
205 98.0 1000.0 8000.0 32.0 2.0 8.0 46.0
206 125.0 2000.0 8000.0 0.0 2.0 14.0 52.0
207 480.0 512.0 8000.0 32.0 0.0 0.0 67.0
208 480.0 1000.0 4000.0 0.0 0.0 0.0 45.0
[209 rows x 7 columns]
Reading An Arff File To Pandas Dataframe
Attribute-Relation File Format (ARFF) is a file format developed by the Machine Learning Project of the University of Waikato, New Zealand. It has been developed by the Computer Science department of the aforementioned University. The ARFF files mostly belong to WEKA (Waikato Environment for Knowledge Analysis), which is free software licensed under the GNU Free Public License. It is a collection of Machine Learning and Data Analysis tools.
In this article, we will see how we can convert an ARFF file into a Pandas data frame.
Prerequisites:
We will be using two modules here.
To install them, execute the following command –
pip install pandas
pip install scipy
Contact Us