我想在不使用 numpy 的情况下制作一个 Class 但是出了点问题
I want to make a Class without using numpy but there's something wrong
我正在努力适应如何使用Class,但我想我犯了一些错误。
我遇到的第一个错误是:
类型错误:init() 缺少 2 个必需的位置参数:'N' 和 'M'
如果我不能用随机整数制作一个矩阵,那么我就不能制作其他的...
所以我非常想解决这个问题...:( 请帮助我!
import random
从键入导入列表
class 麻木的:
def init(self, N: int, M: int):
### 在这里编辑###
self.N = N
self.M = 米
# 用 randInt 制作矩阵
self.matrix = [[]]
self.randInt()
#################
def __str__(self):
### Edit Here ###
return str(self.matrix)
# print matrix
#################
def randInt(self) -> List[List[int]]:
### Edit Here ###
# make random int N * M matrix
self.matrix = [[random.randint(0, 100) for row in range(self.N)] for column in range(self.M)]
#################
def mean(self, axis: int) -> List[int]:
### Edit Here ###
self.axis = axis
if self.axis == 1:
mean = []
for j in range(self.M):
s = 0
for i in range(self.N):
s += self.matrix[j][i]
mean.append(s/self.M)
elif self.axis == 0:
mean = []
for j in range(self.N):
s = 0
for i in range(self.M):
s += self.matrix[i][j]
mean.append(s/self.N)
# calculate mean for each axis
#################
return mean
def argmax(self, axis: int) -> List[int]:
### Edit Here ###
# find index of max value for each axis
if axis == 1:
index = []
for i in range(len(self.matrix)):
index.append(self.matrix[i].index(max(self.matrix[i])))
elif axis == 0:
matrix = [[row[i] for row in self.matrix] for i in range(len(self.matrix[0]))]
index = []
for i in range(len(matrix)):
index.append(matrix[i].index(max(matrix[i])))
#################
return index
def concatenate(self, mat: List[List[int]], axis: int):
### Edit Here ###
# concatenate mat to existing matrix
if axis == 0 :
return self.matrix + mat
elif axis == 1:
for i in range(self.M):
self.matrix[i] = self.matrix[i] + mat[i]
return self.matrix
#################
def zeros(self, N: int, M: int) -> List[List[int]]:
### Edit Here ###
# make N * M matrix with all zero values
self.N = N
self.M = M
zeros = [([0]*self.N) for i in range(self.M)]
#################
return zeros
当您创建 Numpy
class.
的实例时调用 __init__
方法
因此,如果您的 __init__
方法采用 2 个参数 N
和 M
,您的最后一行应该是 print(Numpy(2,3))
.
这也意味着您正在调用 randInt
方法两次:
- 创建对象时,在
init
方法中(Numpy()
应该替换为 self
,否则您将在新的 Numpy
对象,你将有一个无限递归)。
- 然后当您在
print
语句中调用对象上的方法时。
此外,您的 randInt
方法需要 2 个参数 N
和 M
,但您没有使用它们,因为您使用了 class 变量 self.N
和 self.M
在你的方法中。
因此,为了解决您的问题,我建议您
- 将您的
print(Numpy().randInt(3,2))
语句替换为 print(Numpy(3,2))
- 将行
self.matrix = Numpy().randInt(self.N, self.M)
替换为 self.matrix = self.randInt()
- 修改您的
randInt
函数以删除您不使用的 2 个参数:def randInt(self) -> List[List[int]]:
我会做:
import random
from typing import List
class Matrix:
def __init__(self, N: int, M: int):
### Edit Here ###
self.N = N
self.M = M
# make matrix with randInt
self.matrix = [[]]
self.rand_int()
#################
def __str__(self):
### Edit Here ###
return str(self.matrix)
# print matrix
#################
def rand_int(self) -> List[List[int]]:
### Edit Here ###
# make random int N * M matrix
self.matrix = [[random.randint(0, 100) for row in range(self.N)] for column in range(self.M)]
#################
def mean(self, axis: int) -> List[int]:
### Edit Here ###
if self.axis == 0:
mean = []
for j in range(self.M):
s = 0
for i in range(self.N):
s += self.matrix[0][i]
mean.append(s/self.M)
elif self.axis == 1:
mean = []
for j in range(self.N):
s = 0
for i in range(self.M):
s += self.matrix[0][i]
mean.append(s/self.N)
# calculate mean for each axis
#################
return mean
def argmax(self, axis: int) -> List[int]:
### Edit Here ###
# find index of max value for each axis
x = self.axis
index = max(range(len(x)), key=lambda i: x[i])
#################
return index
def concatenate(self, mat: List[List[int]], axis: int):
### Edit Here ###
# concatenate mat to existing matrix
if axis == 0 :
return self.matrix + mat
elif axis == 1:
for i in range(self.M):
self.matrix[i] = self.matrix[i] + mat[i]
return self.matrix
#################
def zeros(self, N: int, M: int) -> List[List[int]]:
### Edit Here ###
# make N * M matrix with all zero values
zeros = [([0]*self.M) for i in range(self.N)]
#################
return zeros
print(Matrix(3,2))
请注意,此代码中有一些非 pythonic 方式。例如,对于 zeros 和 randint,我最好做工厂,这将导致:
import random
class Matrix:
def __init__(self, matrix=None):
self.matrix = matrix
@property
def n(self):
return len(self.matrix)
@property
def m(self):
if len(self.matrix) > 0:
return len(self.matrix[0])
else:
return 0
def __str__(self):
return str(self.matrix)
@classmethod
def rand_int(cls, the_n, the_m):
my_matrix = cls()
my_matrix.matrix = [[random.randint(0, 100) for row in range(the_n)] for column in range(the_m)]
return my_matrix
@classmethod
def zeros(cls, the_n, the_m):
my_matrix = cls()
my_matrix.matrix = [([0]*the_m) for i in range(the_n)]
return my_matrix
print(Matrix())
print(Matrix.zeros(3,2))
print(Matrix.rand_int(3,2))
print(Matrix.rand_int(3,2).n)
print(Matrix.rand_int(3,2).m)
这导致
None
[[0, 0], [0, 0], [0, 0]]
[[31, 32, 62], [99, 89, 85]]
2
3
我正在努力适应如何使用Class,但我想我犯了一些错误。
我遇到的第一个错误是:
类型错误:init() 缺少 2 个必需的位置参数:'N' 和 'M'
如果我不能用随机整数制作一个矩阵,那么我就不能制作其他的...
所以我非常想解决这个问题...:( 请帮助我!
import random
从键入导入列表
class 麻木的: def init(self, N: int, M: int): ### 在这里编辑### self.N = N self.M = 米 # 用 randInt 制作矩阵 self.matrix = [[]] self.randInt() #################
def __str__(self):
### Edit Here ###
return str(self.matrix)
# print matrix
#################
def randInt(self) -> List[List[int]]:
### Edit Here ###
# make random int N * M matrix
self.matrix = [[random.randint(0, 100) for row in range(self.N)] for column in range(self.M)]
#################
def mean(self, axis: int) -> List[int]:
### Edit Here ###
self.axis = axis
if self.axis == 1:
mean = []
for j in range(self.M):
s = 0
for i in range(self.N):
s += self.matrix[j][i]
mean.append(s/self.M)
elif self.axis == 0:
mean = []
for j in range(self.N):
s = 0
for i in range(self.M):
s += self.matrix[i][j]
mean.append(s/self.N)
# calculate mean for each axis
#################
return mean
def argmax(self, axis: int) -> List[int]:
### Edit Here ###
# find index of max value for each axis
if axis == 1:
index = []
for i in range(len(self.matrix)):
index.append(self.matrix[i].index(max(self.matrix[i])))
elif axis == 0:
matrix = [[row[i] for row in self.matrix] for i in range(len(self.matrix[0]))]
index = []
for i in range(len(matrix)):
index.append(matrix[i].index(max(matrix[i])))
#################
return index
def concatenate(self, mat: List[List[int]], axis: int):
### Edit Here ###
# concatenate mat to existing matrix
if axis == 0 :
return self.matrix + mat
elif axis == 1:
for i in range(self.M):
self.matrix[i] = self.matrix[i] + mat[i]
return self.matrix
#################
def zeros(self, N: int, M: int) -> List[List[int]]:
### Edit Here ###
# make N * M matrix with all zero values
self.N = N
self.M = M
zeros = [([0]*self.N) for i in range(self.M)]
#################
return zeros
当您创建 Numpy
class.
__init__
方法
因此,如果您的 __init__
方法采用 2 个参数 N
和 M
,您的最后一行应该是 print(Numpy(2,3))
.
这也意味着您正在调用 randInt
方法两次:
- 创建对象时,在
init
方法中(Numpy()
应该替换为self
,否则您将在新的Numpy
对象,你将有一个无限递归)。 - 然后当您在
print
语句中调用对象上的方法时。
此外,您的 randInt
方法需要 2 个参数 N
和 M
,但您没有使用它们,因为您使用了 class 变量 self.N
和 self.M
在你的方法中。
因此,为了解决您的问题,我建议您
- 将您的
print(Numpy().randInt(3,2))
语句替换为print(Numpy(3,2))
- 将行
self.matrix = Numpy().randInt(self.N, self.M)
替换为self.matrix = self.randInt()
- 修改您的
randInt
函数以删除您不使用的 2 个参数:def randInt(self) -> List[List[int]]:
我会做:
import random
from typing import List
class Matrix:
def __init__(self, N: int, M: int):
### Edit Here ###
self.N = N
self.M = M
# make matrix with randInt
self.matrix = [[]]
self.rand_int()
#################
def __str__(self):
### Edit Here ###
return str(self.matrix)
# print matrix
#################
def rand_int(self) -> List[List[int]]:
### Edit Here ###
# make random int N * M matrix
self.matrix = [[random.randint(0, 100) for row in range(self.N)] for column in range(self.M)]
#################
def mean(self, axis: int) -> List[int]:
### Edit Here ###
if self.axis == 0:
mean = []
for j in range(self.M):
s = 0
for i in range(self.N):
s += self.matrix[0][i]
mean.append(s/self.M)
elif self.axis == 1:
mean = []
for j in range(self.N):
s = 0
for i in range(self.M):
s += self.matrix[0][i]
mean.append(s/self.N)
# calculate mean for each axis
#################
return mean
def argmax(self, axis: int) -> List[int]:
### Edit Here ###
# find index of max value for each axis
x = self.axis
index = max(range(len(x)), key=lambda i: x[i])
#################
return index
def concatenate(self, mat: List[List[int]], axis: int):
### Edit Here ###
# concatenate mat to existing matrix
if axis == 0 :
return self.matrix + mat
elif axis == 1:
for i in range(self.M):
self.matrix[i] = self.matrix[i] + mat[i]
return self.matrix
#################
def zeros(self, N: int, M: int) -> List[List[int]]:
### Edit Here ###
# make N * M matrix with all zero values
zeros = [([0]*self.M) for i in range(self.N)]
#################
return zeros
print(Matrix(3,2))
请注意,此代码中有一些非 pythonic 方式。例如,对于 zeros 和 randint,我最好做工厂,这将导致:
import random
class Matrix:
def __init__(self, matrix=None):
self.matrix = matrix
@property
def n(self):
return len(self.matrix)
@property
def m(self):
if len(self.matrix) > 0:
return len(self.matrix[0])
else:
return 0
def __str__(self):
return str(self.matrix)
@classmethod
def rand_int(cls, the_n, the_m):
my_matrix = cls()
my_matrix.matrix = [[random.randint(0, 100) for row in range(the_n)] for column in range(the_m)]
return my_matrix
@classmethod
def zeros(cls, the_n, the_m):
my_matrix = cls()
my_matrix.matrix = [([0]*the_m) for i in range(the_n)]
return my_matrix
print(Matrix())
print(Matrix.zeros(3,2))
print(Matrix.rand_int(3,2))
print(Matrix.rand_int(3,2).n)
print(Matrix.rand_int(3,2).m)
这导致
None
[[0, 0], [0, 0], [0, 0]]
[[31, 32, 62], [99, 89, 85]]
2
3