0

I have a Python code partially borrowed from Generating Markov transition matrix in Python:

# xstates is a dictionary
# n - is the matrix size
def prob(xstates, n):
    # we want to do smoothing, so create matrix of all 1s
    M = [[1] * n for _ in range(n)]

    # populate matrix by (row, column)
    for key, val in xstates.items():
        (row, col) = key
        M[row][col] = val

    # and finally calculate probabilities
    for row in M:
        s = sum(row)
        if s > 0:
            row[:] = [f/s for f in row]

    return M

xstates here comes in a form of dictionary, e.g. :

{(2, 2): 387, (1, 2): 25, (0, 1): 15, (2, 1): 12, (3, 2): 5, (2, 3): 5, (6, 2): 4, (5, 6): 4, (4, 2): 2, (0, 2): 1}

where (1, 2) means state 1 transits to state 2 and similar to others.

This function generates the matrix of transition probabilities, the sum of all elements in a row is 1. Now I need to normalize the values. How would I do that? Can I do that with numpy library?

Mark
  • 6,052
  • 8
  • 61
  • 129

1 Answers1

2
import numpy as np
M = np.random.random([3,2])
print(M)

row sum to 1

M = M / M.sum(axis=1)[:, np.newaxis]
print(M)

column sum to 1

M = M / M.sum(axis=0)[np.newaxis,:]
print(M)
TracyYXChen
  • 173
  • 1
  • 9
  • 1
    thanks for comment. Could you explain what '[:, np.newaxis]' in the first expression mean? – Mark Jan 13 '20 at 21:48
  • 2
    M.sum(axis=1) returns a 1-D array, but we cannot divide a matrix by a 1-D array, so we add another dimension, e.g. here shape (3,) becomes (3,1), the same as np.reshape(-1,1) – TracyYXChen Jan 14 '20 at 02:29