0

I'm new to Python and I have some questions when applying code to adjust probability distributions.

I am trying to apply this code:

import scipy
import scipy.stats
import matplotlib
import matplotlib.pyplot as plt

class Distribution(object):

    def __init__(self,dist_names_list = []):
        self.dist_names = ['norm','lognorm','expon']
        self.dist_results = []
        self.params = {}

        self.DistributionName = ""
        self.PValue = 0
        self.Param = None

        self.isFitted = False


    def Fit(self, y):
        self.dist_results = []
        self.params = {}
        for dist_name in self.dist_names:
            dist = getattr(scipy.stats, dist_name)
            param = dist.fit(y)

            self.params[dist_name] = param
            #Applying the Kolmogorov-Smirnov test
            D, p = scipy.stats.kstest(y, dist_name, args=param);
            self.dist_results.append((dist_name,p))
        #select the best fitted distribution
        sel_dist,p = (max(self.dist_results,key=lambda item:item[1]))
        #store the name of the best fit and its p value
        self.DistributionName = sel_dist
        self.PValue = p

        self.isFitted = True
        return self.DistributionName,self.PValue*emphasized text*

I understand that y is my DataFrame with my sales data. However, I don't understand what the self argument is. I tried this:

self = pd.DataFrame(columns=['dist_names','dist_results','params','DistributionName','PValue','Param','isFitted'])   

But it did not work. Can someone help me please?

Phillyclause89
  • 674
  • 4
  • 12
Lcs
  • 1
  • 3
  • It's like `this` on some other languages, if that's helpful. – phipsgabler Mar 26 '20 at 10:27
  • Thanks. But what should I do with the self argument? Should I define it before using it in the Fit function? I tried it with Fit (self, i_sales), doing nothing with this argument, but it didn't work. – Lcs Mar 26 '20 at 10:36
  • [self is discussed in the instance-attributes of this realpython article](https://realpython.com/python3-object-oriented-programming/#instance-attributes) I would recommend learning the basics of OOP before diving into a class this complex. – Phillyclause89 Mar 26 '20 at 10:39
  • `self` is passed *implicitly*, you just do `Distribution().Fit(y=42)` without worrying about *passing* `self`. – deceze Mar 26 '20 at 10:44
  • Thank you! OK, I tried `Distribution().Fit(y=i_sales)`, being `i_sales` my dataframe (with sku, date and sales quantity). However, the following error appeared: `TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''` – Lcs Mar 26 '20 at 11:15

0 Answers0