0

dataは、データの1次元配列です。ガウス混合モデルを単一フィーチャデータに適合させる正しい方法は何ですか?

data = [0.0, 7000.0, 0.0, 7000.0, -400.0, 0.0, 7000.0, -400.0, -7400.0, 7000.0, -400.0, -7000.0, -7000.0, 0.0, 0.0, 0.0, -7000.0, 7000.0, 7000.0, 7000.0, 0.0, -7000.0, 6600.0, -7400.0, -400.0, 6600.0, -400.0, -400.0, 6600.0, 6600.0, 6600.0, 7000.0, 6600.0, -7000.0, 0.0, 0.0, -7000.0, -7400.0, 6600.0, -400.0, 7000.0, -7000.0, -7000.0, 0.0, 0.0, -400.0, -7000.0, -7000.0, 7000.0, 7000.0, 0.0, -7000.0, 0.0, 0.0, 6600.0, 6600.0, 6600.0, -7400.0, -400.0, -2000.0, -7000.0, -400.0, -7400.0, 7000.0, 0.0, -7000.0, -7000.0, 0.0, -400.0, -7400.0, -7400.0, 0.0, 0.0, 0.0, -400.0, -400.0, -400.0, -400.0, 6600.0, 0.0, -400.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -400.0, -400.0, 0.0, 0.0, -400.0, -400.0, 0.0, -400.0, 0.0, -400.0] 

私はこのデータにいくつかのガウス分布を当てはめてプロットしたいと思います。

私は

import numpy as np 
from sklearn import mixture 

x = np.array(data) 
clf = mixture.GaussianMixture(n_components=2, covariance_type='full') 
clf.fit(x) 

を実行した場合、私は[OK]を...私はこれと一緒に暮らすことができ、エラーに

ValueError: Expected n_samples >= n_components but got n_components = 2, n_samples = 1 

DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample. 

を取得します。警告は私に何をすべきかを伝えます。私は

x = np.array(data).reshape(-1,1) 
clf = mixture.GaussianMixture(n_components=2, covariance_type='full') 
clf.fit(x) 

を実行する場合しかし、私はエラー

ValueError: Expected the input data X have 1 features, but got 32000 features 

は私が間違って何をやってもらいますか?正しい方法は何ですか?

編集:私はちょうど私がエラーメッセージを読み違えることに気づい

fit()ではエラーが発生していませんが、score_samples()です。

私は後でガウス分布をプロットしようとしています。

x = np.linspace(-8000,8000,32000) 
y = clf.score_samples(x) 

plt.plot(x, y) 
plt.show() 

従ってxが問題であるようです。しかし、いずれもx.reshape(-1,1)は役に立ちません。x.reshape(1,-1)

+2

は、あなたはそれを他の方法(1、-1)再構築しようとしましたか? –

+0

はい、私はすでにこれを試しました。 John Moutafisへの私のコメントを参照してください。 –

+1

(-1,1)、scikit 0.18 –

答えて

4

のリストに変換します

your_samples_list = map(lambda x:[x], your_samples_list) 

してみてください。私の編集で述べたように、fit()ではエラーが発生していませんでしたが、score_samples()です。

両方の関数が多次元配列を認識します。コードの作業

data = np.array(data).reshape(-1,1) 
clf = mixture.GaussianMixture(n_components=1, covariance_type='full') 
clf.fit(data) 

x = np.array(np.linspace(-8000,8000,32000)).reshape(-1,1) 
y = clf.score_samples(x) 

plt.plot(x, y) 
plt.show() 
+0

あなたはそれを見つけました:)私は私の答えを編集したように形を変えるときにエラーが発生していません! –

2

あなただけの一つの特徴の多くのサンプルを持っている場合、これは私がエラーを自分で見つけリスト

[a,b,c] -> [[a],[b],[c]] 
関連する問題