如何使用numpy正确地向量化一个函数?

时间:2015-09-08 14:29:04

标签: arrays python-3.x numpy

我是numpy的新手,我必须在这里做一些蠢事,但我想要的只是生成一个4维概率分布数组。我不明白为什么我的矢量化函数会返回这个声称属于np.ndarray类但不像一个类似的奇怪对象。此外,当我拨打self.inputSpace[:,0]时,它会返回错误。

这里是test.py的全部内容:

import numpy as np

def generateDist(i,j,k):
    return np.squeeze(np.array([i*j,i*(1-j),(1-i)*k,(1-i)*(1-k)]))

generateDist = np.vectorize(generateDist,otypes=[np.ndarray])

class distributionSpace():
    def __init__(self):
        self.grid = 3 # set to 3 for simplicity
        self.inputSpace = np.array([])

    def generateDistribution(self):
        alpha = np.linspace(0.,1.,self.grid)
        beta = np.linspace(0.,1.,self.grid)
        gamma = np.linspace(0.,1.,self.grid)
        i , j , k = np.meshgrid(alpha,beta,gamma)
        i = np.squeeze(i.flatten())
        j = np.squeeze(j.flatten())
        k = np.squeeze(k.flatten())
        self.inputSpace = generateDist(i,j,k)
        print(self.inputSpace)
        return self

if __name__ == '__main__':
    distributionSpace().generateDistribution()

这是我得到的结果:

$ python3 test.py 
[array([ 0.,  0.,  0.,  1.]) array([ 0. ,  0. ,  0.5,  0.5])
 array([ 0.,  0.,  1.,  0.]) array([ 0. ,  0.5,  0. ,  0.5])
 array([ 0.  ,  0.5 ,  0.25,  0.25]) array([ 0. ,  0.5,  0.5,  0. ])
 array([ 0.,  1.,  0.,  0.]) array([ 0.,  1.,  0.,  0.])
 array([ 0.,  1.,  0.,  0.]) array([ 0.,  0.,  0.,  1.])
 array([ 0. ,  0. ,  0.5,  0.5]) array([ 0.,  0.,  1.,  0.])
 array([ 0.25,  0.25,  0.  ,  0.5 ]) array([ 0.25,  0.25,  0.25,  0.25])
 array([ 0.25,  0.25,  0.5 ,  0.  ]) array([ 0.5,  0.5,  0. ,  0. ])
 array([ 0.5,  0.5,  0. ,  0. ]) array([ 0.5,  0.5,  0. ,  0. ])
 array([ 0.,  0.,  0.,  1.]) array([ 0. ,  0. ,  0.5,  0.5])
 array([ 0.,  0.,  1.,  0.]) array([ 0.5,  0. ,  0. ,  0.5])
 array([ 0.5 ,  0.  ,  0.25,  0.25]) array([ 0.5,  0. ,  0.5,  0. ])
 array([ 1.,  0.,  0.,  0.]) array([ 1.,  0.,  0.,  0.])
 array([ 1.,  0.,  0.,  0.])]

1 个答案:

答案 0 :(得分:0)

在这里为搜索的人找到答案: Using Numpy Vectorize on Functions that Return Vectors

TL; DR:

output$loadStatusIndicator = renderUI({
  worked = T
  a = tryCatch(dget(input$loadSavedData$datapath),error=function(x){worked<<-F})
  if(worked){
    #User specified options
    a$sproutData$transformations->sproutData$transformations  #user specified transformations
    a$sproutData$processing->sproutData$processing  #user specified text processing rules
    updateCheckboxGroupInput(session,"processingOptions",selected=sproutData$processing)
    a$sproutData$sc->sproutData$sc  #user specified option to spell check
    updateCheckboxInput(session,"spellCheck",value = sproutData$sc)
    a$sproutData$scOptions->sproutData$scOptions  #user specified spell check options (only used if spell check is turned on)
    updateCheckboxGroupInput(session,"spellCheckOptions",selected=sproutData$scOptions)
    a$sproutData$scLength->sproutData$scLength  #user specified min word lenght for spell check (only used if spell check is turned on)
    updateNumericInput(session,"spellCheckMinLength",value=sproutData$scLength)
    a$sproutData$stopwords->sproutData$stopwords  #user specified stopwords
    a$sproutData$stopwordsLastChoice->sproutData$stopwordsLastChoice
    if(sproutData$stopwordsLastChoice[1] == ""){
      updateSelectInput(session,"stopwordsChoice",selected="none")
    } else if(all(sproutData$stopwordsLastChoice == stopwords('en'))){
      updateSelectInput(session,"stopwordsChoice",selected="en")
    } else if(all(sproutData$stopwordsLastChoice == stopwords('SMART'))){
      updateSelectInput(session,"stopwordsChoice",selected="SMART")
    }
    HTML("<strong>Loaded data!</strong>")
  } else if (!is.null(input$loadSavedData$datapath)) {
    HTML(paste("<strong>Not a valid save file</strong>"))
  }
})