Python numpy or pandas equivalent of the R function sweep()

前端 未结 3 2075
夕颜
夕颜 2021-01-14 01:14

What is numpy or pandas equivalent of the R function sweep()?

To elaborate: in R lets say we have a coefficient vector (say be

3条回答
  •  花落未央
    2021-01-14 01:34

    In numpy the concept is called "broadcasting". Example:

    import numpy as np
    x = np.random.random((4, 3))
    x * np.array(range(4))[:, np.newaxis] # sweep along the rows
    x + np.array(range(3))[np.newaxis, :] # sweep along the columns
    

提交回复
热议问题