简体   繁体   English

我可以将预训练的 pdf 函数传递给 seaborn.distplot 吗?

[英]Can I pass a pretrained pdf function into seaborn.distplot?

I am aware that you can use seaborn.distplot to graph data as a histogram and superimpose a distribution on top of it.我知道您可以使用 seaborn.distplot 将数据绘制为直方图并在其上叠加分布。 I'm aware of a parameter that allows you to pass in a pdf function to do so.我知道有一个参数允许您传入 pdf 函数来执行此操作。 In the source code, it looks like it internally calls fit() to do the training.在源代码中,它看起来像是在内部调用 fit() 来进行训练。 I was wondering if there was a way to pre-train the model, and just use it.我想知道是否有一种方法可以预训练模型,然后直接使用它。

I have tried using lambda functions representing my distribution, but I kept getting errors.我曾尝试使用代表我的分布的 lambda 函数,但我不断收到错误消息。 I have also tried passing parameters into seaborn.distplot to help train with the settings I wanted, but that didn't work either.我还尝试将参数传递到 seaborn.distplot 以帮助训练我想要的设置,但这也不起作用。

Method 1 - Using a lambda for the pretrained model:方法 1 - 对预训练模型使用 lambda:

import seaborn as sns
from scipy import stats

params = stats.exponweib.fit(data, floc=0, f0=1)
custom_weib = lambda x: stats.exponweib.pdf(x, *params)
sns.distplot(data, bins=bin_count, fit=custom_weib, norm_hist=True, kde=False, hist_kws={'log':True})

I'm seeing this error message: AttributeError: 'function' object has no attribute 'fit' ^ It can't take a pre-trained model.我看到此错误消息:AttributeError: 'function' object has no attribute 'fit' ^ It can't take a pre-trained model。

Method 2 - Attempted to pass parameters as part of the fit method.方法 2 - 尝试将参数作为 fit 方法的一部分进行传递。 (I don't know if I'm doing this correctly.) (我不知道我这样做是否正确。)

import seaborn as sns
from scipy import stats

sns.distplot(data, bins=bin_count, norm_hist=True, kde=False, hist_kws=hist_kws, fit=stats.exponweib, floc=0, f0=1)

I get this exception: TypeError: distplot() got an unexpected keyword argument 'floc' ^ It's obvious that I'm not passing in the variables correctly, but I don't know how.我收到此异常:TypeError: distplot() 得到了一个意外的关键字参数 'floc' ^ 很明显我没有正确传递变量,但我不知道如何传递。

Here's a link to the Seaborn source code if you need it: https://github.com/mwaskom/seaborn/blob/master/seaborn/distributions.py如果需要,这里是 Seaborn 源代码的链接: https : //github.com/mwaskom/seaborn/blob/master/seaborn/distributions.py

In principle it's not possible to supply any parameters to seaborn's fit .原则上不可能为 seaborn 的fit提供任何参数。 This is due to the line params = fit.fit(a) in the source code.这是由于源代码中的params = fit.fit(a)

However, it looks like you can trick seaborn by supplying an object that provides a fit() and a pdf() method and modify the arguments within this object.但是,看起来您可以通过提供一个提供fit()pdf()方法的对象并修改该对象中的参数来欺骗 seaborn。

import numpy as np
from scipy.stats import exponweib
import matplotlib.pyplot as plt
import seaborn as sns

fig, ax = plt.subplots(1, 1)

class MyDist():
    def __init__(self, **kw):
        self.dist = exponweib
        self.kw = kw

    def fit(self, data):
        return self.dist.fit(data, **self.kw)

    def pdf(self, data, *args, **kw):
        return self.dist.pdf(data, *args, **kw)


r = exponweib.rvs(3, 2, loc=0.3, scale=1.3, size=100000)

sns.distplot(r, fit=MyDist(floc=0.3, fscale=1.3), norm_hist=True, kde=False)


params = exponweib.fit(r, floc=0.3, fscale=1.3)
x = np.linspace(0.1, 4.1, 100)
ax.plot(x, exponweib.pdf(x, *params),
        'r-', lw=3, alpha=0.6)


plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM