紹介された動画のコーディング作業画像を良く見ると、ほんの一瞬ですが該当部分のインデントを減らしてforループの外に移動させていました。
19:00・20:26付近や21:52以後など
なので、以下の部分のインデントを解除してforループの外に移動すれば動作するでしょう。
x_train = np.array(x_train) / 255.0
y_train = np.array(y_train)
x_test = np.array(x_test) / 255.0
y_test = np.array(y_test)
単純にこちらのようにする。
x_train = np.array(x_train) / 255.0
y_train = np.array(y_train)
x_test = np.array(x_test) / 255.0
y_test = np.array(y_test)
以下は動画が紹介される前のソースコードだけで推測した内容です。
一応何か別の現象が起こった時などのための注意事項として残しておきます。
お手本の記事へのリンクとかソースコードの情報が追加されるかどうか分かりませんが、今のソースコードからすると、問題の原因は以下のコードによりPythonの基本的なリストとして定義された変数がnumpy.ndarrayに変換されてしまったことですね。
例えばこちらの記事と類似の現象です。
'numpy.ndarray' object has no attribute 'append'のエラーについて
リストとしての宣言と初期化
x_train = []
y_train = []
x_test = []
y_test = []
forループでの処理でndarrayに変換される
x_train = np.array(x_train) / 255.0
y_train = np.array(y_train)
x_test = np.array(x_test) / 255.0
y_test = np.array(y_test)
なので、forループの初回は以下の処理は正常に動作しますが、2回目以後はリストでは無くndarrayになっているのでエラーになります。
if f.split("/")[6]=="train":
x_train.append(img_data)
y_train.append(int(f.split("/")[7].split("_")[0]))
elif f.split("/")[6]=="test":
x_test.append(img_data)
y_test.append(int(f.split("/")[7].split("_")[0]))
最初からndarrayとして宣言・初期化しnp.appendを使うか、ループ後半のリスト全体をndarrayに変換してしまう処理を見直して必要なデータだけndarrayに変換するよう書き変えるか、のどちらかになると思われます。
Answer from kunif on Stack OverflowWeek 2 Exercise 6 "'numpy.ndarray' object has no attribute 'append'"
paddleocr 3.3.1 PPStructureV3 推理 PDF 出现 'numpy.ndarray' object has no attribute 'append'
scikit learn - AttributeError: 'numpy.ndarray' object has no attribute 'columns' - Data Science Stack Exchange
AttributeError: 'numpy.ndarray' object has no attribute 'append'
Videos
Use numpy.concatenate(list1 , list2) or numpy.append()
Look into the thread at Concatenate a NumPy array to another NumPy array.
Basically, numpy arrays don't have have access to append(). If we look into documentation, we need to use np.append(array), where array is the values to be appended.
import numpy as np
arr = np.array([1,2,3,47,1,0,2])
np.append(arr, 4)
The problem is that train_test_split(X, y, ...) returns numpy arrays and not pandas dataframes. Numpy arrays have no attribute named columns
If you want to see what features SelectFromModel kept, you need to substitute X_train (which is a numpy.array) with X which is a pandas.DataFrame.
selected_feat= X.columns[(sel.get_support())]
This will return a list of the columns kept by the feature selector.
If you wanted to see how many features were kept you can just run this:
sel.get_support().sum() # by default this will count 'True' as 1 and 'False' as 0
because this :
X = df.iloc[:,:24481].values
y = df.iloc[:, -1].values
you should remove .values or make extra X_col, y_col like that
X_col = df.iloc[:,:24481]
y_col = df.iloc[:, -1]
I need help. This is the code which gives error:
import numpy as np
num = 3
X_new = np.array([])
for i in range(1, num+1):
print('Enter the', i, '. data:')
n = float(input())
X_new.append(n)
print('\n', X_new)
And this is the error:
AttributeError: 'numpy.ndarray' object has no attribute 'append'