英文:
How can I create a New numpy array of matrix dimensions 1x3,1x3 and 1x1 using an already existing 2D array containing 7 elements?
问题
我想要的答案是这样的,但每当我尝试连接第三个数组时,就会显示错误
[[2. 5. 8.] [4. 5. 6.] [5.]]
在使用hstack时运行正常,但我想要三个不同矩阵的集合?
它说除了连接轴之外的所有输入数组维度必须完全匹配,但沿着维度1,索引0处的数组大小为3,索引2处的数组大小为1
在使用hstack时运行正常,但我想要三个不同矩阵的集合?
它说除了连接轴之外的所有输入数组维度必须完全匹配,但沿着维度1,索引0处的数组大小为3,索引2处的数组大小为1
英文:
import numpy as np
def calculate(lst):
arr = np.array(lst)
print(arr.shape)
newarr = arr.reshape((3, 3))
mean = np.mean(newarr)
rowmeans = np.mean(newarr, axis=1)
columnmeans = np.mean(newarr, axis=0)
combined_array1 = rowmeans.reshape(1, 3)
combined_array2 = columnmeans.reshape(1, 3)
combined_array3 = np.array([[mean]]) # Reshape mean to (1, 1)
combinedarray = np.concatenate((combined_array1, combined_array2, combined_array3), axis=0)
print(combinedarray)
calculate([1, 2, 3, 4, 5, 6, 7, 8, 9])
I want my answer to be like this but whenever I try to concatenate the third arrray I shows an error
[[2. 5. 8.] [4. 5. 6.] [5.]]
I works fine when I use hstack but I want a set of three different matrices?
It says all the input array dimensions except for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 3 and the array at index 2 has size 1
I works fine when I use hstack but I want a set of three different matrices?
It says all the input array dimensions except for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 3 and the array at index 2 has size 1
答案1
得分: 1
这是一个列表结构而非数组。普通的NumPy数组是常规数组,可以以高效的方式存储在内存中,具有高效的内存位置索引和高速操作。
如果你想的话,可以将这三个NumPy数组放入一个Python列表中。需要明确的是,该列表只包含对原始数组的引用,访问该列表只是间接访问原始数组的一种方式。
如果你真的 必须 将这个列表结构存储在一个NumPy数组中,那么NumPy 中的 "object" 数组可以用于存储引用链接,但我认为这会颠覆NumPy数组的整个目的,并且可能会在后面给你造成问题。
combinedlist = [combined_array1, combined_array2, combined_array3]
combinedarray = np.empty([3], dtype=object)
combinedarray[:] = combinedlist
输出将会是:
[array([[2., 5., 8.]]) array([[4., 5., 6.]]) array([[5.]])]
英文:
This [[2. 5. 8.] [4. 5. 6.] [5.]] is a list structure not an array. Normal Numpy arrays are regular arrays so that they can be stored in memory with space efficiency, efficient indexing of the memory locations, and high speed operation.
You can put the three numpy arrays into a Python list if you want. To be clear, the list would contain just the references to the original arrays, and accessing that list would just be a way to indirectly access the original arrays.
If you really must store this list structure in a numpy array, then numpy "object" arrays do exist for storing reference links, but I think this upends the whole point of numpy arrays, and will probably cause problems for you later.
combinedlist = [combined_array1, combined_array2, combined_array3]
combinedarray = np.empty([3], dtype=object)
combinedarray[:] = combinedlist
The output will be this:
[array([[2., 5., 8.]]) array([[4., 5., 6.]]) array([[5.]])]
答案2
得分: 0
返回一个列表:
在[56]中: 定义一个名为calculate的函数
...: arr = np.array(lst)
...: print(arr.shape)
...: newarr = arr.reshape((3, 3))
...: mean = np.mean(newarr)
...: rowmeans = np.mean(newarr, axis=1)
...: columnmeans = np.mean(newarr, axis=0)
...: combined_array1 = rowmeans.reshape(1, 3)
...: combined_array2 = columnmeans.reshape(1, 3)
...: combined_array3 = np.array([[mean]]) # 将mean重塑为(1, 1)
...: combined = [combined_array1, combined_array2, combined_array3]
...: return combined
...:
...: alist = calculate([1, 2, 3, 4, 5, 6, 7, 8, 9])
(9,)
这是3个数组,形状分别为(1,3),(1,3),(1,1)。注意双括号。
在[57]中: alist
Out[57]: [array([[2., 5., 8.]]), array([[4., 5., 6.]]), array([[5.]])]
`hstack`将它们连接在最后一个轴上,即轴1:
在[58]中: np.hstack(alist)
Out[58]: array([[2., 5., 8., 4., 5., 6., 5.]])
在[59]中: np.concatenate(alist, axis=1)
Out[59]: array([[2., 5., 8., 4., 5., 6., 5.]])
`axis=0`不起作用,因为轴1变化,分别为3,3,1:
在[60]中: np.concatenate(alist, axis=0)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[60], line 1
----> 1 np.concatenate(alist, axis=0)
ValueError: all the input array dimensions except for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 3 and the array at index 2 has size 1
在[63]中: [i.shape for i in alist]
Out[63]: [(1, 3), (1, 3), (1, 1)]
英文:
Returning a list:
In [56]: def calculate(lst):
...: arr = np.array(lst)
...: print(arr.shape)
...: newarr = arr.reshape((3, 3))
...: mean = np.mean(newarr)
...: rowmeans = np.mean(newarr, axis=1)
...: columnmeans = np.mean(newarr, axis=0)
...: combined_array1 = rowmeans.reshape(1, 3)
...: combined_array2 = columnmeans.reshape(1, 3)
...: combined_array3 = np.array([[mean]]) # Reshape mean to (1, 1)
...: combined = [combined_array1, combined_array2, combined_array3]
...: return combined
...:
...: alist = calculate([1, 2, 3, 4, 5, 6, 7, 8, 9])
(9,)
That is 3 array, with shape (1,3),(1,3),(1,1). Pay attention to the double brackets.
In [57]: alist
Out[57]: [array([[2., 5., 8.]]), array([[4., 5., 6.]]), array([[5.]])]
hstack
joins them on the last axis, axis 1:
In [58]: np.hstack(alist)
Out[58]: array([[2., 5., 8., 4., 5., 6., 5.]])
In [59]: np.concatenate(alist, axis=1)
Out[59]: array([[2., 5., 8., 4., 5., 6., 5.]])
axis=0
doesn't work because axis 1 varies, 3,3,1:
In [60]: np.concatenate(alist, axis=0)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[60], line 1
----> 1 np.concatenate(alist, axis=0)
ValueError: all the input array dimensions except for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 3 and the array at index 2 has size 1
In [63]: [i.shape for i in alist]
Out[63]: [(1, 3), (1, 3), (1, 1)]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论