This is the first part of the tutorial, focusing on MERCS as a simple classifier.
import numpy as np
import os
import sys
from sklearn.metrics import f1_score, accuracy_score, classification_report
import pandas as pd
sys.path.insert(0, '../..') # We add the parent dir to the path
from src.mercs.core import MERCS
from src.mercs.utils import *
import src.datasets as datasets
First, we import the nursery dataset.
train, test = datasets.load_nursery()
This is a fully nominal dataset
train.head()
model = MERCS()
ind_parameters = {'ind_type': 'RF',
'ind_n_estimators': 30}
sel_parameters = {'sel_type': 'Base',
'sel_its': 4,
'sel_param': 1}
train.head()
model.fit(train, **ind_parameters, **sel_parameters)
code = [0,0,0,0,0,0,0,0,1]
len(code)
pred_parameters = {'pred_type': 'MI',
'pred_param': 1.0,
'pred_its': 8}
y_pred = model.predict(test,
**pred_parameters,
qry_code=code)
y_pred
y_true = test[test.columns.values[np.array(code)==1]].values
obs = f1_score(y_true, y_pred, average='macro')
obs
assert isinstance(obs, (int, float))
assert 0 <= obs <= 1
train.iloc[1:8000,2] = np.nan
model.fit(train, **ind_parameters, **sel_parameters)
train
y_pred = model.predict(test,
**pred_parameters,
qry_code=code)
obs = f1_score(y_true, y_pred, average='macro')
obs
md = model.s['metadata']
md
md = {k:v for k,v in md.items() if k is not 'FI'}
nominal_attributes = [x for x in range(md['nb_atts']) if md['is_nominal'][x] == 1]
numeric_attributes = [x for x in range(md['nb_atts']) if md['is_nominal'][x] == 0]
nominal_attributes
np.unique(md['is_nominal'])
np.unique(md['types']).shape[0]
a = model.m_codes.copy()
b = model.m_codes.copy()
a.copy()
a = np.array([[1,2,3]])
b = np.array([[3,4,5]])
c = None
np.concatenate((a,b,c))
a.shape
a = np.array([1,0,0,0])
u = np.unique(a)
#u.sort()
u
np.array_equal(np.unique(a),[1,0])