Search code examples
pythonpandasscikit-learnclass-methodpositional-argument

Whats the correct way to call and use this class? Also have TypeError: missing 1 required positional argument: 'self'


I'm still learning the various uses for class methods. I have some code that performs linear regression. So I decided to make a general class called LinRegression and use more specific methods that call the class based on the type of linear regression (i.e use one trailing day, or 5 trailing days etc for the regression).

Anyways, here it goes. I feel like I am doing something wrong here with regards to how I defined the class and am calling the class. This is from the main.py file:

lin_reg = LinRegression(daily_vol_result)
lin_reg.one_day_trailing()

And this is from the linear_regression file (just showing the one day trailing case):

class LinRegression:
    import matplotlib.pyplot as plt
    import numpy as np
    from sklearn.linear_model import LinearRegression as lr
    from sklearn.metrics import mean_squared_error as mse
    from SEplot import se_plot as SE

    def __init__(self, daily_vol_result):
        """
        :param daily_vol_result: result from def daily_vol_calc
        """
        import numpy as np
        data = np.asarray(daily_vol_result['Volatility_Daily'])
        self.data = data

    @classmethod
    def one_day_trailing(cls, self):
        """
        Compute one day trailing volatility
        :return: Mean Squared error, slope: b, and y-int: c
        """

        x = self.data[:-1]
        y = self.data[1:]
        x = x.reshape(len(x), 1)
        cls.lr.fit(x, y)
        b = cls.lr.coef_[0]
        c = cls.lr.intercept_
        y_fit1 = b * x + c
        MSE1 = cls.mse(y, y_fit1)
        print("MSE1 is " + str(MSE1))
        print("intercept is " + str(c))
        print("slope is " + str(b))

        cls.SE(y, y_fit1)

        return MSE1, b, c

What I "think" I am doing is that when I call lin_reg, I already have the daily_vol_result passed, then lin_reg.one_day_trailing() should just execute the one_day_trailing def using the self defined in init.

However, I get TypeError: one_day_trailing() missing 1 required positional argument: 'self'. Some other info, the variable, daily_vol_result is a DataFrame and I convert to np array to do the linear regression with sklearn.

Also, when I tried messing around with the code to work, I had an additional issue where the line: lr.fit(x, y) gave me a type error with no positional arg for y. I checked the existence and length of y to see if it matched x and it checks out. I am pretty confused as to how I was only passing one arg.

Your ideas and advice are welcome, thanks!


Solution

  • I found out that the linear regression package was acting like a class and so lr.fit(self, x, y) was what it wanted as an input. I first instantiated the class as:

    A = lr(), then A.fit(x,y).
    

    I had this line in my main file:

    ASDF = LinRegression.one_day_trailing(daily_vol_result)
    

    I also figured out a more general way to produce these functions. I did not end up needing to use @classmethod or @staticmethod