Skip to content

Tutorial: Your first classification

Gustavo Rosa edited this page Apr 7, 2021 · 6 revisions

Performing a classification with OPFython is easy as pie. Every code starts with some imports, right?

import opfython.math.general as g	 
import opfython.stream.loader as l	 
import opfython.stream.parser as p	 
import opfython.stream.splitter as s	  
from opfython.models import SupervisedOPF

The next step is to load any data, where it should be a numpy array. We offer some pre-loading and pre-parsing methods in order to assist users loading data from .txt, .csv or .json files.

# Loading a .txt file to a numpy array	 
txt = l.load_txt('data/boat.txt')	 
  
# Parsing a pre-loaded numpy array	 
X, Y = p.parse_loader(txt)

Next, if one desires, the data can be split into training, validation, or testing sets.

# Splitting data into training and testing sets	 
X_train, X_test, Y_train, Y_test = s.split(X, Y, percentage=0.5, random_state=1)

Finally, we can instantiate the OPF class. Note that it is possible to change the distance metric or to load pre-computed distances from a file.

# Creates a SupervisedOPF instance	  
opf = SupervisedOPF(distance='log_squared_euclidean', pre_computed_distance=None)

Now, invoke a simple command in order to train the classifier.

# Fits training data into the classifier	 
opf.fit(X_train, Y_train)

Further, new predictions can be realized with another simple command.

# Predicts new data	  
preds = opf.predict(X_test)

In order to check how effective the classifier was, one can use our pre-defined metrics, such as the OPF-based accuracy.

# Calculating accuracy	  
acc = g.opf_accuracy(Y_test, preds)	 
 	 
print(f'Accuracy: {acc}')
Clone this wiki locally