質問をすることでしか得られない、回答やアドバイスがある。

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

新規登録して質問してみよう
ただいま回答率
85.48%
Jupyter

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

NumPy

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Python

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

Q&A

解決済

1回答

11964閲覧

'numpy.ndarray' object has no attribute 'append'のエラー

maguro2020

総合スコア34

Jupyter

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

NumPy

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Python

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

0グッド

0クリップ

投稿2020/10/18 11:53

#前提・実現したいこと
Variational Autoencoderを使った画像の異常検知 後編 (塩尻MLもくもく会#7)
上記のURLのサイト様のコードを参考にVAEとオリジナルデータセットを用いて異常検知を行いたいと考えております。

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

Python

1------------------------------------------------------------------------- 2AttributeError Traceback (most recent call last) 3<ipython-input-15-3088cb233051> in <module> 4 2 if y_train[i] == 1:#スニーカーは7 5 3 temp = x_train[i,:,:,:] 6----> 4 x_train_b.append(temp.reshape((x_train_shape[1],x_train_shape[2],x_train_shape[3]))) 7 5 8 6 x_train_b = np.array(x_train_b) 9 10AttributeError: 'numpy.ndarray' object has no attribute 'append'

コードの途中で上記のようなエラーが発生してしまい、どのようにコードを改良すればいいのかわからない状態です。

#コード

Python

1from __future__ import absolute_import 2from __future__ import division 3from __future__ import print_function 4 5from keras.layers import Lambda, Input, Dense, Reshape 6from keras.models import Model 7from keras.losses import mse 8from keras.utils import plot_model 9from keras import backend as K 10from keras.layers import BatchNormalization, Activation, Flatten 11from keras.layers.convolutional import Conv2DTranspose, Conv2D 12 13import numpy as np 14import matplotlib.pyplot as plt 15import matplotlib.colors as colors 16import os 17from sklearn import metrics 18os.chdir('/Users/user_name/desktop/VAE') 19os.getcwd() 20 21def result_score(model, x, name, height=80, width=80, move=2): 22 score = [] 23 24 for k in range(len(x)): 25 max_score = -1000000000 26 if k%100 == 0: 27 print(k) 28 29 for i in range(int((x.shape[1]-height)/move)+1): 30 for j in range(int((x.shape[2]-width)/move)+1): 31 x_sub = x[k, i*move:i*move+height, j*move:j*move+width, 0] 32 x_sub = x_sub.reshape(1, height, width, 1) 33 34 #従来手法 35 if name == "old_": 36 #スコア 37 temp_score = model.evaluate(x_sub, batch_size=1, verbose=0) 38 if temp_score > max_score: 39 max_score = temp_score 40 41 #提案手法 42 else: 43 #スコア 44 mu, sigma = model.predict(x_sub, batch_size=1, verbose=0) 45 loss = 0 46 for o in range(height): 47 for l in range(width): 48 loss += 0.5 * (x_sub[0,o,l,0] - mu[0,o,l,0])**2 / sigma[0,o,l,0] 49 if loss > max_score: 50 max_score = loss 51 52 score.append(max_score) 53 54 return(score) 55 56def cut_img(x, number, height=224, width=224): 57 print("cutting images ...") 58 x_out = [] 59 x_shape = x.shape 60 61 for i in range(number): 62 shape_0 = np.random.randint(0,x_shape[0]) 63 shape_1 = np.random.randint(0,x_shape[1]-height) 64 shape_2 = np.random.randint(0,x_shape[2]-width) 65 temp = x[shape_0, shape_1:shape_1+height, shape_2:shape_2+width, 0] 66 x_out.append(temp.reshape((height, width, x_shape[3]))) 67 68 print("Complete.") 69 x_out = np.array(x_out) 70 71 return x_out 72 73# reparameterization trick 74# instead of sampling from Q(z|X), sample eps = N(0,I) 75# z = z_mean + sqrt(var)*eps 76def sampling(args): 77 z_mean, z_log_var = args 78 batch = K.shape(z_mean)[0] 79 dim = K.int_shape(z_mean)[1] 80 # by default, random_normal has mean=0 and std=1.0 81 epsilon = K.random_normal(shape=(batch, dim)) 82 return z_mean + K.exp(0.5 * z_log_var) * epsilon 83 84# dataset 85from bcn_dataset import BCN_Dataset2 86(x_train, y_train), (x_test, y_test) = BCN_Dataset2.create_bcn() 87 88x_train = x_train.reshape(x_train.shape[0], 224, 224, 3) 89x_test = x_test.reshape(x_test.shape[0], 224, 224, 3) 90 91x_train = x_train.astype('float32') / 255 92x_test = x_test.astype('float32') / 255 93 94x_train_b = [] 95x_test_b = [] 96x_test_n = [] 97 98x_train_shape = x_train.shape 99 100for i in range(len(x_train)): 101 if y_train[i] == 1:#スニーカーは7 102 temp = x_train[i,:,:,:] 103 x_train_b.append(temp.reshape((x_train_shape[1],x_train_shape[2],x_train_shape[3]))) 104 105x_train_b = np.array(x_train_b) 106x_train_b = cut_img(x_train_b, 50) 107print("train data:",len(x_train_b))

#試していること
1.'numpy.ndarray' object has no attribute 'append'のエラーについて

2.特徴量データ設定の際のnp.arrayとlistの扱い 決定木の可視化の際のエラー'numpy.ndarray' object has no attribute 'colum

現在、上記URLに似たようなエラーメッセージでの質問をされている方がおり、なんとか自分の問題も解決できないかとコードと回答の意味の解読をしておりますが、自分のコードをどのように改良すればいいのかわからなく頭を抱えております。アドバイスやご助言でもいただけたら幸いです。

#補足
使っているPCはmacOS Catalina バージョン10.15.5
Pythonのバージョンは3.6.5です
Jupiter Notebookを使用しています

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

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

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

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

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

guest

回答1

0

ベストアンサー

ndarrayappend()メソッドは在りませんが、numpy.append()なら在ります。
使いたかったのはこちらでしょうか?

numpy.append

投稿2020/10/18 12:17

meg_

総合スコア10580

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

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

maguro2020

2020/10/18 13:25

ご回答いただきありがとうございます。meg_様。 自分の解釈が間違っていたら大変申し訳ないのですが、コードの一番下から4行目にあたる ``` x_train_b.append(temp.reshape((x_train_shape[1],x_train_shape[2],x_train_shape[3]))) ``` append()をnumpy.append()に変更すればよろしいのでしょうか?もしくはコード内にあるappend()を全てnumpy.append()に変更する必要があるのでしょうか?
meg_

2020/10/18 13:34

> x_train_b.append(temp.reshape((x_train_shape[1],x_train_shape[2],x_train_shape[3]))) 上記であれば、numpy.append(x_train_b, temp.reshape((x_train_shape[1],x_train_shape[2],x_train_shape[3])))になるかと思いますが、他の部分は分かりません。appendメソッド自体はlistなど他にもありますので。 あくまで「AttributeError: 'numpy.ndarray' object has no attribute 'append'」エラーに対しての回答となります。
maguro2020

2020/10/18 13:47

ご回答いただきありがとうございます。meg_様。 もう少し自分でコードをいじってみたいと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問