Simple way to create matrix of random numbers

前端 未结 13 1689
灰色年华
灰色年华 2020-12-12 21:20

I am trying to create a matrix of random numbers, but my solution is too long and looks ugly

random_matrix = [[random.random() for e in range(2)] for e in ra         


        
13条回答
  •  情话喂你
    2020-12-12 21:57

    First, create numpy array then convert it into matrix. See the code below:

    import numpy
    
    B = numpy.random.random((3, 4)) #its ndArray
    C = numpy.matrix(B)# it is matrix
    print(type(B))
    print(type(C)) 
    print(C)
    

提交回复
热议问题