training = []
empty_list = [0] *len(classes)
for doc in docs:
bag = []
word_pat = doc[0]
word_pat = [lem.lemmatize((w.lower())) for w in word_pat]
for w in words:
if w in word_pat:
bag.append(1)
else:
bag.append(0)
output_row = list(empty_list)
output_row[classes.index(doc[1])] = 1
training.append([bag, output_row])
random.shuffle(training)
**training = np.array(training)**
执行后,弹出以下错误:
ValueError:用序列设置数组元素。请求的数组在2维之后具有非均匀形状。检测形状为(38,2)+不均匀部分。
任何想法?
我主要是按照我在网上看到的一个教程,但即使我一步一步地遵循,它不起作用,它可能是过时的。我最近才研究过ML等,以前主要做过java OOP,所以我真的没有知识来修复这个错误,因为我不完全理解numpy。
您使用的是什么numpy
版本?
通常情况下,我会使用traceback请求完整的错误消息。但是如果**
应该指示问题行,那可能就足够了(如果非常规的话)。
在较旧的numpy版本中,我可以重新创建一个类似的问题:
In [171]: alist = [np.ones((3,4,2)), np.zeros((3,4,3))]
In [172]: np.array(alist)
C:UserspaulAppDataLocalTempipykernel_68362629805649.py:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
np.array(alist)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[172], line 1
----> 1 np.array(alist)
ValueError: could not broadcast input array from shape (3,4,2) into shape (3,4)
错误是不言自明的。您正在尝试将几个具有不兼容形状的数组合并到一个数组中。
您检查过training
列表中元素的形状了吗?
training
数组格式不正确。您需要向该方法传递一个额外的参数才能使其工作。
将带有星号的代码替换为:
training = np.array(training, dtype=object)