new = zero(rows_A,cols_B)
for i in range(rows_A):
for j in range(cols_B):
new[i][j] += np.sum(A[i] * B[:,j])
If I'm using this form of array [[0, 0, 0], [0, 1, 0], [0, 2, 1]]
in B
it is giving me an error
TypeError: list indices must be integers, not tuple
but if I'm using same array B
, in place of A
, it's working well.
I am getting this type of return array
[[0, 0, 0], [0, 1, 0], [0, 2, 1]]
so i want to convert it into this form
[[0 0 0]
[0 1 0]
[0 2 1]]
numpy.asarray
will do that.
import numpy as np
B = np.asarray([[0, 0, 0], [0, 1, 0], [0, 2, 1]])
This produces
array([[0, 0, 0],
[0, 1, 0],
[0, 2, 1]])
which can be indexed with [:, j]
.
Also, it looks like you're trying to do a matrix product. You can do the same thing with just one line of code using np.dot
:
new = np.dot(A, B)
It appears that B
is a list. You can't index it as B[:,i]
-- Which is implcitly passed to __getitem__
as (slice(None,None,None),i)
-- ie a tuple.
You could convert B
to a numpy array first ( B = np.array(B)
) and then go from there ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.