open-risk / transitionMatrix

Statistical analysis and visualization of state transition phenomena
https://www.openriskmanagement.com/tags/transition-matrix/
Apache License 2.0
86 stars 32 forks source link

Address numpy.matrix deprecation #10

Open open-risk opened 3 years ago

open-risk commented 3 years ago

numpy.matrix is on a deprecation path, given than a transitionMatrix object derives from it, some alternative should be developed

epogrebnyak commented 3 years ago

I think the depreciation is an opportunity to revisit the structure of the classes - they are so overloaded now with IO operations, matrix validation, manipulation, etc.

Can anything simple as below work?

from typing import List
import numpy as np

TM = np.ndarray
TMSet = List[TM]

def empty(dimensions: int) -> TM:
   return np.identity(dimension)

Another idea is to hide ndarray behind a dataclass, but leave non-essential methods out:

from dataclasses import dataclass

@dataclass
class TransitionMatrix:
    matrix: np.ndarray

def matrix(values) -> TransitionMatrix:
    pass

def read_csv(filename) -> TransitionMatrix:
    pass

https://github.com/open-risk/transitionMatrix/blob/f69c58904c95b38c5bac4d5451eb52a48cf15175/transitionMatrix/model.py#L204-L211