Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Python:将numpy数组中的元素洗牌并重新放入初始顺序_Arrays_Python 3.x_Numpy_Sorting - Fatal编程技术网

Arrays Python:将numpy数组中的元素洗牌并重新放入初始顺序

Arrays Python:将numpy数组中的元素洗牌并重新放入初始顺序,arrays,python-3.x,numpy,sorting,Arrays,Python 3.x,Numpy,Sorting,我有一个数组,必须在该数组上修改一些值。为了做到这一点,我必须改变数组元素的顺序,在改变了它的值之后,我想把值放回它们的初始顺序。但两个小时后,我真的不知道该怎么做 在更改顺序时,我需要按绝对值从最大的元素到最小的元素进行排序。然后,我需要让元素的总和=1,所以我修改了元素,但是不可能对数组重新排序 这是我的代码: output = np.random.uniform(-1, 1, (4, 1)).ravel() sorted_weigths = np.sort(abs(output))[::

我有一个数组,必须在该数组上修改一些值。为了做到这一点,我必须改变数组元素的顺序,在改变了它的值之后,我想把值放回它们的初始顺序。但两个小时后,我真的不知道该怎么做

在更改顺序时,我需要按绝对值从最大的元素到最小的元素进行排序。然后,我需要让元素的总和=1,所以我修改了元素,但是不可能对数组重新排序

这是我的代码:

output = np.random.uniform(-1, 1, (4, 1)).ravel()

sorted_weigths = np.sort(abs(output))[::-1]
sorted_indices = np.argsort(sorted_weigths)[::-1] 
signs = [i < 0 for i in output]  

if np.sum(abs(sorted_weigths)) > 1:
    alloc = 1
    for i in range(output.shape[0]):
        if alloc > abs(sorted_weigths[i]):
            sorted_weigths[i] = sorted_weigths[i]
            alloc = alloc - abs(sorted_weigths[i])
        elif alloc > 0:
            sorted_weigths[i] = alloc
            alloc = alloc - alloc
        else:
            sorted_weigths[i] = 0
else:
    pass

sorted_weigths[sorted_indices]

for i in range(len(signs)):
    if signs[i] == True:
        sorted_weigths[i] = -sorted_weigths[i]
    else:
        pass
这是诀窍,但修改输出值不起作用。
因此,任何帮助或暗示都将不胜感激。非常感谢

解决方案实际上是根据原始输出的位置重新排列变换数组的位置,而不是跟踪无序索引

解决办法是:

output = np.random.uniform(-1, 1, (4, 1)).ravel()

sorted_weigths = np.sort(abs(output))[::-1]
sorted_indices = np.argsort(abs(output))[::-1] 
signs = [i < 0 for i in output]  

if np.sum(abs(sorted_weigths)) > 1:
    alloc = 1
    for i in range(output.shape[0]):
        if alloc > abs(sorted_weigths[i]):
            sorted_weigths[i] = sorted_weigths[i]
            alloc = alloc - abs(sorted_weigths[i])
        elif alloc > 0:
            sorted_weigths[i] = alloc
            alloc = alloc - alloc
        else:
            sorted_weigths[i] = 0
else:
    pass

sorted_weigths_ = copy.deepcopy(sorted_weigths)
for i in range(sorted_indices.shape[0]):
    sorted_weigths_[sorted_indices[i]] = sorted_weigths[i]

for i in range(len(signs)):
    if signs[i] == True:
        sorted_weigths_[i] = -sorted_weigths_[i]
    else:
        pass

print(output)
print(sorted_weigths_)
output=np.random.uniform(-1,1,(4,1)).ravel()
sorted_weights=np.sort(abs(output))[:-1]
排序的索引=np.argsort(abs(output))[:-1]
符号=[i<0表示输出中的i]
如果np.和(abs(分类重量))大于1:
alloc=1
对于范围内的i(output.shape[0]):
如果alloc>abs(分类重量[i]):
分类重量[i]=分类重量[i]
alloc=alloc-abs(分类重量[i])
elif alloc>0:
排序的重量[i]=alloc
alloc=alloc-alloc
其他:
排序的_weights[i]=0
其他:
通过
排序的重量=复制。深度复制(排序的重量)
对于范围内的i(已排序的_索引形状[0]):
排序的重量[排序的重量指数[i]]=排序的重量[i]
对于范围内的i(len(符号)):
如果符号[i]==真:
排序的权重[i]=-排序的权重[i]
其他:
通过
打印(输出)
打印(已排序的重量)

解决方案实际上是根据原始输出的位置重新排列变换数组的位置,而不是跟踪无序索引

解决办法是:

output = np.random.uniform(-1, 1, (4, 1)).ravel()

sorted_weigths = np.sort(abs(output))[::-1]
sorted_indices = np.argsort(abs(output))[::-1] 
signs = [i < 0 for i in output]  

if np.sum(abs(sorted_weigths)) > 1:
    alloc = 1
    for i in range(output.shape[0]):
        if alloc > abs(sorted_weigths[i]):
            sorted_weigths[i] = sorted_weigths[i]
            alloc = alloc - abs(sorted_weigths[i])
        elif alloc > 0:
            sorted_weigths[i] = alloc
            alloc = alloc - alloc
        else:
            sorted_weigths[i] = 0
else:
    pass

sorted_weigths_ = copy.deepcopy(sorted_weigths)
for i in range(sorted_indices.shape[0]):
    sorted_weigths_[sorted_indices[i]] = sorted_weigths[i]

for i in range(len(signs)):
    if signs[i] == True:
        sorted_weigths_[i] = -sorted_weigths_[i]
    else:
        pass

print(output)
print(sorted_weigths_)
output=np.random.uniform(-1,1,(4,1)).ravel()
sorted_weights=np.sort(abs(output))[:-1]
排序的索引=np.argsort(abs(output))[:-1]
符号=[i<0表示输出中的i]
如果np.和(abs(分类重量))大于1:
alloc=1
对于范围内的i(output.shape[0]):
如果alloc>abs(分类重量[i]):
分类重量[i]=分类重量[i]
alloc=alloc-abs(分类重量[i])
elif alloc>0:
排序的重量[i]=alloc
alloc=alloc-alloc
其他:
排序的_weights[i]=0
其他:
通过
排序的重量=复制。深度复制(排序的重量)
对于范围内的i(已排序的_索引形状[0]):
排序的重量[排序的重量指数[i]]=排序的重量[i]
对于范围内的i(len(符号)):
如果符号[i]==真:
排序的权重[i]=-排序的权重[i]
其他:
通过
打印(输出)
打印(已排序的重量)