# Rewrite Matlab into Python: Try to fit parametric probability distributions - Equivalent functions in Python

조회 수: 14(최근 30일)
Ramón Wilhelm 2021년 4월 13일
편집: Mayank 2021년 10월 15일
Has anyone an idea how to rewrite this Mathlab Distribution Fitting function into 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 level\r',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 level\n',distname);
end
end
end
My approach starts with
i
mport scipy.stats
import scipy
from sklearn.preprocessing import StandardScaler
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:
distname = getattr(scipy.stats, dn)
probDist = distname.fit(x.T)
res1= scipy.stats.kstest(x, dn, args=param)
...
I'm not sure, if my way is right, because there are so many functions I couldn't find equivalent Python functions to them.

댓글을 달려면 로그인하십시오.

### 답변(1개)

Mayank 2021년 10월 15일
편집: Mayank 2021년 10월 15일
The major strength of python is currently is libraries. You need not rewrite this function as there is a library available for this purpose.
Please check this article. The library (`fitter`) source code is available at github.

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!