0

I am running a loop.

The formula is reading an iterable object as a string.

dist_names = ['Lognorm','Weibull']

for i in dist_names:
    param = scipy.stats.i.fit(data)

I want the formula above to be 'scipy.stats.Lognorm.fit(data)'.

How can I make the 'i' be read as 'Lognorm'

Thanks!

Robert
  • 19
  • 2
  • Something like `getattr(scipy.stats, i)().fit(data)` should work - but you would want to have lower-case "lognorm" rather than "Lognorm": https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.lognorm.html – slothrop May 27 '23 at 15:24
  • See: https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string – slothrop May 27 '23 at 15:27
  • the getattr has the same issue. param = scipy.stats.getattr(scipy.stats, i)().fit(data) results in the same error. module 'scipy.stats' has no attribute 'getattr' thanks for the quick reply – Robert May 27 '23 at 15:31
  • 1
    Can you try the code in my original comment? Not `scipy.stats.getattr...` but `getattr(scipy.stats, ... ` – slothrop May 27 '23 at 15:32
  • <<>> results in the error _parse_args() missing 2 required positional arguments: 'a' and 'c' – Robert May 27 '23 at 15:36
  • Right, so it looks like your particular distribution needs parameters when instantiating it - you can see a wide range of them here https://stackoverflow.com/questions/37559470/what-do-all-the-distributions-available-in-scipy-stats-look-like. For example if `i` is `'alpha'` then you'd want to be doing something like `getattr(scipy.stats, i)(a=3.57, loc=0.00, scale=1.00).fit(data)`. To be honest, the variation in parameters between different distributions makes a generic loop approach like this a bit tricky, unless you know that all your dists use the same named parameters. – slothrop May 27 '23 at 15:42
  • hmm thanks but i don't think this approach will work. I might just need to do it manually unfortunately. I thought there was some way to use 'inter' but i don't know. – Robert May 27 '23 at 15:48
  • I figured out what you meant, thanks!!! – Robert May 28 '23 at 13:11

0 Answers0