Python 如何基于另一个数组的元素复制数组的元素?
我有另一个数组Python 如何基于另一个数组的元素复制数组的元素?,python,jupyter-notebook,Python,Jupyter Notebook,我有另一个数组预期值,由未显示的代码生成,示例如下: pop=np.zeros((population_size,chromosome_length)) for i in range(population_size): for j in range(i,chromosome_length): pop[i,j] = random.randint(0, 1) pop array([[0., 1., 0., 1., 1., 1., 0., 0., 1., 1.
预期值
,由未显示的代码生成,示例如下:
pop=np.zeros((population_size,chromosome_length))
for i in range(population_size):
for j in range(i,chromosome_length):
pop[i,j] = random.randint(0, 1)
pop
array([[0., 1., 0., 1., 1., 1., 0., 0., 1., 1.],
[0., 0., 1., 0., 1., 0., 1., 1., 0., 0.],
[0., 0., 1., 0., 0., 1., 1., 0., 0., 1.],
[0., 0., 0., 0., 1., 0., 1., 1., 1., 0.],
[0., 0., 0., 0., 1., 0., 1., 1., 0., 1.],
[0., 0., 0., 0., 0., 0., 0., 1., 0., 0.]])
然后,我想根据自定义间隔对预期值
进行分类:
array([[1.99214608],
[1.45140389],
[0.07068525],
[0.69507167],
[1.08384057],
[0.70685254]])
我希望最终输出为:
actual=np.zeros((population_size,1))
for i in range(len(expected)):
if expected[i]>=1.5:
actual[i]=2
elif 1.5>expected[i]>=0.9:
actual[i]=1
else:
actual[i]=0
actual=actual.astype(int)
total_count=int(np.sum(actual))
print(total_count)
[[2]
[1]
[0]
[0]
[1]
[0]]
4
基于
总计数中的值
。pop
的第一行复制了两次,第二行复制了一次,第五行复制了一次。简而言之,我想要的是基于另一个数组的整数元素重复/复制/复制一个数组的元素。我将尝试在部分中解决这个问题,因为您使用NumPy数组就像它们是列表一样,因此首先会失去库的许多用途。虽然语法更加紧凑,但速度显著提高
创造人口
这个很简单。我们可以通过使用直接替换生成pop
。我们需要指定总体大小
和染色体长度
的值,并使用这些值来指定输出大小
array([[0., 1., 0., 1., 1., 1., 0., 0., 1., 1.],
[0., 1., 0., 1., 1., 1., 0., 0., 1., 1.],
[0., 0., 1., 0., 1., 0., 1., 1., 0., 0.],
[0., 0., 0., 0., 1., 0., 1., 1., 0., 1.]])
注意:这不会给出与实际问题中包含的值完全相同的值,因为我们尚未为随机数生成器设置种子。但是,代码直接等同于for
循环,但性能更高
生成预期值
我不能完全替换这个部分,因为替换循环太多了,有些变量也没有定义。所以,我只是假设我将得到与您所示相同的2D数组:
population_size = 6
chromosome_length = 10
pop = np.random.randint(0, 2, (population_size, chromosome_length))
将数据装箱
这有点复杂。我们可以利用来存储间隔(0、0.9和1.5)之间的数据。但是,这种方法不适用于二维阵列,因此我将首先使用它展平阵列
这将返回的每个值所属的bin标识列表。然而,bin恒等式从1开始,我们想进一步使用这些值作为数组的指示符,所以我还要同时从结果中减去1
expected = np.array([[1.99214608],
[1.45140389],
[0.07068525],
[0.69507167],
[1.08384057],
[0.70685254]])
最后的步骤
我将创建一个与bin类别相对应的值数组。然后,我们可以使用相应的替换值替换dig
的值
bins = np.array([0, 0.9, 1.5])
dig = np.digitize(expected.ravel(), bins) - 1
最后:),我们可以使用actual
以正确的比例从pop
中获取行来构建输出
最终代码
给出:
import numpy as np
population_size = 6
chromosome_length = 10
pop = np.random.randint(0, 2, (population_size, chromosome_length))
# But I'm going to deliberately overwrite the above to solve your particular case
pop = np.array([[0., 1., 0., 1., 1., 1., 0., 0., 1., 1.],
[0., 0., 1., 0., 1., 0., 1., 1., 0., 0.],
[0., 0., 1., 0., 0., 1., 1., 0., 0., 1.],
[0., 0., 0., 0., 1., 0., 1., 1., 1., 0.],
[0., 0., 0., 0., 1., 0., 1., 1., 0., 1.],
[0., 0., 0., 0., 0., 0., 0., 1., 0., 0.]])
# Hard-coded :/
expected = np.array([[1.99214608],
[1.45140389],
[0.07068525],
[0.69507167],
[1.08384057],
[0.70685254]])
bins = np.array([0, 0.9, 1.5])
dig = np.digitize(expected.ravel(), bins) - 1
replacements = np.array([0, 1, 2])
actual = np.take(replacements, dig)
out = np.repeat(pop, actual, axis=0)
print(out)
对不起,什么是预期的?你问题最下面的数组?不,我可以看到你那部分的代码,并且在你的问题中格式正确。我的意思是,名为expected
的变量在哪里定义?最后一个数组是实际的,所以expected
在哪里?我无法验证任何方法,除非我可以根据该集合输入进行验证。请把问题包括在伊托克,划掉它。现在还有更多的未知数。我们需要一个-我不需要代码来生成预期的
,如果你能给我一个代表性的数组,这样我们就可以实际运行代码。我建议删除您刚才添加的代码感谢您的帮助。学到了很多:)
import numpy as np
population_size = 6
chromosome_length = 10
pop = np.random.randint(0, 2, (population_size, chromosome_length))
# But I'm going to deliberately overwrite the above to solve your particular case
pop = np.array([[0., 1., 0., 1., 1., 1., 0., 0., 1., 1.],
[0., 0., 1., 0., 1., 0., 1., 1., 0., 0.],
[0., 0., 1., 0., 0., 1., 1., 0., 0., 1.],
[0., 0., 0., 0., 1., 0., 1., 1., 1., 0.],
[0., 0., 0., 0., 1., 0., 1., 1., 0., 1.],
[0., 0., 0., 0., 0., 0., 0., 1., 0., 0.]])
# Hard-coded :/
expected = np.array([[1.99214608],
[1.45140389],
[0.07068525],
[0.69507167],
[1.08384057],
[0.70685254]])
bins = np.array([0, 0.9, 1.5])
dig = np.digitize(expected.ravel(), bins) - 1
replacements = np.array([0, 1, 2])
actual = np.take(replacements, dig)
out = np.repeat(pop, actual, axis=0)
print(out)
[[0. 1. 0. 1. 1. 1. 0. 0. 1. 1.]
[0. 1. 0. 1. 1. 1. 0. 0. 1. 1.]
[0. 0. 1. 0. 1. 0. 1. 1. 0. 0.]
[0. 0. 0. 0. 1. 0. 1. 1. 0. 1.]]