背景
我的情緒分析研究涉及各種資料集。最近我遇到了一個資料集,不知何故我無法成功訓練。.CSV我主要使用檔案格式的開放資料,Pandas因此NumPy被大量使用。
在我的研究中,其中一種方法是嘗試集成自動化機器學習(AutoML),而我選擇使用的庫是Auto-Keras,主要是利用它的TextClassifier()包裝函式來實作AutoML。
主要問題
我已經通過官方檔案驗證TextClassifier()了 NumPy 陣列格式的資料。但是,當我將資料加載到需要訓練的列中Pandas DataFrame并在這些列上使用時,仍然顯示以下錯誤:.to_numpy()
ValueError Traceback (most recent call last)
<ipython-input-13-1444bf2a605c> in <module>()
16 clf = ak.TextClassifier(overwrite=True, max_trials=2)
17
---> 18 clf.fit(x_train, y_train, epochs=3, callbacks=cbs)
19
20
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float).
錯誤相關的代碼扇區
Pandas DataFrame我使用 洗掉不需要的列的扇區,并使用提供的函式.drop()將所需的列轉換為NumPyArray 。to_numpy()Pandas
df_src = pd.read_csv(get_data)
df_src = df_src.drop(columns=["Name", "Cast", "Plot", "Direction",
"Soundtrack", "Acting", "Cinematography"])
df_src = df_src.reset_index(drop=True)
X = df_src["Review"].to_numpy()
Y = df_src["Overall Sentiment"].to_numpy()
print(X, "\n")
print("\n", Y)
主要錯誤代碼部分,我在其中執行并StratifedKFold()同時TextClassifier()用于訓練和測驗模型。
fold = 0
for train, test in skf.split(X, Y):
fold = 1
print(f"Fold #{fold}\n")
x_train = X[train]
y_train = Y[train]
x_test = X[test]
y_test = Y[test]
cbs = [tf.keras.callbacks.EarlyStopping(patience=3)]
clf = ak.TextClassifier(overwrite=True, max_trials=2)
# The line where it indicated the error.
clf.fit(x_train, y_train, epochs=3, callbacks=cbs)
pred = clf.predict(x_test) # result data type is in lists of `string`
ceval = clf.evaluate(x_test, y_test)
metrics_test = metrics.classification_report(y_test, np.array(list(pred), dtype=int))
print(metrics_test, "\n")
print(f"Fold #{fold} finished\n")
補充
我正在分享與錯誤相關的完整代碼Google Colab,您可以在此處幫助我進行診斷。
編輯筆記
我已經嘗試了潛在的解決方案,例如:
x_train = np.asarray(x_train).astype(np.float32)
y_train = np.asarray(y_train).astype(np.float32)
或者
x_train = tf.data.Dataset.from_tensor_slices((x_train,))
y_train = tf.data.Dataset.from_tensor_slices((y_train,))
但是,問題仍然存在。
uj5u.com熱心網友回復:
其中一個字串等于nan。只需洗掉此條目和相應的標簽。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/471438.html
上一篇:NumpyArray沒有將陣列的所有值復制到csv檔案
下一篇:3d和1d陣列的乘積和求和
