numpy或scipy中是否有任何函數對於給定的pdf,從該分佈採樣的點將返回?Numpy採樣函數
例如,我有高斯分佈的混合物:
means = 0., 8.
stdevs = 1.0,1.0
weights = 0.75,0.25
pdfs = [p * norm.pdf(x, mu, sd) for mu, sd, p in zip(means, stdevs, weights)]
感謝
numpy或scipy中是否有任何函數對於給定的pdf,從該分佈採樣的點將返回?Numpy採樣函數
例如,我有高斯分佈的混合物:
means = 0., 8.
stdevs = 1.0,1.0
weights = 0.75,0.25
pdfs = [p * norm.pdf(x, mu, sd) for mu, sd, p in zip(means, stdevs, weights)]
感謝
你可以用numpy解決這個問題
import numpy as np
x_vals = np.random.normal(0., 1.0, 100)
y_vals = np.random.normal(8., 1.0, 100)
x_norm = [*x_vals, *y_vals]
如果要合併多個pdf,以便仍然可以通過x
,則需要創建一個新函數。做到這一點的一種方法是創建一個函數返回一個函數與高斯的加權和。
from scipy.stats import norm
import numpy as np
def create_mixed_pdf(means, stdevs, weights):
# this uses a lambda function to return the weighted sum of the pdfs at x
return lambda x: sum([p*norm(mu, sd).pdf(x) for mu, sd, p in zip(means, stdevs, weights)])
# this sets `my_pdf` to be the combined pdf distributions
my_pdf = create_mixed_pdf(means, stdevs, weights)
# testing:
import matplotlib.pyplot as plt
x = np.arange(-4, 12, .05)
plt.plot(x, my_pdf(x))
plt.show()
什麼是*之前x_vals和y_vals意味着在那裏?順便說一下,它是高斯混合而不是多元高斯。 –