簡體   English   中英

從高斯混合模型python采樣數據點

[英]Sampling data points from a Gaussian Mixture Model python

我真的是python和GMM的新手。 我最近學習了GMM,並嘗試從此處實施代碼

運行gmm.sample()方法時遇到了一些問題:

gmm16 = GaussianMixture(n_components=16, covariance_type='full', random_state=0)    
Xnew = gmm16.sample(400,random_state=42)
plt.scatter(Xnew[:, 0], Xnew[:, 1])

錯誤顯示:

TypeError: sample() got an unexpected keyword argument 'random_state'

我檢查了最新的文檔,發現方法樣本應該只包含n,它表示要生成的樣本數。 但是當我刪除“ random_state = 42”時,出現新錯誤:

代碼:

Xnew = gmm16.sample(400)
plt.scatter(Xnew[:, 0], Xnew[:, 1])

錯誤:

TypeError: tuple indices must be integers or slices, not tuple

當您實現Jake VanderPlas的代碼時,有人會遇到這個問題嗎? 我該怎么辦呢?

我的Jupyter:

筆記本服務器的版本為:5.7.4

Python 3.7.1(默認,2018年12月14日,13:28:58)

輸入“版權”,“信用”或“許可證”以獲取更多信息

IPython 7.2.0-增強的交互式Python 輸入“?” 求助。

您的問題在於將數據饋送到散點圖的方式。 特別是在元組中有一個numpy數組,並且索引的方式不正確。 嘗試這個。

plt.scatter(Xnew[0][:,0], Xnew[0][:,1])

基本上,我們擁有的是第一個索引Xnew[0]將指向您想要的元組中的元素(numpy數組),第二個將根據需要對其進行切片。 [:,1]在這里,我們獲取所有行和第二列。

因為sample方法返回一個tuple ,所以您收到TypeError ,請參見此處

這應該做的工作:

Xnew, Ynew = gmm16.sample(400)  # if Ynew is valuable
plt.scatter(Xnew[:, 0], Xnew[:, 1])

要么

Xnew, _ = gmm16.sample(400)  # if Ynew isn't valuable
plt.scatter(Xnew[:, 0], Xnew[:, 1])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM