I was asked to rewrite the distribution fitting function from Matlab code to Python code.

```
%%file distribution_fitting.m
function pd = distribution_fitting(feature)
distnames =["Poisson", "Exponential", "Gamma", "ExtremeValue", "Kernel"];
values_must_be_positive =["Poisson", "Exponential", "Gamma"];
x=feature.';
x_values = linspace(min(x),max(x));
for dn=distnames
if min(x)<0 & ~isempty(find(strcmp(dn, values_must_be_positive)))
continue;
end
distname = char(dn);
pd = fitdist(x.',distname);
res1=kstest(x, 'CDF', pd);
res2=chi2gof(x, 'CDF', pd);
if (~res1 && ~res2)
fprintf('%s with 5%% significance levelr',distname);
cdfplot(x)
hold on
plot(x_values,cdf(pd,x_values),'r-')
plot(x_values,pdf(pd,x_values),'g-')
legend('Empirical CDF',[distname ' CDF'],[distname ' PDF'],'Location','best');
%title(['Empirical CDF and ', [distname ' CDF/PDF']);
hold off
return;
else
fprintf('Not %s with 5%% significance leveln',distname);
end
end
end
```

My first approach looks like this:

```
def distribution_fitting(feature):
distnames = ["Poisson", "Exponential", "Gamma", "ExtremeValue", "Kernel"]
values_must_be_positive = ["Poisson", "Exponential", "Gamma"]
x = feature
x_values = np.linspace(np.min(x), np.max(x))
for dn in distnames:
if (np.min(x) < 0 and (np.where(dn in values_must_be_positive)))
continue
distname = str(dn)
```

According to Implement the MATLAB 'fitdist' in Python

I should use from `import scipy.stats as st`

the `st.gaussian_kde(data)`

and `st.norm.fit(data)`

.

I have no idea, if this would fit, because Matlab has a very own system compared to Python.

Is my Python approach right? Where can I found out how to write the next lines into Python code? I’m not familiar with Matlab.

Source: Python Questions