<small id='FP2hz'></small><noframes id='FP2hz'>

      <legend id='FP2hz'><style id='FP2hz'><dir id='FP2hz'><q id='FP2hz'></q></dir></style></legend>
    1. <i id='FP2hz'><tr id='FP2hz'><dt id='FP2hz'><q id='FP2hz'><span id='FP2hz'><b id='FP2hz'><form id='FP2hz'><ins id='FP2hz'></ins><ul id='FP2hz'></ul><sub id='FP2hz'></sub></form><legend id='FP2hz'></legend><bdo id='FP2hz'><pre id='FP2hz'><center id='FP2hz'></center></pre></bdo></b><th id='FP2hz'></th></span></q></dt></tr></i><div id='FP2hz'><tfoot id='FP2hz'></tfoot><dl id='FP2hz'><fieldset id='FP2hz'></fieldset></dl></div>
        <bdo id='FP2hz'></bdo><ul id='FP2hz'></ul>
      1. <tfoot id='FP2hz'></tfoot>

        提取每个x和y的多个数组的最小值和最大值

        Extracted minimal and maximal values of multiple arrays of every x and y(提取每个x和y的多个数组的最小值和最大值)

          <tbody id='pR1ft'></tbody>

        <small id='pR1ft'></small><noframes id='pR1ft'>

          1. <tfoot id='pR1ft'></tfoot>

          2. <i id='pR1ft'><tr id='pR1ft'><dt id='pR1ft'><q id='pR1ft'><span id='pR1ft'><b id='pR1ft'><form id='pR1ft'><ins id='pR1ft'></ins><ul id='pR1ft'></ul><sub id='pR1ft'></sub></form><legend id='pR1ft'></legend><bdo id='pR1ft'><pre id='pR1ft'><center id='pR1ft'></center></pre></bdo></b><th id='pR1ft'></th></span></q></dt></tr></i><div id='pR1ft'><tfoot id='pR1ft'></tfoot><dl id='pR1ft'><fieldset id='pR1ft'></fieldset></dl></div>
            • <legend id='pR1ft'><style id='pR1ft'><dir id='pR1ft'><q id='pR1ft'></q></dir></style></legend>
                • <bdo id='pR1ft'></bdo><ul id='pR1ft'></ul>

                  本文介绍了提取每个x和y的多个数组的最小值和最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试列出4个不同数据集的最小值和最大值。这些数据集是我在实验室做的几个测试的四次多项式拟合。我在下面做了一个示例代码来说明困难是什么。数据集的数组具有不同的长度,并且从不同的x值开始。这就是为什么我没能用一个简单的for循环解决这个问题。

                  绘制的蓝色和红色线条显示最小数组和最大数组在打印时的外观。

                  我希望示例代码中的一切都清楚了。

                  # -*- coding: utf-8 -*-
                  """
                  Created on Mon Oct  4 10:49:21 2021
                  
                  @author: Lodewijk
                  """
                  import math
                  import numpy as np
                  import matplotlib.pyplot as plt
                  from scipy.optimize import curve_fit
                  from numpy import arange
                  
                  #%% Creating X and Y example values
                  XTest1=list(range(0,40))
                  YTest1=np.empty(len(XTest1))
                  XTest2=list(range(10,40))
                  YTest2=np.empty(len(XTest2))
                  XTest3=list(range(2,40))
                  YTest3=np.empty(len(XTest3))
                  XTest4=list(range(5,38))
                  YTest4=np.empty(len(XTest4))
                  for i in range(len(XTest1)):
                      YTest1[i]=math.sin(XTest1[i])
                  for i in range(len(XTest2)):
                      YTest2[i]=3*math.sin(XTest2[i])
                  for i in range(len(XTest3)):
                      YTest3[i]=2*math.sin(XTest3[i])-0.5
                  for i in range(len(XTest4)):
                      YTest4[i]=0.5*math.sin(XTest4[i])+1
                  
                  
                  
                  plt.plot(XTest1,YTest1, label='Data 1')
                  plt.plot(XTest2,YTest2, label='Data 2')
                  plt.plot(XTest3,YTest3, label='Data 3')
                  plt.plot(XTest4,YTest4, label='Data 4')
                  plt.legend()
                  plt.show()
                  #%% Making a 4th order polynomial best fit graph through the data sets
                  
                  
                  
                  def objective_4(x,a,b,c,d,e):
                      return a * x**4 +b*x**3 +c*x**2+d*x+e 
                  pars, cov = curve_fit(objective_4, XTest1,YTest1)
                  x_line1 = arange(min(XTest1), max(XTest1), 1)
                  a, b, c, d, e = pars
                  y_line1 = objective_4(x_line1, a, b, c, d, e)
                  
                  pars, cov = curve_fit(objective_4, XTest2,YTest2)
                  x_line2 = arange(min(XTest2), max(XTest2), 1)
                  a, b, c, d, e = pars
                  y_line2 = objective_4(x_line2, a, b, c, d, e)
                  
                  pars, cov = curve_fit(objective_4, XTest3,YTest3)
                  x_line3 = arange(min(XTest3), max(XTest3), 1)
                  a, b, c, d, e = pars
                  y_line3 = objective_4(x_line3, a, b, c, d, e)
                  
                  pars, cov = curve_fit(objective_4, XTest4,YTest4)
                  x_line4 = arange(min(XTest4), max(XTest4), 1)
                  a, b, c, d, e = pars
                  y_line4 = objective_4(x_line4, a, b, c, d, e)
                  
                              
                  plt.plot(x_line1,y_line1, label='Test1')
                  plt.plot(x_line2,y_line2, label='Test2')
                  plt.plot(x_line3,y_line3, label='Test3')
                  plt.plot(x_line4,y_line4, label='Test4')
                  plt.legend()
                  plt.show()
                      
                  

                  推荐答案

                  执行此操作的一个选项是使用np.nan填充您的数据,以确保它们都具有相同的维度。一旦这样做了,您就可以使用np.nanminnp.nanmax计算最小和最大值,同时丢弃np.nan值。 总体而言,代码如下所示:

                  import math
                  import numpy as np
                  import matplotlib.pyplot as plt
                  from scipy.optimize import curve_fit
                  from numpy import arange
                  
                  #%% Creating X and Y example values
                  XTest1=list(range(0,40))
                  YTest1=np.empty(len(XTest1))
                  XTest2=list(range(10,40))
                  YTest2=np.empty(len(XTest2))
                  XTest3=list(range(2,40))
                  YTest3=np.empty(len(XTest3))
                  XTest4=list(range(5,38))
                  YTest4=np.empty(len(XTest4))
                  for i in range(len(XTest1)):
                      YTest1[i]=math.sin(XTest1[i])
                  for i in range(len(XTest2)):
                      YTest2[i]=3*math.sin(XTest2[i])
                  for i in range(len(XTest3)):
                      YTest3[i]=2*math.sin(XTest3[i])-0.5
                  for i in range(len(XTest4)):
                      YTest4[i]=0.5*math.sin(XTest4[i])+1
                  
                  plt.plot(XTest1,YTest1, label='Data 1')
                  plt.plot(XTest2,YTest2, label='Data 2')
                  plt.plot(XTest3,YTest3, label='Data 3')
                  plt.plot(XTest4,YTest4, label='Data 4')
                  plt.legend()
                  plt.show()
                  #%% Making a 4th order polynomial best fit graph through the data sets
                  
                  
                  
                  def objective_4(x,a,b,c,d,e):
                      return a * x**4 +b*x**3 +c*x**2+d*x+e 
                  pars, cov = curve_fit(objective_4, XTest1,YTest1)
                  x_line1 = arange(min(XTest1), max(XTest1), 1)
                  a, b, c, d, e = pars
                  y_line1 = objective_4(x_line1, a, b, c, d, e)
                  
                  pars, cov = curve_fit(objective_4, XTest2,YTest2)
                  x_line2 = arange(min(XTest2), max(XTest2), 1)
                  a, b, c, d, e = pars
                  y_line2 = objective_4(x_line2, a, b, c, d, e)
                  
                  pars, cov = curve_fit(objective_4, XTest3,YTest3)
                  x_line3 = arange(min(XTest3), max(XTest3), 1)
                  a, b, c, d, e = pars
                  y_line3 = objective_4(x_line3, a, b, c, d, e)
                  
                  pars, cov = curve_fit(objective_4, XTest4,YTest4)
                  x_line4 = arange(min(XTest4), max(XTest4), 1)
                  a, b, c, d, e = pars
                  y_line4 = objective_4(x_line4, a, b, c, d, e)
                  
                              
                  plt.plot(x_line1,y_line1, label='Test1')
                  plt.plot(x_line2,y_line2, label='Test2')
                  plt.plot(x_line3,y_line3, label='Test3')
                  plt.plot(x_line4,y_line4, label='Test4')
                  
                  ######## Padding start #######
                  min_x=min(XTest1[0],XTest2[0],XTest3[0],XTest4[0])
                  max_x=max(XTest1[-1],XTest2[-1],XTest3[-1],XTest4[-1])
                  x=np.arange(min_x,max_x)
                  y_line1_pad=(XTest1[0]-min_x)*[np.nan]+list(y_line1)+(max_x-XTest1[-1])*[np.nan]
                  y_line2_pad=(XTest2[0]-min_x)*[np.nan]+list(y_line2)+(max_x-XTest2[-1])*[np.nan]
                  y_line3_pad=(XTest3[0]-min_x)*[np.nan]+list(y_line3)+(max_x-XTest3[-1])*[np.nan]
                  y_line4_pad=(XTest4[0]-min_x)*[np.nan]+list(y_line4)+(max_x-XTest4[-1])*[np.nan]
                  y_line_pad_all=np.array([y_line1_pad,y_line2_pad,y_line3_pad,y_line4_pad])
                  
                  ####### Compute min and max ######
                  min_y=np.nanmin(y_line_pad_all,axis=0)
                  max_y=np.nanmax(y_line_pad_all,axis=0)
                  
                  ####### PLot min and max ######
                  plt.plot(x,min_y,color='r',ls='--',lw=2,label='min')
                  plt.plot(x,max_y,color='b',ls='--',lw=2,label='max')
                  
                  plt.legend()
                  plt.show()
                  

                  ,输出为:

                  这篇关于提取每个x和y的多个数组的最小值和最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)

                      <small id='3XQ1r'></small><noframes id='3XQ1r'>

                    • <i id='3XQ1r'><tr id='3XQ1r'><dt id='3XQ1r'><q id='3XQ1r'><span id='3XQ1r'><b id='3XQ1r'><form id='3XQ1r'><ins id='3XQ1r'></ins><ul id='3XQ1r'></ul><sub id='3XQ1r'></sub></form><legend id='3XQ1r'></legend><bdo id='3XQ1r'><pre id='3XQ1r'><center id='3XQ1r'></center></pre></bdo></b><th id='3XQ1r'></th></span></q></dt></tr></i><div id='3XQ1r'><tfoot id='3XQ1r'></tfoot><dl id='3XQ1r'><fieldset id='3XQ1r'></fieldset></dl></div>
                    • <tfoot id='3XQ1r'></tfoot>

                        <bdo id='3XQ1r'></bdo><ul id='3XQ1r'></ul>
                            <tbody id='3XQ1r'></tbody>

                            <legend id='3XQ1r'><style id='3XQ1r'><dir id='3XQ1r'><q id='3XQ1r'></q></dir></style></legend>