基于一个数组值的插值

时间:2017-02-09 08:43:55

标签: python numpy scipy interpolation

我有两个带值的数组:

library(quantmod)
library(shiny)

date_range <- as.POSIXct(index(data))
if (interactive()) {
  options(device.ask.default = FALSE)
  ui <- fluidPage(  
    titlePanel("Select Range to zoom-in:"),
    sidebarLayout(
      sidebarPanel(
        dateRangeInput("Range", "Choose Date Range:", min=first(date_range),
                   max=last(date_range), format = "dd-mm-yyyy")
      ),
      mainPanel(
        plotOutput("Plot")
      )
    )
  )

  server <- function(input, output) {
    output$Plot <- renderPlot({
      chartSeries(data, type = c("auto", "candlesticks", "matchsticks",   "bars","line"), 
              theme=chartTheme("white"), name=paste(start(data), end(data),sep = " ")) 
      zoomChart(dateRangeInput) 
    })
  }
   shinyApp(ui, server)
}

我想评估一下这个函数:

x = np.array([100, 123, 123, 118, 123])
y = np.array([12, 1, 14, 13])

所以,我想填写def func(a, b): return a*0.8 * (b/2) 缺失值。

我正在使用:

y

现在,我不知道如何从这里继续。如果我尝试:

import numpy as np
from scipy import interpolate

def func(a, b):
    return a*0.8 * (b/2)


x = np.array([100, 123, 123, 118, 123])
y = np.array([12, 1, 14, 13])

X, Y = np.meshgrid(x, y)

Z = func(X, Y)

f = interpolate.interp2d(x, y, Z, kind='cubic')

Znew充满了纳米价值。

另外,我想反过来说。

如果xnew = np.linspace(0,150,10) ynew = np.linspace(0,150,10) Znew = f(xnew, ynew) 小于x,我想总是根据x值进行插值。

所以,例如:

y x = np.array([1,3,4])

我想现在从y中删除值。

我该如何处理?

1 个答案:

答案 0 :(得分:1)

要从1d数组进行插值,您可以使用np.interp,如下所示:

np.interp(np.linspace(0,1,len(x)), np.linspace(0,1,len(y)),y)

您可以查看documentation了解完整详情,但简而言之:

  • 考虑您的数组y具有从0到1的引用值(示例[5,2,6,3,9]将具有索引[0,0.25,0.5,0.75,1]
  • 函数的第二个和第三个参数是索引和向量y
  • 第一个参数是y
  • 的插值的索引

作为一个例子:

>>> y = [0,5]
>>> indexes = [0,1]
>>> new_indexes = [0,0.5,1]
>>> np.interp(new_indexes, indexes, y)
[0,2.5,5]