多表用于以更类似于python的方式相互依赖的循环
亲爱的飞越者: 我在一个循环中编写了一个工作循环,该循环通过列表中的nArray。我想知道是否有更优雅的“pythonisch”解决方案 如果有一个列表理解的解决方案,你能一步一步地解释一下吗?这样我就可以跟着学习了?我试过了,但是依赖于for循环的for循环对我不起作用多表用于以更类似于python的方式相互依赖的循环,python,numpy,for-loop,list-comprehension,Python,Numpy,For Loop,List Comprehension,亲爱的飞越者: 我在一个循环中编写了一个工作循环,该循环通过列表中的nArray。我想知道是否有更优雅的“pythonisch”解决方案 如果有一个列表理解的解决方案,你能一步一步地解释一下吗?这样我就可以跟着学习了?我试过了,但是依赖于for循环的for循环对我不起作用 我有一个列表y_,其中每个列表元素I都是j元素的数组 我有一个不同重量值的dict。这些值的键可以在y_中找到。标记的y_还包含一个掩蔽值0 [在这里,我试图设置get(key,default=0),但这产生了一个错误,所以
- 我有一个列表y_,其中每个列表元素I都是j元素的数组
- 我有一个不同重量值的dict。这些值的键可以在y_中找到。标记的y_还包含一个掩蔽值0 [在这里,我试图设置get(key,default=0),但这产生了一个错误,所以我使用了if]
y_labeled=list([1,2,2,4,4,1,1],[2,1,1,4,4,0,0],[2,2,2,2,0,0,0])
weights_dict={1:5, 2:6, 4:19}
结果:
weight=list([5,6,6,19,19,5,5],[6,5,5,19,19,0,0],[6,6,6,6,0,0,0])
使用
numpy
数组索引的解决方案:
import numpy as np
y_labeled=[np.array([1,2,2,4,4,1,1]),
np.array([2,1,1,4,4,0,0]),
np.array([2,2,2,2,0,0,0])]
# Convert dictionary to numpy array
weights_dict={1:5, 2:6, 4:19}
weight_vector = np.zeros(max(weights_dict.keys()) + 1)
for k, v in weights_dict.items():
weight_vector[k] = v
print("Weight vector")
print(weight_vector)
# use y_labeled to select the coefficients of weight_matrix
weight = [weight_vector[y] for y in y_labeled]
print("Output weight")
for w in weight:
print(w)
输出:
Weight vector
[ 0. 5. 6. 0. 19.]
Output weight
[ 5. 6. 6. 19. 19. 5. 5.]
[ 6. 5. 5. 19. 19. 0. 0.]
[6. 6. 6. 6. 0. 0. 0.]
使用嵌套:
终于明白了这个话题。。。。 下面是一个使用numpy和Ndarray的解决方案
# -*- coding: utf-8 -*-
import numpy as np
y_labeled = [np.random.randint(low = 0, high = 10, size = (5, 2)),
np.random.randint(low = 0, high = 10, size = (5, 2)),
np.random.randint(low = 0, high = 10, size = (5, 2))]
weights = np.random.randint(low = 0, high = 10, size = (10, ))
weights_dict = {i:weights[i] for i in range(10)}
results = list()
for y_mat in y_labeled:
weight_matrix = np.zeros(shape = y_mat.shape)
for i in range(10):
weight_matrix[y_mat == i] = weights_dict[i]
results.append(weight_matrix)
请参见主帖子的编辑。提前谢谢我会看一看,我现在唯一能说的是数组元素上的循环效率非常低。numpy的兴趣在于你可以在阵列范围内执行操作。亲爱的Mathieu,我也这么认为。经过多年的Matlab学习,我的大脑仍然处于“错误”的状态。整个帖子的目的是给我的大脑重新布线:-)谢谢你提供了更多的细节。。。我的意思是上面的代码甚至不能工作<代码>tmp2?何时初始化重量?当你说:“每个列表元素i是j元素的一个数组”时,数组的形状是什么?是1D吗?天哪,对不起。只需在循环之前调用tmp2=[],对于weight=list()也是如此。我会马上编辑这个。谢谢你的贡献。也许我对你说过。反之亦然:y_标签列表(数组([…]),数组([…])。所以y_标记的每个列表元素都是一个1D数组。亲爱的Vincent,看起来非常干净,谢谢。coff coff,我得到了一个错误,因为实际上每个列表元素(x,1)都有一个2D数组,而不是(x,)。很抱歉我必须在哪里应用.flatte()?
y\u label=[elt.flatte()for elt in y\u label]
可能会起作用?或者子列表中的x.flatte()
亲爱的Mathieu,Vincent[[weights_dict.get(x,0)for x in sublist.flatte()]for sublist in y_label]完成了这项工作。你是最棒的。P.s.马修,你上面的回答很有魅力。我选择Vincents作为最终答案,因为它与单线解决方案相比更具蟒蛇式。多亏了你们所有人。@FloridaMan你们也可以用%timeit测试这两个。这一个的优点是它与NDarray(无论大小和尺寸)一起工作,并且它使用以C。。。而且比列表理解快得多。Vincent的答案对于1D来说很好,对于1D来说可能更快。谢谢你的澄清。我肯定会重用你的代码,因为我多次遇到这个问题,NDarryas N>1。非常感谢您提供了更通用的代码。
>>> y_labeled = [[1,2,2,4,4,1,1], [2,1,1,4,4,0,0], [2,2,2,2,0,0,0]]
>>> weights_dict = {1:5, 2:6, 4:19}
>>> [[weights_dict.get(x, 0) for x in sublist] for sublist in y_labeled]
[[5, 6, 6, 19, 19, 5, 5], [6, 5, 5, 19, 19, 0, 0], [6, 6, 6, 6, 0, 0, 0]]
# -*- coding: utf-8 -*-
import numpy as np
y_labeled = [np.random.randint(low = 0, high = 10, size = (5, 2)),
np.random.randint(low = 0, high = 10, size = (5, 2)),
np.random.randint(low = 0, high = 10, size = (5, 2))]
weights = np.random.randint(low = 0, high = 10, size = (10, ))
weights_dict = {i:weights[i] for i in range(10)}
results = list()
for y_mat in y_labeled:
weight_matrix = np.zeros(shape = y_mat.shape)
for i in range(10):
weight_matrix[y_mat == i] = weights_dict[i]
results.append(weight_matrix)