I want to create a matrix with 1 column and n rows, to use in a calculation for a PageRank algorithm. If I make it like this, and then use the matrix in a calculation, it gives this result:
A = [[0.0375,0.4625,0.0375,0.32083333],
[0.0375, 0.0375,0.0375,0.32083333],
[0.8875, 0.0375, 0.0375, 0.32083333],
[0.0375, 0.4625, 0.8875, 0.0375]]
my_dp = 1/4
r = np.matrix([my_dp, my_dp, my_dp, my_dp])
r = np.transpose(r)
print(r)
for i in range(1,50):
r = A*r
print("Final:", print(r))
[[0.25]
[0.25]
[0.25]
[0.25]]
[[0.3570795 ]
[0.19760835]
[0.30663962]
[0.13867253]]
Final: None
But if I create it automatically, using np.empty and .fill, I get this result:
r = np.empty(n)
r.fill(my_dp)
r = r[None].T
print(r)
for i in range(1,50):
r = A*r
print("Final:", print(r))
[[0.25]
[0.25]
[0.25]
[0.25]]
[[3.35329783e-71 3.35329783e-71 7.21422583e-04 9.73677480e-18]
[1.60559016e-25 3.35329783e-71 3.35329783e-71 9.73677480e-18]
[1.60559016e-25 7.21422583e-04 3.35329783e-71 3.35329783e-71]
[1.60559016e-25 3.35329783e-71 3.35329783e-71 3.35329783e-71]]
Final: None
A
is an nxn adjacency matrix.
Why is this? As you can see, if I print the matrices, they look identical, and they should be.
I tried creating a new matrix and filling it with .fill, I tried creating a matrix with .full, but everything resulted in the second outcome. The only time it works properly is when I create the matrix manually, which is not very possible, since to continue, I will need to have hundreds of elements in the matrix.
It isn't clearly visible, but there IS a difference between the two examples.
The difference is that the first r
is an np.matrix
, and the second r
is an np.array
. One of the few differences between the two is the multiply operator. Using *
on a matrix does a matrix multiply. Using *
on an array does an element-wise multiply, where r
gets broadcast to fit the shape of A
.
If you want a matrix multiply, use the @
operator:
r = A@r
What are the differences between numpy arrays and matrices? Which one should I use?