-1

I'm trying to study python. but this error makes me sad. If anyone knows that error, please tell me why... code is

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 is word vectors such as

dataX[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.1619538813829422, 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]

1
  • The error usually means that your list input has non-conforming shape to a 2d (or nd-) array. Arrays are always rectangular in numpy: each row is the same size. Check that your nested list contains lists of all the same length at every level of nesting. Commented Nov 25, 2017 at 13:18

1 Answer 1

0

Seeing just one element of Xdata doesn't help much, except to suggest that it is a triply nested list.

I'll try to recreate something simpler. Note that a couple of the sublists are shorter:

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

With sublist differences np.array creates an object 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)

If I force it to create a numeric array, it gives your error:

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.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.