问题
I have 9 arrays that I want to manipulate to find all possible combinations, so that the name of the resulting array tells me which arrays have been combined. For example:
a1_a2 = array1 - array2
a1_a3 = array1 - array3
a1_a4 = array1 - array4
.
.
.
a9_a6 = array9 - array6
a9_a7 = array9 - array7
a9_a8 = array9 - array8
Obviously I could hardcode it, but how could I do it in a loop?
I thought of writing a function for it, something like:
def combineArrays(array1, array2):
result_name = name_of_array1 + '_' + name_of_array2 # How would I do this bit?
result = array1 - array2
return result
for firstArray in arrays:
for secondArray in reversed(arrays): # to go backwards through the list of arrays
combineArrays(firstArray, secondArray)
As an example, the following arrays:
array1 = [1,2,nan,4,5]
array2 = [5,4,3,2,1]
array3 = [2,4,6,8,10]
should produce the output
a1_a2 = [-4,-2,nan,2,4]
a1_a3 = [-2,-4,nan,-8,-10]
a2_a1 = [4,2,nan,-2,-4]
a2_a3 = [2,-2,-6,-10,-14]
a3_a2 = [-2,2,6,10,14]
a3_a1 = [2,4,nan,8,10]
so, element-wise subtraction for each possible combination of arrays.
Since I'm using numpy
, is there even a linear algebra-type way to do this? And how can I get the program to name the arrays for me?
This question seems to be asking something similar, but I don't understand the answer.
回答1:
This is a generalized code that works with any function: You might go through it to understand but it does what you exactly want:
def factorial(n):
if n==0:
return 1
else:
return n*factorial(n-1)
def choose(n,m):
if n < m:
raise ValueError("n is less than m!")
if m < 0:
raise ValueError("m cannot be negative!!")
else:
return int(factorial(n)/factorial(m)/factorial(n-m))
def combn(x, m, FUN = None):
import numpy as np
if not isinstance(m,int):
raise ValueError("m must be a single integer")
if isinstance(x, int) and x > 0:
x = np.arange(1, x+1)
n, x, e, h, a, nofun = len(x), np.array(x), 0, m, np.arange(1,m+1), FUN==None
if not nofun and not callable(FUN):
raise TypeError("'FUN' must be a function or None")
if (nofun):r = x[a-1]
else: r = FUN(x[a-1])
out = [None] * choose(n, m)
out[0] = r
if m > 0:
i = 2
nmmp1 = n - m + 1
while a[0] != nmmp1:
if e < n - h:
h, e, j = 1, a[m-1], 1
else:
e = a[m - h-1]
h += 1
j = np.arange(1,h+1)
a[m - h + j-1] = e + j
if (nofun): r = x[a-1]
else: r = FUN(x[a-1])
out[i-1] = r
i += 1
return out
You can save this for future use:. Now after this you can do any combinations that you want:
combn(4,3)
Out[854]: [array([1, 2, 3]), array([1, 2, 4]), array([1, 3, 4]), array([2, 3, 4])]
combn([1,2,3,4],3)
Out[855]: [array([1, 2, 3]), array([1, 2, 4]), array([1, 3, 4]), array([2, 3, 4])]
combn([1,2,3,4],3,sum)
Out[856]: [6, 7, 8, 9]
In your case, you could do:
array1 = [1,2,np.nan,4,5]
array2 = [5,4,3,2,1]
array3 = [2,4,6,8,10]
combn([array1,array2,array3],2,lambda x: x[0]-x[1])
Out[859]:
[array([-4., -2., nan, 2., 4.]),
array([-1., -2., nan, -4., -5.]),
array([ 3., 0., -3., -6., -9.])]
Now run the same code with lambda x:x[1]-x[0]
thus giving you all the combinations that you want
来源:https://stackoverflow.com/questions/51529995/generate-combinations-of-arrays