[英]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. 我正在尝试生成一个矩阵,其中包含mxn并包含随机数。 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'. 我收到错误:int()参数必须是字符串或数字,而不是'元组'。 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**,**) .
你的init中有一个逗号(在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]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.