0

I have an array of numbers called X with shape (1, 19).

Also I have some functions like X, np.log(X), 1/(1+np.exp(-X)) and so on.

I need to get all combinations of arrays with applied functions on each item in array.

For example:

X = [ 1 2 3 ]

Y[1] = [ 1 2 3] Y[2] = [ ln(1) 2 3 ] Y[3] = [ 1 ln(2) 3 ] ... Y[k] = [ ln(1) ln(2) ln(3) ] ... Y[m] = [ 1 ln(2) exp(3)] ... Y[n] = [ exp(1) exp(2) exp(3) ]

Is it possible?

  • loop through the list and run all your functions one by one – midori Jan 29 '16 at 19:28
  • this way isn't possible to get result like this: [ ln(1) 2 ln(3) ] isn't it? – Vladislav Denisov Jan 29 '16 at 19:39
  • It is possible to create arrays with functions applied on all elements like Y[1] = X, Y[2] = ln(X), Y[3] = exp(X) and then apply [this method](http://stackoverflow.com/questions/798854/all-combinations-of-a-list-of-lists) but I don't think that it will be a good code – Vladislav Denisov Jan 29 '16 at 19:44
  • That's the only way to do it... I'm actually trying that right now :) – OneCricketeer Jan 29 '16 at 19:45
  • 1
    If you have a list of N numbers and M functions to apply like that, I belive that will be M^N different combinations. In the case of N=19 and M=3, that will be 1.2 billion arrays – Vidar Jan 29 '16 at 19:48
  • The `itertools` module will help you. – Caridorc Jan 29 '16 at 19:50
  • @Vidar You're right! In my case N=19 and M=5, but I don't need to store it all in memory, I need to get only top5 r values of regression with this parameters. :) – Vladislav Denisov Jan 29 '16 at 19:59

3 Answers3

0

This is an example that does exactly what you want, but on a much smaller X and with 2 functions:

import math as m
import numpy as np
import itertools

def RetX(x):
    return x

def RetLogX(x):
    return m.log(x)

if __name__ == '__main__':
    funcs = [RetX, RetLogX]
    funcsIdx = np.arange(len(funcs))

    arrayNumbers = [1, 2, 3]
    for combination in itertools.product(funcsIdx, repeat=len(arrayNumbers)):
        result = []
        for idx in range(len(combination)):
            func = funcs[combination[idx]]
            result.append(func(arrayNumbers[idx]))
        print(result)
halfer
  • 19,824
  • 17
  • 99
  • 186
0

Here is an answer using list-comprehension

import itertools
import math

def iden(x):
    return x

def log(x):
    return math.log(x)

def exp(x):
    return 1/(1+math.exp(-x))

X = [1, 2, 3]
funcs = [iden, log, exp]

def apply(functions, values):
    return [func(val) for func,val in zip(functions, values)]

values = [apply(f, X) for f in itertools.product(funcs, repeat=len(funcs))]

The apply function seemed useful. If you don't want it, then this will do

values = [[func(val) for func,val in zip(f, X)] for f in itertools.product(funcs, repeat=len(funcs))]

Sample output

[1, 2, 3] # (iden, iden, iden)
[1, 2, 1.0986122886681096] # (iden, iden, log)
[1, 2, 0.95257412682243336] # (iden, iden, exp)
[1, 0.69314718055994529, 3] # (iden, log, iden)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0
functions=np.array([np.abs,lambda x : -x,np.square])
values=np.arange(3)
nf,nv=len(functions),len(values)
select=[[int(x) for x in np.base_repr(k,nf,nv)[-nv:]] for k in range(nf**nv)]
Y=[[f(x) for f,x in zip(functions[s],values)] for s in select]

select have nf**nv elements:

[[0, 0, 0],
 [0, 0, 1],
 [0, 0, 2],
 [0, 1, 0],
 [0, 1, 1],
 [0, 1, 2],
 [0, 2, 0],
 [0, 2, 1],
 [0, 2, 2],
 [1, 0, 0],
 [1, 0, 1],
 [1, 0, 2],
 [1, 1, 0],
 [1, 1, 1],
 [1, 1, 2],
 [1, 2, 0],
 [1, 2, 1],
 [1, 2, 2],
 [2, 0, 0],
 [2, 0, 1],
 [2, 0, 2],
 [2, 1, 0],
 [2, 1, 1],
 [2, 1, 2],
 [2, 2, 0],
 [2, 2, 1],
 [2, 2, 2]]

Then Y is what you want.

B. M.
  • 18,243
  • 2
  • 35
  • 54