0

Attempting to bring my Python up to snuff with my R skills, and realized that I have no clue how to do simple matrix arithmetic without the use of Numpy.

x = [[1,2,3],[4,5,6]]
y = [[1,2],[3,4],[5,6]]

I tried

X = (x*y for x,y in zip([[1,2,3],[4,5,6]],[[1,2],[3,4],[5,6]]))

but got <generator object <genexpr> at 0xb205fc34>

I also tried unsuccessfully to multiply an array by a list.

x = [[1],[2],[3]
y = [1,2,3]

I tried to get the outer product of the two using

Y = (x.doty for x,y in zip([[1],[2],[3]],[[1,2,3]]))

but print y returned '6'

Thanks in advance!

EDIT: I am looking to replicate the following (written in R)

I am looking to replicate this (written in R)

x = matrix(c(1,2,3,4,5,6),
  nrow = 2,
  ncol = 3,
  byrow = TRUE)

y = matrix(c(1,2,3,4,5,6),
           nrow = 3,
           ncol = 2,
           byrow = TRUE)

z = x%*%y

print(z)
[,1] [,2]
[1,]   22   28
[2,]   49   64

y1 = (1,2,3)
x1 = t(y1)
outer.product = x1 %*% y1

print(outer.product)
[,1] [,2] [,3]
[1,]    1    2    3
[2,]    2    4    6
[3,]    3    6    9
scribbles
  • 4,089
  • 7
  • 22
  • 29

1 Answers1

0

Python lists and NumPy arrays are different. If you are working with NumPy arrays then you can do things like:

>>> x = [[1,2,3],[4,5,6]]
>>> y = [[1,2],[3,4],[5,6]]
>>> X = np.array(x)
>>> Y = np.array(y)
>>> X.dot(Y)
array([[22, 28],
       [49, 64]])
>>> 
Community
  • 1
  • 1
YXD
  • 31,741
  • 15
  • 75
  • 115