我正在嘗試做一個假新聞分類模型,所以作為預處理的一部分,我做了停用詞刪除、詞幹提取和詞形還原。之後我使用 Doc2Vec 將文本轉換為向量。x 和 y 是 FER2013 數據集的矢量化文本和標題列,看起來像這樣:
array(['[-0.78902 -1.3584563 -1.2094668 1.2671869 -0.505878 -0.46793452 -0.05335827 -0.3953245 0.64238095 0.29936427 0.64882624 -0.9903825 -0.16372935 -0.5240088 -0.8061704 0.94803816 0.49816552 1.0211618 -0.3891333 -0.53825814]',
'[ 1.3473806 -2.2537904 -0.01150447 1.0409713 -0.17815335 -0.92815816 0.01454325 -1.78424 -1.2680324 0.39823842 -0.3313817 -0.7288629 -2.3424263 0.50622666 -0.1096359 0.6213235 -0.18285985 1.0153825 0.12197232 -0.31061244]',
'[-0.21220133 -0.94656384 -0.31035122 -0.20017953 -0.04308065 -0.2723616 0.13090962 -0.51174223 -0.63989 0.9296897 -1.516201 0.90435594 0.19001946 -0.60641354 1.2848449 -0.9736119 0.05583194 -0.55788654 -1.2701703 1.361641 ]',
...,
'[ 0.07217433 -0.01937062 -0.6813549 0.9267837 0.8567494 0.11055075 -0.49166957 0.05391011 -0.4867952 0.68353873 -0.34195983 0.11552304 0.07995445 -0.68140924 0.14169812 0.05574211 0.85227823 0.28077438 0.08299595 -0.93785906]',
'[-0.03407184 1.0156128 -0.3216298 0.38381922 0.41431156 -0.22601238 -0.45248717 -0.918599 -0.16802132 0.1707096 -0.9931418 -0.62819743 -0.22415633 -2.451051 -0.26168516 -0.04228298 0.71497554 -0.27817437 -1.0023195 -0.43352002]',
'[-0.60217994 0.1949403 0.29088852 -0.5941371 0.5181151 -0.789163 0.887305 -1.6515299 -0.8250909 0.11755247 0.08880343 -0.23532224 0.52777374 0.31153452 -0.39013034 -0.08209435 1.7401134 1.4870292 -0.18821365 1.1624134 ]'],
dtype=object)
這是我用於訓練測試拆分和擬合的代碼:
xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.2, random_state=42)
model = MultinomialNB()
model.fit(xtrain, ytrain)
print(model.score(xtest, ytest))
但是在這裡,存在向量元素中的換行問題。因此,結果,當我將其轉換為 np 數組以適合模型時,最後生成了一個“\n”字符。我用空值替換了 "\n" 來解決這個問題,但同樣的錯誤仍然存在。
ValueError: could not convert string to float: '[ 0.3060216 0.04662842 0.44954425 1.4223956 1.3165258 0.7194798 1.2665545 -0.4921699 -0.01131658 0.02519435 -0.6978444 -1.2564504 -0.6709047 0.02319291 0.05263066 -0.1006538 1.3066514 -0.9096088 0.3219977 0.31393617]'
當我檢查這些數組的 dtype 時,它顯示為對象,即使在錯誤中,它被指定為字符串。
請從標籤中刪除機器學習,這是一個python錯誤!
錯誤很明顯,您正在嘗試將 String 轉換為 float 並且該 String 包含'[' char 並包含空格......它無法轉換,您需要清理該 String :
def check_float(element) :
try:
float(element)
return(True)
except ValueError :
return False
new_List_floats=[]
for st in that_array :
List_of_string = st.split(" ")
for element in List_of_string :
el = element.split(" ")[0]
el =element.split("]")[0]
el = element.split("[")[0]
if check_float(el) :
new_List_floats +=[float(el)]
現在您可以使用新的 List 來完成您的過程,只需注意數組的形狀並在需要時對代碼進行一些更改。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句