Python 查找满足阈值关系的组合
给定Python 查找满足阈值关系的组合,python,loops,combinations,combinatorics,python-collections,Python,Loops,Combinations,Combinatorics,Python Collections,给定phi、theta、n_1和n_2的值,我需要找到满足以下条件的所有可能对(n_1,n_2): 0 <= N_1 <= n_1 0 <= N_2 <= n_2 N_1 - phi * N_2 >= theta 0这是有效的: import itertools def _get_N_1_and_N_2(n_1, n_2, phi, theta): """Get the (N_1, N_2) pairs as defined in Griffith (1
phi
、theta
、n_1
和n_2
的值,我需要找到满足以下条件的所有可能对(n_1
,n_2
):
0 <= N_1 <= n_1
0 <= N_2 <= n_2
N_1 - phi * N_2 >= theta
0这是有效的:
import itertools
def _get_N_1_and_N_2(n_1, n_2, phi, theta):
"""Get the (N_1, N_2) pairs as defined in Griffith (1963).
See Equation 3.
Parameters
----------
n_1 : integer
Number of excitatory inputs.
n_2 : integer
Number of inhibitory inputs.
phi : number
Factor that captures the difference between excitatory and
inhibitory synaptic efficiencies.
theta : number
Spike threshold.
"""
N_1 = range(n_1 + 1)
N_2 = range(n_2 + 1)
possible_N_1_N_2 = itertools.product(N_1, N_2)
N_1_N_2 = []
for N_1, N_2 in possible_N_1_N_2:
if N_1 - phi * N_2 >= theta:
N_1_N_2.append((N_1, N_2))
return N_1_N_2
我想我可以把for
循环成if
语句,这是一个非常混乱的列表。但是naa
以下是测试:
import nose.tools as nt
def test__get_N_1_and_N_2():
# Figure 3A in Griffith, 1963, Biophy J.
n_1 = 4
n_2 = 0
theta = 2
phi = 1
desired = [(2, 0), (3, 0), (4, 0)]
actual = _get_N_1_and_N_2(n_1, n_2, phi, theta)
nt.assert_list_equal(desired, actual)
# Figure 3B.
n_1 = 5
n_2 = 1
theta = 2
phi = 2
desired = [(2, 0), (3, 0), (4, 0), (4, 1), (5, 0), (5, 1)]
actual = _get_N_1_and_N_2(n_1, n_2, phi, theta)
nt.assert_list_equal(desired, actual)
您可以使用numpy和矢量化,类似于下面的内容
import numpy as np
phi = 0.5
theta = 1
n1 = 10
n2 = 20
N1 = np.random.randint(-100, 100, size=100)
N2 = np.random.randint(-100, 100, size=100)
N1 = N1[(N1 >= 0) & (N1 <= n1)]
N2 = N2[(N2 >= 0) & (N2 <= n2)]
a = N2 * theta + phi
res = N1.reshape(N1.shape[0], 1) - a.reshape(1, a.shape[0])
indices = np.argwhere(res >= 0)
pairs = zip(N1[indices[:,0]], N2[indices[:,1]])
根据@dbliss请求,以下是模块化版本及其测试
import numpy as np
def calc_combination(N1, N2, n1, n2, theta, phi):
N1 = N1[(N1 >= 0) & (N1 <= n1)]
N2 = N2[(N2 >= 0) & (N2 <= n2)]
a = N2 * theta + phi
res = N1.reshape(N1.shape[0], 1) - a.reshape(1, a.shape[0])
indices = np.argwhere(res >= 0)
pairs = zip(N1[indices[:,0]], N2[indices[:,1]])
return pairs
def test_case():
n1 = 5
n2 = 1
theta = 2
phi = 2
N1 = np.arange(n1 + 1)
N2 = np.arange(n2 + 1)
assert (calc_combination(N1, N2, n1, n2, theta, phi) ==
[(2, 0), (3, 0), (4, 0), (4, 1), (5, 0), (5, 1)])
test_case()
将numpy导入为np
def calc_组合(N1、N2、N1、N2、θ、φ):
N1=N1[(N1>=0)和(N1=0)和(N2=0)
pairs=zip(N1[索引[:,0]],N2[索引[:,1]]
返回对
def测试用例():
n1=5
n2=1
θ=2
φ=2
N1=np.arange(N1+1)
N2=np.arange(N2+1)
断言(计算单位组合(N1,N2,N1,N2,θ,φ)==
[(2, 0), (3, 0), (4, 0), (4, 1), (5, 0), (5, 1)])
测试用例()
首先,您可以将最后一个不等式中的(-phi*N_2)
从左到右移动:N_1>=theta+phi*N_2
,这定义了N_1
的下界。其次是phi
,theta
,N_1
,N_2
,N整数(1) 是的。(2)所有的n
s和n
s都是非负整数。phi
和theta
不一定是整数。phi
必须是正的。看起来theta
通常是非负的。我只是编了一些数字
import numpy as np
def calc_combination(N1, N2, n1, n2, theta, phi):
N1 = N1[(N1 >= 0) & (N1 <= n1)]
N2 = N2[(N2 >= 0) & (N2 <= n2)]
a = N2 * theta + phi
res = N1.reshape(N1.shape[0], 1) - a.reshape(1, a.shape[0])
indices = np.argwhere(res >= 0)
pairs = zip(N1[indices[:,0]], N2[indices[:,1]])
return pairs
def test_case():
n1 = 5
n2 = 1
theta = 2
phi = 2
N1 = np.arange(n1 + 1)
N2 = np.arange(n2 + 1)
assert (calc_combination(N1, N2, n1, n2, theta, phi) ==
[(2, 0), (3, 0), (4, 0), (4, 1), (5, 0), (5, 1)])
test_case()