gpt4 book ai didi

python - 我们可以制作md矩阵吗?

转载 作者:行者123 更新时间:2023-12-01 07:33:59 26 4
gpt4 key购买 nike

  1. 使用 python 制作 md 矩阵。
  2. reshape函数的原理是什么?

输入和结果应该看起来像结果

 in = (0, 1, 2, 3, 4, 5, 6, 7, 8)
tensor = (2, 3, 2)
result:
(((0, 1), (2, 3), (4, 5)), ((6, 7), (8, 0), (0, 0)))

最佳答案

尝试:

def multiply (arr):
result = 1
for x in arr:
result *= x
return result

def reshape(shape, arr):
result = arr
newSize = multiply(shape)
if (len(arr) < newSize):
while (len(arr) < newSize):
result.append(0)
else:
result= result[:newSize]

for s in shape[::-1]:
result = [ result[i:i+s] for i in range(0,len(result),s)]
return result

data = [0, 1, 2, 3, 4, 5, 6, 7, 8]
shape = [2, 3, 2]

print(reshape(shape, data))

data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
shape = [6, 2]

print(reshape(shape, data))

关于python - 我们可以制作md矩阵吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57085827/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com