Search code examples
pythonmachine-learningregressionlogistic-regressionnon-linear-regression

How can I do ordinal regression using the mord module in python?


I am trying to predict a label based on some features and I have some training data.

Searching for ordinal regression in python, I found http://pythonhosted.org/mord/ but I could not figure out how to use it.

It would be great if someone has an example code to demonstrate how to use this module. Here are the classes in the mord module:

>>>import mord    
>>>dir(mord)
    ['LAD',
 'LogisticAT',
 'LogisticIT',
 'LogisticSE',
 'OrdinalRidge',
 '__builtins__',
 '__doc__',
 '__file__',
 '__name__',
 '__package__',
 '__path__',
 '__version__',
 'base',
 'check_X_y',
 'grad_margin',
 'linear_model',
 'log_loss',
 'metrics',
 'np',
 'obj_margin',
 'optimize',
 'propodds_loss',
 'regression_based',
 'sigmoid',
 'svm',
 'threshold_based',
 'threshold_fit',
 'threshold_predict',
 'utils']

Solution

  • I believe it follows the API of Scikit-learn. So here is an example:

    import numpy as np
    import mord as m
    c = m.LogisticIT() #Default parameters: alpha=1.0, verbose=0, maxiter=10000
    c.fit(np.array([[0,0,0,1],[0,1,0,0],[1,0,0,0]]), np.array([1,2,3]))
    c.predict(np.array([0,0,0,1]))
    c.predict(np.array([0,1,0,0]))
    c.predict(np.array([1,0,0,0]))
    

    The output will be as follows:

    array([1])

    array([2])

    array([3])

    Hope it was helpful