Example usage

This notebook shows example usage of rerfClassifier class. Based on the guide found at https://www.datacamp.com/community/tutorials/random-forests-classifier-python with rerfClassifier swapped out in place of sklearn’s RandomForestClassifier

This notebook can run interactively using Gigantum

[1]:
from rerf.rerfClassifier import rerfClassifier

# Import scikit-learn dataset library
from sklearn import datasets
[2]:
# Load dataset
iris = datasets.load_iris()
[3]:
# print the label species(setosa, versicolor,virginica)
print(iris.target_names)
['setosa' 'versicolor' 'virginica']
[4]:
# print the names of the four features
print(iris.feature_names)
['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
[5]:
# Creating a DataFrame of given iris dataset.
import pandas as pd
[6]:
data = pd.DataFrame(
    {
        "sepal length": iris.data[:, 0],
        "sepal width": iris.data[:, 1],
        "petal length": iris.data[:, 2],
        "petal width": iris.data[:, 3],
        "species": iris.target,
    }
)
print(data.head())
   sepal length  sepal width  petal length  petal width  species
0           5.1          3.5           1.4          0.2        0
1           4.9          3.0           1.4          0.2        0
2           4.7          3.2           1.3          0.2        0
3           4.6          3.1           1.5          0.2        0
4           5.0          3.6           1.4          0.2        0
[7]:
# Import train_test_split function
from sklearn.model_selection import train_test_split
[8]:
X = data[["sepal length", "sepal width", "petal length", "petal width"]]  # Features
y = data["species"]  # Labels
[9]:
# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3
)  # 70% training and 30% test
[10]:
# Create a RerF Classifier
clf = rerfClassifier(n_estimators=100)
[11]:
print(clf)
rerfClassifier(feature_combinations=1.5, image_height=None, image_width=None,
               max_depth=None, max_features='auto', min_samples_split=1,
               n_estimators=100, n_jobs=None, oob_score=False,
               patch_height_max=None, patch_height_min=1, patch_width_max=None,
               patch_width_min=1, projection_matrix='RerF', random_state=None)
[12]:
# Train the model using the training sets y_pred=clf.predict(X_test)
clf.fit(X_train, y_train)
[12]:
rerfClassifier(feature_combinations=1.5, image_height=None, image_width=None,
               max_depth=None, max_features='auto', min_samples_split=1,
               n_estimators=100, n_jobs=None, oob_score=False,
               patch_height_max=None, patch_height_min=1, patch_width_max=None,
               patch_width_min=1, projection_matrix='RerF', random_state=None)
[13]:
y_pred = clf.predict(X_test)
[14]:
# Import scikit-learn metrics module for accuracy calculation
from sklearn import metrics
[15]:
# Model Accuracy, how often is the classifier correct?
print("Accuracy:", metrics.accuracy_score(y_test, y_pred))
Accuracy: 0.9777777777777777