提取数组列表的最小值,在Python中绘制线及其交点

2024-09-29 21:47:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个由子数组组成的列表,比如:

[array(.....),array(.....),array(.....),....]

所有数组的长度都相同(基本上每个数组代表一条线)。你知道吗

我想逐元素提取最小值。如果每个数组有100个元素,那么我希望最后的列表长度为100个元素。我还要这些线相交的点。类似这样的事情应该能澄清我的意思:

https://www.dropbox.com/s/xshkhvqp0ay3vxc/g14.png


Tags: httpscom元素列表pngwww代表数组
3条回答

像这样:

>>> import numpy as np
>>> np.random.seed(0)
>>> data = np.random.rand(3, 4)
>>> data
array([[ 0.5488135 ,  0.71518937,  0.60276338,  0.54488318],
       [ 0.4236548 ,  0.64589411,  0.43758721,  0.891773  ],
       [ 0.96366276,  0.38344152,  0.79172504,  0.52889492]])
>>> result = data.min(axis=1)
>>> result
array([ 0.54488318,  0.4236548 ,  0.38344152])
>>> 

假设您有以下列表:

>>> l = [array('i', [5, 15, 1, 25]),
         array('i', [5, 15, 2, 25]), 
         array('i', [5, 15, 3, 25])]

您可以通过以下方法获得每个数组的最小值:

>>> [min(x) for x in l]
[1, 2, 3]

抱歉,我不明白你剩下的问题:)

我并不认为这是最好的方法,因为我的思想还没有完全围绕Python。在计算相图时,我已经编写了一个类似问题的解决方案。在给定的温度下,对于不同相数的自由能函数,总的最小自由能曲线是什么,在每个点上哪个函数是最小的。你知道吗

G = []
while iPhase < len(f): # loop through all free energy functions
    G.append(f[iPhase](x)) # x is an array of x values 
    iPhase = iPhase+1
minG = G{0][:] # define an overall minimum free energy curve, starting with 0'th
minF = np.zeros(len(minG)) # prepare for index indicating which function f[i](x) is min
iPhase = 1
while iPhase < len(f):
    nextF = iPhase*np.ones(len(x0, dtype=np.int)
    np.less(G[iPhase],minG, nextF) # where is next free energy function less than current min
    minG = np.minimum(minG, G[iPhase]) # new overall minimum
    minF = np.ma.filled(np.ma.array(minF, mask=nextF), fill_value=iPhase) # new index as needed
    iPhase = iPhase+1

所以,最终的输出是一个总体最小值minG,以及它来自哪个曲线的指数minF。现在,如果您想细化交点,可以使用

changes = np.array(np.where(minF[:-1]!=minF[1:]))

返回线交叉的位置以及涉及哪些函数的索引。如果不同的函数真的是直线y=mx*b,你可以做代数运算得到精确的交叉。对于更复杂的函数,需要一个更复杂的过程(我将临时函数定义为所考虑的两个函数之间的差异,然后使用科学优化牛顿得到零)。你知道吗

相关问题 更多 >

    热门问题