Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am new to python, so please, bear with me!

This function:

def kerf(X,m):
        [n_samples, ]= X.shape
        n_sa, n_fe = m.shape
        ker = np.zeros((n_samples, n_sa))
        for i, x_i in enumerate(X):
            for j, m_j in enumerate(m):
                ker[i, j] = (np.dot(x_i, m_j))  # problem is here!!
        return ker

I call it like this:

Z=kerf(myarray[0,[0,1]],myarray[:,[0,1]])


ker[i, j] = (np.dot(x_i, m_j))
ValueError: setting an array element with a sequence.

myarray is basically the same matrix. Why?

share|improve this question
up vote 2 down vote accepted

When I replace the problem line with:

print(np.dot(x_i, m_j).shape)

it repeatedly prints (2,).

ker[i, j] takes 1 value; 2 values is sequence.

Please give us the dimensions of the arrays at various points, such as myarray (I guessed and tried a (3,4)), and at the problem point. print(...shape) is an essential debugging tool in numpy.

Do you need help on figure out why it's (2,)? May I suggest stepping through the loop in an interactive shell, looking at shapes at various points along the way.


the 2 inputs to the dot look like:

(1.0, array([ 1.,  1.]))

a scalar, and a 2 element array - so the dot is also a 2 element array.

You need to explain what size you expect these 2 arrays to be, and what size you expect the dot. Actually we can get the result - it's got to be (1,) or a scalar - 1 value to put in the one slot ker.


You can probably replace the double iteration with a single dot product (or if need be with an einsum call). But let's get this iteration working first.

share|improve this answer
    
myarray[0,[0,1]] contains just one row of these two numbers [1,233 0.6675] while myarray[:,[0,1]] contains like 12 rows of pair values like [1.879 0.5656]. I just wanna the dot product of the one row array with all the elements of the 12 rows array. – Th. Mtt. Oct 5 '15 at 12:04
    
With this one liner: ker = np.dot(myarray[0,[0,1]],np.transpose(myarray[:,[0,1]])) I can make it work, however i am not sure if it's quite good. – Th. Mtt. Oct 5 '15 at 12:23
    
Or shorter myarray[0, [0,1]).dot(myarray[:, [0,1].T), That looks like a good dot usage. Transpose if often used with matrix multipication to correctly pair dimensions. – hpaulj Oct 5 '15 at 16:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.