質問概要
機械学習初心者です。
Pythonを用いて、画像を自動分類したいのですが、(恐らく)学習フェーズで以下のエラーが出ており、先に進めません。
このエラーの理由をご教示願います。
array = np.array(array, dtype=dtype, order=order, copy=copy) ValueError: setting an array element with a sequence.
質問
機械学習初心者です。
前回、【Python/機械学習】ネットから取得した画像を自動分別したいで、質問させていただきましたが、
どうしてもエラーの内容がわからなかったので、再度質問させていただきます。
Pythonを用いて、画像を分類したいと思っており、しかし機械学習を初めて学習するので、書き方を学ぼうと以下のサイト様のコードをコピペしました。
コピペコードは以下のものです。
lang
1#-*- encoding: utf-8 -*- 2 3from PIL import Image 4import numpy as np 5import os 6import pandas as pd 7import pylab as pl 8from sklearn.decomposition import RandomizedPCA 9from sklearn.externals import joblib 10from sklearn.svm import LinearSVC 11 12 13STANDARD_SIZE = (300, 167) 14 15 16def img_to_matrix(filename, verbose=False): 17 img = Image.open(filename) 18 if verbose: 19 print 'changing size from %s to %s' % (str(img.size), str(STANDARD_SIZE)) 20 img = img.resize(STANDARD_SIZE) 21 imgArray = np.asarray(img) 22 return imgArray # imgArray.shape = (167 x 300 x 3) 23 24 25def flatten_image(img): 26 s = img.shape[0] * img.shape[1] * img.shape[2] 27 img_wide = img.reshape(1, s) 28 return img_wide[0] 29 30 31def main(): 32 img_dir = 'images/' 33 images = [img_dir + f for f in os.listdir(img_dir)] 34 labels = ['architecture' if 'architecture' in f.split('/')[-1] else 'food' for f in images] 35 36 data = [] 37 for image in images: 38 img = img_to_matrix(image) 39 img = flatten_image(img) 40 data.append(img) 41 42 data = np.array(data) 43 44 is_train = np.random.uniform(0, 1, len(data)) <= 0.7 45 y = np.where(np.array(labels) == 'architecture', 1, 0) 46 47 train_x, train_y = data[is_train], y[is_train] 48 49 # plot in 2 dimensions 50 pca = RandomizedPCA(n_components=2) 51 X = pca.fit_transform(data) 52 df = pd.DataFrame({"x": X[:, 0], "y": X[:, 1], 53 "label": np.where(y == 1, 'architecture', 'food')}) 54 colors = ['red', 'yellow'] 55 for label, color in zip(df['label'].unique(), colors): 56 mask = df['label'] == label 57 pl.scatter(df[mask]['x'], df[mask]['y'], c=color, label=label) 58 59 pl.legend() 60 pl.savefig('pca_feature.png') 61 62 # training a classifier 63 pca = RandomizedPCA(n_components=5) 64 train_x = pca.fit_transform(train_x) 65 66 svm = LinearSVC(C=1.0) 67 svm.fit(train_x, train_y) 68 joblib.dump(svm, 'model.pkl') 69 70 # evaluating the model 71 test_x, test_y = data[is_train == False], y[is_train == False] 72 test_x = pca.transform(test_x) 73 print pd.crosstab(test_y, svm.predict(test_x), 74 rownames=['Actual'], colnames=['Predicted']) 75 76if __name__ == '__main__': 77 main()
ここで、このコードを眺めていると、
images
というフォルダの中に、
architecture_
food_
という名前から始まる画像を予め入れておき学習させるようだったので、
とりあえず適当に画像を上記の2つになるようにリネームし、images
フォルダに入れて、Pythonコードをターミナル上から実行しました。
すると、どうも
X = pca.fit_transform(data)
の行でエラーが出ているらしく、
array = np.array(array, dtype=dtype, order=order, copy=copy) ValueError: setting an array element with a sequence.
のようなエラー内容で返ってきました。
変数data
の中身をprint
しても、しっかり値は入っているようですし、
変数labels
の中身をprint
してもしっかり値は入っておりました。
これは何がいけないのでしょうか?
このエラー内容でググって見ても、複素数がうんたらという内容が出てきましたが、
今回は複素数は使っていないように思われます。
初心者過ぎて申し訳ないのですが、解決策などご教示いただければ幸いです。
よろしくお願いいたします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。