2016-11-29 6 views
1

sklearn.model_selectionを使用して列車とテストセットを生成したいと思うサイズの異なるjpgイメージのフォルダがあります.train_test_split()。
これは、これまでの私のコードです:異なるサイズのjpgをnumpy.arrayにロードしています - ValueError:サンプル数が一致しない入力変数が見つかりました

helper = list() 
y = list() 

for path, subdirs, files in os.walk(inputDir): 
    for s in subdirs: 
     y.append(s) 
    for f in files: 
     img_path = os.path.join(path,f) 
     pixels = Image.open(img_path).getdata() 
     helper.append(pixels) 

x = np.asarray(helper) 

x_train, x_test, y_train, y_test = train_test_split(x,y) #error occurs here 

私は、次のエラーメッセージが表示されます。

File "getTrainTestSet.py", line 57, in getTrainTestSet x_train, x_test, y_train, y_test = train_test_split(x,y)
File "/usr/local/lib/python2.7/dist-packages/sklearn/model_selection/_split.py", line 1689, in train_test_split arrays = indexable(*arrays)
File "/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.py", line 206, in indexable check_consistent_length(*result)
File "/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.py", line 181, in check_consistent_length " samples: %r" % [int(l) for l in lengths])
ValueError: Found input variables with inconsistent numbers of samples: [120, 0]

私はこの問題を解決する助けてください。

ありがとうございます!


編集:私はtrain_test_split()関数との混乱をしない方法でそれを行う方法を考え出し:

y = list() 
helpers = list() 

for path, subdirs, files in os.walk(inputDir): 
    for s in subdirs: 
     files = glob.glob(inputDir+ s + '/*.jpg') 
     helpers.append(np.array([np.array(Image.open(f)) for f in files])) 
     y.append(s) 

x = np.array([np.array(h) for h in helpers]) 

x_train, x_test, y_train, y_test = train_test_split(x,y) 

私は問題がlen(y)x.shape[0]が同じでなければならないということだったと思います。最終的なxは4つのサブディレクトリがあり、合計でイメージファイルを持つので、形(4、)を持っています。

あなたのご意見ありがとうございました!

+0

'x'の形状とdtypeは何ですか?私はそれが1dオブジェクト配列であると考えます。さまざまなサイズのテストやトレーニングの画像を扱う方法があるかどうかを調べるために 'sklearn'を研究してください。私は、通常の処理では一貫したサイズ(および多次元配列)が必要であると確信しています。 – hpaulj

+0

x.shape ==(120、)、x.dtype == objectです。 @Def_Osが示唆しているようにnp.atleast_2d(x)を使用すると、形状は(1,120)であり、dtypeはオブジェクトのままです。しかし、2次元配列でも、私はまだValueErrorを取得しています(下記参照)。私はウェブで解決策を探していますが、残念ながらサイズの異なる画像を扱う方法はまだありません。 – hsvar

+0

このコードは、すべて同じサイズの一連の画像でテストします。 – hpaulj

答えて

0

xは、サイズ[no_of_samples、no_of_features]の2次元配列でなければなりません。これを行う:

x = np.atleast_2d(x).T 
+0

私はこれを試しましたが、私はまだValueErrorを取得しました:サンプル数の不一致で入力変数が見つかりました:[1、0] – hsvar

+0

これを転置してみてください: 'x = np.atleast_2d(x).T' –

+0

これはうまくいかなかった。あなたのご意見ありがとうございます!私は今それを理解した。私は私のために働いた解決策で私の質問を編集しています。 – hsvar

関連する問題