0

I currently have this code:

import numpy as np

def mapfeature_binominal_array(x1,x2,n=6,list=[]):

    if len(list)==0:
        list.append(x1)
        list.append(x2)

    if n==1:
        print(np.array(list))
        return np.array(list)
    else:
        for i in range(0,n+1):
            formula=((x1)**(n-i))*((x2)**(i))
            list.append(formula)


        mapfeature_binominal_array(x1,x2,n-1,list)

test_array=mapfeature_binominal_array(-0.25,1.5)

which prints out the following output:

[ -2.50000000e-01   1.50000000e+00   2.44140625e-04  -1.46484375e-03
   8.78906250e-03  -5.27343750e-02   3.16406250e-01  -1.89843750e+00
   1.13906250e+01  -9.76562500e-04   5.85937500e-03  -3.51562500e-02
   2.10937500e-01  -1.26562500e+00   7.59375000e+00   3.90625000e-03
  -2.34375000e-02   1.40625000e-01  -8.43750000e-01   5.06250000e+00
  -1.56250000e-02   9.37500000e-02  -5.62500000e-01   3.37500000e+00
   6.25000000e-02  -3.75000000e-01   2.25000000e+00]

However, when i try:

print(test_array)

I get None.

I am wondering why it does not assign the array to test_array.

kbunarjo
  • 1,277
  • 2
  • 11
  • 27
gskang
  • 27
  • 1
  • 9
  • You recursively call 6->5->4->3->2->1, but return value only when returning 1->2, but not 2->3-> etc. – VPfB Oct 11 '16 at 06:40

2 Answers2

1

You never return the result of the recursive call:

else:
    for i in range(0,n+1):
        formula=((x1)**(n-i))*((x2)**(i))
        list.append(formula)

    mapfeature_binominal_array(x1,x2,n-1,list)

The function just ends there without a return so None is returned instead.

Add return there:

else:
    for i in range(0,n+1):
        formula=((x1)**(n-i))*((x2)**(i))
        list.append(formula)

    return mapfeature_binominal_array(x1,x2,n-1,list)

A recursive function call is just like any other function call; you still need to do something with the result of that function call; the value it returns does not automatically propagate to be the return value of the outer call.

You are also falling into the default mutable argument value trap; you set list=[] in the function signature and append to that. The values in that list persist across calls, so future calls to the function will still have those values from previous calls, and you'll get wrong results.

Use a sentinel like None instead, and try to avoid using the names of built-in types like list:

def mapfeature_binominal_array(x1, x2, n=6, lst=None):
    if lst is None:
        lst = [x1, x2]

You can immediately set that initial list to [x1, x2].

The corrected code then runs and produces your results:

>>> import numpy as np
>>> def mapfeature_binominal_array(x1, x2, n=6, lst=None):
...     if lst is None:
...         lst = [x1, x2]
...     if n == 1:
...         return np.array(lst)
...     else:
...         for i in range(n + 1):
...             formula = (x1 ** (n - i)) * (x2 ** i)
...             lst.append(formula)
...         return mapfeature_binominal_array(x1, x2, n - 1, lst)
...
>>> mapfeature_binominal_array(-0.25, 1.5)
array([ -2.50000000e-01,   1.50000000e+00,   2.44140625e-04,
        -1.46484375e-03,   8.78906250e-03,  -5.27343750e-02,
         3.16406250e-01,  -1.89843750e+00,   1.13906250e+01,
        -9.76562500e-04,   5.85937500e-03,  -3.51562500e-02,
         2.10937500e-01,  -1.26562500e+00,   7.59375000e+00,
         3.90625000e-03,  -2.34375000e-02,   1.40625000e-01,
        -8.43750000e-01,   5.06250000e+00,  -1.56250000e-02,
         9.37500000e-02,  -5.62500000e-01,   3.37500000e+00,
         6.25000000e-02,  -3.75000000e-01,   2.25000000e+00])
Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

You forgot to return the result of mapfeature_binominal_array(x1,x2,n-1,list).

Philip Tzou
  • 5,926
  • 2
  • 18
  • 27