Home>

I can not correctly collect all the procedures inside the class. There is a set of data that needs to be encrypted. I create a class, inside which a matrix is ​​created and the original data is multiplied by it:

class Preparer (TransformerMixin, BaseEstimator):
def fit (self, X, y= None, random_state= None):
    if random_state:
        np.random.seed (random_state)
    X_d= X.shape [1]
    Z= np.random.normal (size= (X_d, X_d))
    self.Z= Z
    self.X= X
    return self
def transform (self, X, y= None):
    return X.dot (self.Z)
X_new= Preparer (). Fit_transform (X, random_state= 12345)

I want to add a check of this matrix for reversibility inside the class, first I tried it like this:

class Preparer (TransformerMixin, BaseEstimator):
def fit (self, X, y= None, random_state= None):
    if random_state:
        np.random.seed (random_state)
    X_d= X.shape [1]
    Z= np.random.normal (size= (X_d, X_d))
    self.Z= Z
    self.X= X
    return self
def check (self):
    ch= self.Z
    Z_rev= np.linalg.inv (ch)
    if np.allclose (np.dot (ch, Z_rev), np.eye (ch.shape [0])) is True:
        ans= "Correct, it's alive"
    else:
        ans= "Incorrect"
    return ans
def transform (self, X, y= None):
    return X.dot (self.Z)
X_new= Preparer (). Fit_transform (X, random_state= 12345)
print (Preparer (). check ())
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<
ipython-input-49-970f16a02444 >
 in <
module >
      1 X_new= Preparer (). Fit_transform (X, random_state= 12345)
---->
 2 check_m= Preparer (). Check ()
      3 print (Preparer (). Check ())
      4 display (X.shape, X_new.shape)
<
ipython-input-48-325b53b325ec >
 in check (self, y)
     15
     16 def check (self, y= None):
--->
 17 ch= self.Z
     18 Z_rev= np.linalg.inv (ch)
     19 if np.allclose (np.dot (ch, Z_rev), np.eye (ch.shape [0])) is True:
AttributeError: 'Preparer' object has no attribute 'Z'

Then I tried to define variables through the constructor:

class Preparer (TransformerMixin, BaseEstimator):
    def __init __ (self, Z, X):
        self.Z= Z
        self.X= X
    def fit (self, X, y= None, random_state= None):
        if random_state:
            np.random.seed (random_state)
        X_d= X.shape [1]
        self.Z= np.random.normal (size= (X_d, X_d))
        return self
    def check (self):
        Z_rev= np.linalg.inv (Z)
        if np.allclose (np.dot (self.Z, Z_rev), np.eye (self.Z.shape [0])) is True:
            ans= "Correct, it's alive"
        else:
            ans= "Incorrect"
        return ans
    def transform (self, X, y= None):
        return X.dot (self.Z)

As a result, I catch the following error:

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<
ipython-input-10-ceee7b2c8018 >
 in <
module >
---->
 1 X_new= Preparer (). Fit_transform (X, random_state= 12345)
      2 #print (Preparer (). Check ())
      3 display (X.shape, X_new.shape)
TypeError: __init __ () missing 2 required positional arguments: 'Z' and 'X'

I can't figure out how to put it all together correctly. I looked through several articles by class, but I could not find any detailed one, everything is too simplified there, then in real life I cannot reproduce it using my own example. Please tell me how to build everything inside the class, or can you advise what article is good.