简体   繁体   中英

Random matrix: int() argument must be a string or a number, not 'tuple'

I am trying to generate a matrix with which is mxn and contains random numbers. I have produced the following code, but am confused by the error I am receiving. Here is the code I am using:

class MP:
def __init__(self,
          mSize, nSize
             ):
    self.mSize=mSize,
    self.nSize=nSize

def RMatrix(param):
  assert isinstance(param, MP)
  m = int(param.mSize)
  n = int(param.nSize)
  A=np.random.rand(m,n)
  return (np.matrix(A))

I am receiving the error: int() argument must be a string or a number, not 'tuple'. Why are the values being passed as tuple? What is going on here and how can I fix this?

Got it. You have an comma in your init (at self.mSize=mSize**,**) . Remove that and your code will work fine.

import numpy as np
class MP:
    def __init__(self,
              mSize, nSize
                 ):
        self.mSize=mSize
        self.nSize=nSize

    def RMatrix(param):
      assert isinstance(param, MP)
      m = int(param.mSize)
      n = int(param.nSize)
      A=np.random.rand(m,n)
      return (np.matrix(A))

x=MP(3,3)
print x.RMatrix()

Output:

[[ 0.88170563  0.56061723  0.71311863]
 [ 0.3550273   0.83179011  0.10337231]
 [ 0.43324567  0.52177816  0.04648175]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM