ValueError:使用序列python,numpy设置数组元素[重复]

Posted

技术标签:

【中文标题】ValueError:使用序列python,numpy设置数组元素[重复]【英文标题】:ValueError: setting an array element with a sequence python,numpy [duplicate] 【发布时间】:2018-05-09 05:30:44 【问题描述】:

我正在尝试学习 python。但这个错误让我很难过。如果有人知道这个错误,请告诉我为什么...... 代码是

data_vec = []
data_vec_sp = []
for i in range(0,len(data_ham)):
    for j in range(0,len(data_ham[i])):
       data_vec.append(np.ndarray.tolist(model.wv[data_ham[i][j]]))
for i in range(0,len(data_spam)):
    for j in range(0,len(data_spam[i])):
       data_vec_sp.append(np.ndarray.tolist(model.wv[data_spam[i][j]]))
lengths = []
for i in range(len(data_ham)):
    lengths.append(len(data_ham[i]))
lengths_sp = []
for i in range(len(data_spam)):
    lengths_sp.append(len(data_spam[i]))
max_seq=max(lengths+lengths_sp)

dataX = []
for i in range(len(lengths)):
    total = sum(lengths[:i])
    dataX.append(data_vec[total:total+lengths[i]])
dataY = []
for i in range(len(data_ham)):
    dataY.append([['0']])
for i in range(len(lengths_sp)):
    total = sum(lengths_sp[:i])
    dataX.append(data_vec[total:total+lengths_sp[i]])
for i in range(len(data_spam)):
    dataY.append([['1']])

processed = np.array(dataX,dtype = "f")
***Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.***

dataX 是词向量 比如

数据X[0][0]= [-0.1615869700908661,-0.3353504538536072,-0.05911640822887421,0.2664419412612915,0.23376771807670593,0.013697666116058826,0.08846186846494675,0.023173419758677483,0.23772063851356506,0.003773996140807867,-0.1691707819700241,0.4130796790122986,-0.0094629917293787,0.2146264612674713,0.2039080709218979,0.09040140360593796,0.14459063112735748,-0.12667202949523926,0.33783870935440063,0.006472950801253319,0.26460835337638855,0.13737431168556213 ,0.04874108359217644,0.17068377137184143,-0.13585394620895386,0.1331317275762558,0.15738724172115326,0.32112088799476624,-0.5897336006164551,-0.21117675304412842,-0.07932453602552414,0.2983131408691406,-0.14773669838905334,-0.11435408890247345,0.013958614319562912,0.27606719732284546,-0.1857372522354126,-0.3181498646736145,0.671415388584137,-0.029171733185648918,-0.19198976457118988, - 0.24715560674667358, 0.20854316651821136, 0.10005248337984085, -0.2556343376636505, 0.1531827747821808, 0.023028111085295677, 0. 19538813829422,0.13868877291679382,0.20250250399112701,0.06268277019262314,0.08580871671438217,0.06232626363635063,-0.014056061394512653,0.0780516192317009,-0.24966590106487274,-0.013334897346794605,0.3877508044242859,0.09984628856182098,0.2474772036075592,-0.016906747594475746,-0.17072062194347382,0.10843656957149506,0.15756770968437195,0.06598909944295883,0.017902569845318794,-0.549013614654541,0.21266651153564453,0.031236575916409492, 0.050672244280576706,0.27269500494003296,-0.252864271402359,0.3161967992782593,-0.04220119118690491,0.10098162293434143,-0.45764288306236267,0.31370213627815247,0.3360774517059326,-0.21591132879257202,-0.20584052801132202,0.13033269345760345,0.2552568316459656,-0.15911956131458282,0.18036118149757385,-0.09493415802717209,-0.021795518696308136,0.3608446717262268,0.03977162763476372,-0.19157318770885468, - 0.24235652387142181,-0.1861632615327835,0.0054754409939050674,-0.3136100769042969,-0.0009150459081865847,- 0.11295150965452194, 0.29373738169670105, 0.09046965092420578, 0.18789909780025482, 0.32259559631347656, -0.10442658513784409]

【问题讨论】:

该错误通常意味着您的列表输入具有不符合二维(或 nd-)数组的形状。数组在 numpy 中总是矩形的:每一行的大小相同。检查您的嵌套列表是否包含每个嵌套级别的所有相同长度的列表。 【参考方案1】:

只看到Xdata 的一个元素并没有多大帮助,只是暗示它是一个三重嵌套列表。

我会尝试重新创建一些更简单的东西。请注意,有几个子列表更短:

In [371]: dataX=[[[1,2,3],[4,5,6]],[[1,2],[3,4]]]

与子列表不同np.array 创建一个对象数组:

In [372]: np.array(dataX)
Out[372]: 
array([[list([1, 2, 3]), list([4, 5, 6])],
       [list([1, 2]), list([3, 4])]], dtype=object)

如果我强制它创建一个数字数组,它会给出你的错误:

In [373]: np.array(dataX,dtype=int)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-373-29c5879a94b0> in <module>()
----> 1 np.array(dataX,dtype=int)

ValueError: setting an array element with a sequence.

【讨论】:

以上是关于ValueError:使用序列python,numpy设置数组元素[重复]的主要内容,如果未能解决你的问题,请参考以下文章

ValueError:使用序列设置数组元素

ValueError:在昏暗 2 处预期长度为 0 的序列(得到 1)

无法在 Python 中捕获 ValueError

Python、matplotlib、ValueError:一个序列的真值是模棱两可的。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all() [重复]

SKLearn ValueError:使用序列设置数组元素

紧密布局抛出错误:ValueError:max()arg是一个空序列