Python 将结果存储为大数组的元组

Python 将结果存储为大数组的元组,python,numpy,scipy,scikit-learn,Python,Numpy,Scipy,Scikit Learn,要按列计算最小值: import numpy as np from scipy import signal d0 = np.random.random_integers(10, 12, (5,5)) d1 = np.random.random_integers(1, 3, (5,5)) d2 = np.random.random_integers(4, 6, (5,5)) d3 = np.random.random_integers(7, 9, (5,5)) d4 = np.random.ra

要按列计算最小值:

import numpy as np
from scipy import signal

d0 = np.random.random_integers(10, 12, (5,5))
d1 = np.random.random_integers(1, 3, (5,5))
d2 = np.random.random_integers(4, 6, (5,5))
d3 = np.random.random_integers(7, 9, (5,5))
d4 = np.random.random_integers(1, 3, (5,5))
d5 = np.random.random_integers(13, 15, (5,5))

data = np.array([d0,d1,d2,d3,d4,d5])

data = data.reshape(6,25)


data = data.T
希望以原始数据的形式存储结果

minimas =[]
for x in data:
    minima = (signal.argrelmin(x, axis=0))
    minimas.append(x[minima])
但是

是否可以将结果存储为大型数组的元组?
希望知道是否有更有效的方法来解决此问题。

minimas
是25个数组的列表(每个数组的形状
(2,)
)<代码>np。数组(极小值)具有形状
(25,2)

工作

您可以使用
dtype=object
minimas
打包到一个数组中:

np.array(minimas).reshape(5,5,2)

如果
minimas
中的数组大小不同,则该数组将自动具有类型
对象

minarray = np.empty((5,5),dtype=object) # initialize an empty array of 'objects'
minarray[:] = [tuple(m) for m in minimas]
minarray.reshape(5,5)

array([[(1, 2), (1, 1), (2, 2), (2, 2), (3, 3)],
...
       [(3, 2), (1, 2), (1, 1), (1, 2), (2, 3)]], dtype=object)
产生

minimas[-1] = np.array([1,2,3]) # tweak data so there is some length variation
minimas = [tuple(row) for row in minimas]
np.array(minimas)
可以
重塑(5,5)


如果
minimas
中的数组长度都相同,则
np.array(minimas)
生成一个最终大小为2维的数组。这就是为什么我必须初始化所需类型的
minarray
数组,并在其中插入
minimas
。这是我在回答其他SO问题时学到的
numpy
的一个模糊部分。

您需要将每个项目转换为列表,然后使用列表切片,您可以索引您需要的任何值

array([(1, 2), (1, 1), (2, 2), (2, 2), (3, 3), (2, 1), (3, 2), (3, 3),
       (1, 2), (2, 3), (1, 2), (2, 3), (2, 2), (1, 2), (3, 3), (2, 2),
       (1, 3), (3, 1), (3, 2), (2, 1), (3, 2), (1, 2), (1, 1), (1, 2),
       (1, 2, 3)], dtype=object)  # shape=(25,)

你想找什么?每列的最小值?是的,每列的最小值我想将其存储为元组,这样我可以分离属于每列的最小值,因为每列的最小值可能有不同的数目。每列的最小值的数目在我处理的数据中可能不同,我需要存储所有的最小值,我认为它是元组。还有别的好办法吗。无论如何,我必须将所有计算出的最小值分别存储到每一列。我无法理解您的代码。什么是x?x和我在数据中使用的x相同吗…??我添加了一些注释。如果最小值的数目不同,则数组将自动具有
dtype=object
。但在您的示例中,最小值的数量都是相同的,需要一些额外的代码来生成元组数组。谢谢您的努力。但最小值和最小值之间存在差异,因为最小值指的是曲线的最小点,在我的问题中,每列有两个最小值。我想将问题中从for循环接收到的值存储为元组,以便每列的最小值位于单独的元组中
np.array(tup)
已经是
(25,2)
。不需要重塑,除非它是
(5,5,2)
minimas[-1] = np.array([1,2,3]) # tweak data so there is some length variation
minimas = [tuple(row) for row in minimas]
np.array(minimas)
array([(1, 2), (1, 1), (2, 2), (2, 2), (3, 3), (2, 1), (3, 2), (3, 3),
       (1, 2), (2, 3), (1, 2), (2, 3), (2, 2), (1, 2), (3, 3), (2, 2),
       (1, 3), (3, 1), (3, 2), (2, 1), (3, 2), (1, 2), (1, 1), (1, 2),
       (1, 2, 3)], dtype=object)  # shape=(25,)
import numpy as np
from scipy import signal

d0 = np.random.random_integers(10, 12, (5, 5))
d1 = np.random.random_integers(1, 3, (5, 5))
d2 = np.random.random_integers(4, 6, (5, 5))
d3 = np.random.random_integers(7, 9, (5, 5))
d4 = np.random.random_integers(1, 3, (5, 5))
d5 = np.random.random_integers(13, 15, (5, 5))

data = np.array([d0, d1, d2, d3, d4, d5])

data = data.reshape(6, 25)

data = data.T

minimas = []
for x in data:
    minima = (signal.argrelmin(x, axis=0))
    minimas.append(x[minima])
tup = list(a.tolist() for a in minimas)
ab = (np.array(tup).reshape(25, 2))
print(ab[0]) #[3 1]