Skip to content

Instantly share code, notes, and snippets.

@bitnulleins
Created January 31, 2023 22:32
Show Gist options
  • Save bitnulleins/3b2e8f8f3f67b8f44c93ad8317c06767 to your computer and use it in GitHub Desktop.
Save bitnulleins/3b2e8f8f3f67b8f44c93ad8317c06767 to your computer and use it in GitHub Desktop.
Ordinal Classification / Ordinal Regression class
from sklearn.base import BaseEstimator
import numpy as np
class OrdClass(BaseEstimator):
"""
Helper class that solves ordinal classification (classes that have an order to them eg cold,warm,hot)
Paper (2001): https://www.cs.waikato.ac.nz/~eibe/pubs/ordinal_tech_report.pdf
Fixed source from: https://github.com/garyongguanjie/Ordinal-Classifier
"""
def __init__(self,classifier=None,clf_args=None):
"""
y needs to be a number that start from 0 and increments by 1
classifier object needs to be able to return a probability
"""
self.classifier = classifier
self.clfs = []
self.clf_args = clf_args
self.final_prob = None
def fit(self,X,y,**fit):
self.X = X
self.y = y
import copy
no_of_classifiers = int(np.max(self.y)) #since y starts from 0
self.classes_ = list(range(no_of_classifiers+1))
for i in range(no_of_classifiers):
# make a copy of y because we want to change the values of y
copy_y = np.copy(self.y)
# make a binary classification here
copy_y[copy_y<=i] = 0
copy_y[copy_y>i] = 1
classifier = copy.deepcopy(self.classifier)
classifier.fit(self.X,copy_y,**fit)
self.clfs.append(classifier)
return self
def predict_proba(self,test):
prob_list = []
final_prob = []
length = len(self.clfs)
for clf in self.clfs:
prob_list.append(clf.predict_proba(test)[:,1])
for i in range(length+1):
if i == 0:
final_prob.append(1-prob_list[i])
elif i == length:
final_prob.append(prob_list[i-1])
else:
final_prob.append(prob_list[i-1]-prob_list[i])
answer = np.array(final_prob).transpose()
self.final_prob= answer
return answer
def predict(self,test):
self.predict_proba(test)
return np.argmax(self.final_prob,axis=1)
def score(self,X,y,sample_weight=None):
return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment