Getting the parameter names of scipy.stats distributions

2024/11/15 5:45:34

I am writing a script to find the best-fitting distribution over a dataset using scipy.stats. I first have a list of distribution names, over which I iterate:

dists = ['alpha', 'anglit', 'arcsine', 'beta', 'betaprime', 'bradford', 'norm']
for d in dists:dist = getattr(scipy.stats, d)ps = dist.fit(selected_data)errors.loc[d,['D-Value','P-Value']] = kstest(selected.tolist(), d, args=ps)errors.loc[d,'Params'] = ps

Now, after this loop, I select the minimum D-Value in order to get the best fitting distribution. Now, each distribution returns a specific set of parameters in ps, each with their names and so on (for instance, for 'alpha' it would be alpha, whereas for 'norm' they would be mean and std).

Is there a way to get the names of the estimated parameters in scipy.stats?

Thank you in advance

Answer

Warren Weckesser and I have developed a more robust solution:

import sys
import scipy.statsdef list_parameters(distribution):"""List parameters for scipy.stats.distribution.# Argumentsdistribution: a string or scipy.stats distribution object.# ReturnsA list of distribution parameter strings."""if isinstance(distribution, str):distribution = getattr(scipy.stats, distribution)if distribution.shapes:parameters = [name.strip() for name in distribution.shapes.split(',')]else:parameters = []if distribution.name in scipy.stats._discrete_distns._distn_names:parameters += ['loc']elif distribution.name in scipy.stats._continuous_distns._distn_names:parameters += ['loc', 'scale']else:sys.exit("Distribution name not found in discrete or continuous lists.")return parameters

The discussion can be found here.

https://en.xdnf.cn/q/71485.html

Related Q&A

Does Python 3 gzip closes the fileobj?

The gzip docs for Python 3 states thatCalling a GzipFile object’s close() method does not close fileobj, since you might wish to append more material after the compressed dataDoes this mean that the g…

pip stopped working after upgrading anaconda v4.4 to v5.0

I ran the command conda update anaconda to update anaconda v4.4 to v5.0After anaconda was successfully upgraded to v5.0, I had problems running pip.This is the error output I see after running pip;Trac…

Python Django- How do I read a file from an input file tag?

I dont want the file to be saved on my server, I just want the file to be read and printed out in the next page. Right now I have this.(index.html)<form name="fileUpload" method="post…

ImportError: cannot import name AutoModelWithLMHead from transformers

This is literally all the code that I am trying to run: from transformers import AutoModelWithLMHead, AutoTokenizer import torchtokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small&…

UnicodeEncodeError: ascii codec cant encode characters in position 0-6: ordinal not in range(128)

Ιve tried all the solution that I could find, but nothing seems to work: teext = str(self.tableWidget.item(row, col).text())Im writing in greek by the way...

selenium PhantomJS send_keys doesnt work

I am using selenium and PhantomJS for testing. I followed Seleniums simple usage, but send_keys doesnt work on PhantomJS, it works on Firefox. Why? I have to use button.click() instead?#!/usr/bin/pyt…

Replace values in column of Pandas DataFrame using a Series lookup table

I want to replace a column of values in a DataFrame with a more accurate/complete set of values generated by a look-up table in the form of a Series that I have prepared.I thought I could do it this wa…

Behavior of round function in Python

Could anyone explain me this pice of code:>>> round(0.45, 1) 0.5 >>> round(1.45, 1) 1.4 >>> round(2.45, 1) 2.5 >>> round(3.45, 1) 3.5 >>> round(4.45, 1) 4.5…

Pygame application runs slower on Mac than on PC

A friend and I are making a game in Python (2.7) with the Pygame module. I have mostly done the art for the game so far and he has mostly done the coding but eventually I plan to help code with him onc…

How to extract feature vector from single image in Pytorch?

I am attempting to understand more about computer vision models, and Im trying to do some exploring of how they work. In an attempt to understand how to interpret feature vectors more Im trying to use …