質問するログイン新規登録
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

1回答

503閲覧

(ValueError: setting an array element with a sequence.)の原因と対策方法を教えてください

hk1217_

総合スコア0

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2023/08/30 12:06

編集2023/08/30 14:19

0

0

質問失礼いたします。初歩的な質問で大変恐縮なのですが、お答え頂ければ幸いです。

https://aiacademy.jp/texts/show/?id=164
こちらのサイトを参考にプログラムしているのですが、検索してもエラーの原因がわかりませんでした。
原因・対策を教えてください。

発生している問題・エラーメッセージ

Traceback (most recent call last): File "c:\Users\----\Desktop\SS\createNPY.py", line 77, in <module> np.save("./dataset.npy", xy) File "<__array_function__ internals>", line 200, in save File "C:\Users\----\AppData\Roaming\Python\Python311\site-packages\numpy\lib\npyio.py", line 521, in save arr = np.asanyarray(arr) ^^^^^^^^^^^^^^^^^^ ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (4,) + inhomogeneous part.

該当のソースコード

Python

1from PIL import Image 2import os, glob 3import numpy as np 4from PIL import ImageFile 5# IOError: image file is truncated (0 bytes not processed)回避のため 6ImageFile.LOAD_TRUNCATED_IMAGES = True 7 8# indexを教師ラベルとして割り当てるため、0にはdogを指定し、1には猫を指定 9classes = ["circle", "square"] 10num_classes = len(classes) 11image_size = 64 12num_testdata = 25 13 14X_train = [] 15X_test = [] 16y_train = [] 17y_test = [] 18 19for index, classlabel in enumerate(classes): 20 photos_dir = "./data/" + classlabel 21 files = glob.glob(photos_dir + "/*.jpg") 22 for i, file in enumerate(files): 23 image = Image.open(file) 24 image = image.convert("RGB") 25 image = image.resize((image_size, image_size)) 26 data = np.asarray(image) 27 if i < num_testdata: 28 X_test.append(data) 29 y_test.append(index) 30 else: 31 32 # angleに代入される値 33 # -20 34 # -15 35 # -10 36 # -5 37 # 0 38 # 5 39 # 10 40 # 15 41 # 画像を5度ずつ回転 42 for angle in range(-20, 20, 5): 43 44 img_r = image.rotate(angle) 45 data = np.asarray(img_r) 46 X_train.append(data) 47 y_train.append(index) 48 # FLIP_LEFT_RIGHT は 左右反転 49 img_trains = img_r.transpose(Image.FLIP_LEFT_RIGHT) 50 data = np.asarray(img_trains) 51 X_train.append(data) 52 y_train.append(index) # indexを教師ラベルとして割り当てるため、0にはdogを指定し、1には猫を指定 53 54X_train = np.array(X_train) 55X_test = np.array(X_test) 56y_train = np.array(y_train) 57y_test = np.array(y_test) 58 59""" 60print(X_train.ndim) 61print(X_test.ndim) 62print(y_train.ndim) 63print(y_test.ndim) 64 65print(X_train.size) 66print(X_test.size) 67print(y_train.size) 68print(y_test.size) 69 70print(X_train.shape) 71print(X_test.shape) 72print(y_train.shape) 73print(y_test.shape) 74""" 75 76xy = (X_train, X_test, y_train, y_test) 77np.save("./dataset.npy", xy)

試したこと

下のコードを入れて次元と要素数を取得しました

Python

1print(X_train.ndim) 2print(X_test.ndim) 3print(y_train.ndim) 4print(y_test.ndim) 5 6print(X_train.size) 7print(X_test.size) 8print(y_train.size) 9print(y_test.size) 10 11print(X_train.shape) 12print(X_test.shape) 13print(y_train.shape) 14print(y_test.shape)

結果↓

4 4 1 1 3922329600 614400 319200 50 (319200, 64, 64, 3) (50, 64, 64, 3) (319200,) (50,)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

meg_

2023/08/30 13:25

エラーメッセージには「File "c:\Users\----\Desktop\SS\createNPY.py", line 77, in <module>」とありますが、該当のソースコードは60行までしかありません。該当のソースコードを実行しても同じエラーが発生しますか?
hk1217_

2023/08/30 14:18

すいません! print(X_train.ndim) print(X_test.ndim) print(y_train.ndim) print(y_test.ndim) print(X_train.size) print(X_test.size) print(y_train.size) print(y_test.size) print(X_train.shape) print(X_test.shape) print(y_train.shape) print(y_test.shape) このコードをコメントアウトしていたので行数に狂いがありました
hk1217_

2023/08/30 14:19

更新させていただきました
meg_

2023/08/30 15:04

下記コードは成功しますか? np.save("./test.npy", X_train)
guest

回答1

0

参考サイトのコードでsaveを使っているのは解せませんが、かわりにsavez_compressedだと複数のshapeの異なるarrayを圧縮保存できます。
読み込みも以下のような感じでloadできます。
参考:numpy.savez_compressed:複数のNumPy配列を圧縮&バイナリ保存

Python

1from PIL import Image 2import numpy as np 3 4 5def disp(*xy): 6 for e in xy: 7 print(e.shape) 8 9image_size = 64 10 11X_train = [] 12X_test = [] 13y_train = [] 14y_test = [] 15 16image = Image.new('RGB',(image_size,image_size)) 17data = np.asarray(image) 18 19for i in range(2): 20 X_test.append(data) 21 y_test.append(0) 22 23for i in range(10): 24 X_train.append(data) 25 y_train.append(0) 26 27X_train = np.array(X_train) 28X_test = np.array(X_test) 29y_train = np.array(y_train) 30y_test = np.array(y_test) 31 32xy = (X_train, X_test, y_train, y_test) 33disp(X_train, X_test, y_train, y_test) 34 35np.savez_compressed('ret.npz', *xy) 36 37# 読み込み 38loaded = np.load('ret.npz') 39X_train, X_test, y_train, y_test = loaded.values() 40disp(X_train, X_test, y_train, y_test) 41""" 42(10, 64, 64, 3) 43(2, 64, 64, 3) 44(10,) 45(2,) 46"""

投稿2023/08/31 01:29

can110

総合スコア38350

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.30%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問