从 matlab 转换为 python/numpy/ 的一般问题
General issues translating from matlab to python/numpy/
我正在尝试 'translate' 使用 numpy python 的工作 matlab 脚本。
在matlab代码中生成了如下的某种变量:
GA.Ng=2; % number of genes
GA.Np=Np; % size of population
GA.NG=NG; % number of generations
GA.pc=0.5; % probability of crossover
GA.alpha=0.5; % blend ratio for crossover
GA.pm=0.1; % probability of a gene being mutated
GA.xmn=[0 0]; % vector of minimum values for unnormalized genes
GA.xmx=[5 5]; % vector of maximum values for unnormalized genes
如何在 python 中实现此目的?我试过了,但没有成功:
def example1p6A(NG, Np, rf, pf):
GA = np.zeros(1, dtype = [('Ng', int),
('Np', int),
('NG', int),
('pc', int),
('alpha', float),
('pm', int),
('xmin', float),
('xmax', float)])
GA['Ng'] = 2 # Number of genes
GA['Np'] = Np # size of population
GA['NG'] = NG # number of generations
GA['pc'] = 0.5 # probability of crossover
GA['alpha'] = 0.5 # blend ratio for crossover
GA['pm'] = 0.1 # probability of a gene being mutated
GA['xmin'] = np.array([0, 0]) # vector of minimum values for unnormalised genes
GA['xmax'] = np.array([5, 5]) # vector of maximum values for unnormalised genes
# Init population:
P = np.random.rand(5,5)
#return (GA['Ng'][0], Np, rf, pf)
return P
我收到错误消息
ValueError: could not broadcast input array from shape (2) into shape (1)
在 Python 中,您可以使用 dictionary:
def example1p6A(NG, Np, rf, pf):
GA = dict(Ng=2,
Np=Np,
NG=NG,
pc=0.5,
alpha=0.5,
pm=0.1,
xmn=[0, 0],
xmx=[5, 5])
P = np.random.rand(5,5)
return (GA['Ng'][0], Np, rf, pf)
问题是您将 xmin
和 xmax
定义为 float
,但您试图将它们分配为数组。所以这就是你收到错误的原因。您正在尝试将“来自形状 (2) 的输入数组”分配给具有“形状 (1)”的对象。所以,解决方案是将xmin
和xmax
定义为float
的数组。下面是一个可以使其正常工作的示例。
def example1p6A(NG, Np, rf, pf):
GA = np.zeros(1, dtype = [('Ng', int),
('Np', int),
('NG', int),
('pc', int),
('alpha', float),
('pm', int),
('xmin', (float, (2,))),
('xmax', (float, (2,)))])
GA['Ng'] = 2 # Number of genes
GA['Np'] = Np # size of population
GA['NG'] = NG # number of generations
GA['pc'] = 0.5 # probability of crossover
GA['alpha'] = 0.5 # blend ratio for crossover
GA['pm'] = 0.1 # probability of a gene being mutated
GA['xmin'] = np.array([0, 0]) # vector of minimum values for unnormalised genes
GA['xmax'] = np.array([5, 5]) # vector of maximum values for unnormalised genes
# Init population:
P = np.random.rand(5,5)
#return (GA['Ng'][0], Np, rf, pf)
return P
有关这方面的更多信息,请查看此 link:
我正在尝试 'translate' 使用 numpy python 的工作 matlab 脚本。
在matlab代码中生成了如下的某种变量:
GA.Ng=2; % number of genes
GA.Np=Np; % size of population
GA.NG=NG; % number of generations
GA.pc=0.5; % probability of crossover
GA.alpha=0.5; % blend ratio for crossover
GA.pm=0.1; % probability of a gene being mutated
GA.xmn=[0 0]; % vector of minimum values for unnormalized genes
GA.xmx=[5 5]; % vector of maximum values for unnormalized genes
如何在 python 中实现此目的?我试过了,但没有成功:
def example1p6A(NG, Np, rf, pf):
GA = np.zeros(1, dtype = [('Ng', int),
('Np', int),
('NG', int),
('pc', int),
('alpha', float),
('pm', int),
('xmin', float),
('xmax', float)])
GA['Ng'] = 2 # Number of genes
GA['Np'] = Np # size of population
GA['NG'] = NG # number of generations
GA['pc'] = 0.5 # probability of crossover
GA['alpha'] = 0.5 # blend ratio for crossover
GA['pm'] = 0.1 # probability of a gene being mutated
GA['xmin'] = np.array([0, 0]) # vector of minimum values for unnormalised genes
GA['xmax'] = np.array([5, 5]) # vector of maximum values for unnormalised genes
# Init population:
P = np.random.rand(5,5)
#return (GA['Ng'][0], Np, rf, pf)
return P
我收到错误消息
ValueError: could not broadcast input array from shape (2) into shape (1)
在 Python 中,您可以使用 dictionary:
def example1p6A(NG, Np, rf, pf):
GA = dict(Ng=2,
Np=Np,
NG=NG,
pc=0.5,
alpha=0.5,
pm=0.1,
xmn=[0, 0],
xmx=[5, 5])
P = np.random.rand(5,5)
return (GA['Ng'][0], Np, rf, pf)
问题是您将 xmin
和 xmax
定义为 float
,但您试图将它们分配为数组。所以这就是你收到错误的原因。您正在尝试将“来自形状 (2) 的输入数组”分配给具有“形状 (1)”的对象。所以,解决方案是将xmin
和xmax
定义为float
的数组。下面是一个可以使其正常工作的示例。
def example1p6A(NG, Np, rf, pf):
GA = np.zeros(1, dtype = [('Ng', int),
('Np', int),
('NG', int),
('pc', int),
('alpha', float),
('pm', int),
('xmin', (float, (2,))),
('xmax', (float, (2,)))])
GA['Ng'] = 2 # Number of genes
GA['Np'] = Np # size of population
GA['NG'] = NG # number of generations
GA['pc'] = 0.5 # probability of crossover
GA['alpha'] = 0.5 # blend ratio for crossover
GA['pm'] = 0.1 # probability of a gene being mutated
GA['xmin'] = np.array([0, 0]) # vector of minimum values for unnormalised genes
GA['xmax'] = np.array([5, 5]) # vector of maximum values for unnormalised genes
# Init population:
P = np.random.rand(5,5)
#return (GA['Ng'][0], Np, rf, pf)
return P
有关这方面的更多信息,请查看此 link: