Python:随机播放并放回numpy数组的初始顺序元素

时间:2019-05-08 15:53:07

标签: arrays python-3.x numpy sorting

我有一个数组,必须在上面修改一些值。为此,我必须更改数组元素的顺序,更改其值后,我想将这些值放回其初始顺序。但是两个小时后,我真的不知道该怎么做。

更改顺序时,我需要将它们按绝对值从最大到最小的元素排序。然后,我需要使元素的总和= = 1,以便我修改元素,但随后无法对数组重新排序。

这是我的代码:

.opener-wrapper {
  display: flex;
  flex-wrap: wrap;
}

.opener {
  width: 33%;
  height: 100%;
  min-width: 33%;
  min-height: 100%;
} 

#opener1::before {
  background-image: url('https://source.unsplash.com/random');
  background-size: cover;
  background-position: center;
  content: "";
  display: block;
  position: absolute;
  width: 33%;
  height: 12em;
  z-index: -2;
  opacity: 0.7;
}

#opener1::after {
  background-color: #314F59;
  content: "";
  display: block;
  position: absolute;
  width: auto;
  height: 100%;
  z-index: -1;
  opacity: .35;
}

#opener2::before {
  background-image: url('https://source.unsplash.com/random');
  background-size: cover;
  background-position: center;
  content: "";
  display: block;
  position: absolute;
  width: 33%;
  height: 12em;
  z-index: -2;
  opacity: 0.7;
}

#opener2::after {
  background-color: #314F59;
  content: "";
  display: block;
  position: absolute;
  width: auto;
  height: 100%;
  z-index: -1;
  opacity: .35;
}

#opener3::before {
  background-image: url('https://source.unsplash.com/random');
  background-size: cover;
  background-position: center;
  content: "";
  display: block;
  position: absolute;
  width: 33%;
  height: 12em;
  z-index: -2;
  opacity: 0.7;
}

#opener3::after {
  background-color: #314F59;
  content: "";
  display: block;
  position: absolute;
  width: auto;
  height: 100%;
  z-index: -1;
  opacity: .35;
}

.flex-center {
  /* display: flex;
  flex-direction: column;
  justify-content: center;
  align-content: center;  */
}

.opener-msg {
  color: #fff;
  text-shadow: #343a40 2px 2px;
  min-width: 100%;
  min-height: 12em;
  position: relative;
  margin: 3% 0;
  text-transform: uppercase;
}

.opener-msg::before {
  content: "";
  display: block;
  position: absolute;
  margin-left: 0;
  min-width: 100%;
  min-height: 12em;
  z-index: -1;
  opacity: 0.4;
  background-color: #343a40;
}

.opener-title,
.opener-subtitle {
  width: 100%;
  display: block;
  text-align: center;
}

.opener-title {
  margin: 3% 0;
}

@media only screen and (max-width: 600px) {
    .opener {
        width: 100%;
        height: 100%;
        min-width: 100%;
        min-height: 100%;
    }

    #opener1::before {
        width: 100%;
        min-width: 100%;
    }

    #opener2::before {
        width: 100%;
        min-width: 100%;
    }

    #opener3::before {
        width: 100%;
        min-width: 100%;
    }

}

我知道

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

这是技巧,但修改输出的值不起作用。 因此,任何帮助或提示将不胜感激。非常感谢

1 个答案:

答案 0 :(得分:0)

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

解决方案是:

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_)