Reputation: 55
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?
Upvotes: 1
Views: 461
Reputation: 231665
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.
Upvotes: 2