Python缩放数组的一部分而不是res

2024-04-25 17:52:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图规范化数据中特定范围的数字,同时保持其余的值不变。这也是一个非常大的数据集

这是我的尝试

import numpy as np

# An unrelated scaling measure, the intial array shape I want to keep.
Intscale1 = np.array([data/1700 for data in Int1])

# Normalizing the first 500 values in the array
Intscaled11 = 2*Intscale1[:500]

# Rest of the data set is left unchanged 
Intscaled12 = Intscale1[501:]

# Combing both to the same shape as Intscale1
Intscale111 = Intscaled11 += Intscaled12

不幸的是,这不起作用。它回来了

operands could not be broadcast together with shapes (500,) (1548,) (500,) 最后一部分

他们是我想要实现的一种方式吗


Tags: theto数据inimportdataasnp
2条回答

你可以用地图。我看到的问题是使用2*数组的位置。这个操作实际上就像做array+array,在python中,它只会在数组后面附加数组,因此通过一个映射可以修复它

# An unrelated scaling measure, the intial array shape I want to keep.
Intscale1 = np.array([data/1700 for data in Int1])

# Normalizing the first 500 values in the array
Intscaled11 = map(lambda x: x*2, Intscale1[:500])

# Rest of the data set is left unchanged 
Intscaled12 = Intscale1[501:]

# Combing both to the same shape as Intscale1
Intscale111 = np.concatenate([Intscaled1, Intscaled2])

问题是,您尝试使用的+作为元素级加法运算符。它尝试从数组中添加值,但失败了,因为它们的大小不同。函数concatenate from numpy允许您将两个numpy数组连接到另一个更大的数组中。如果必须重新排列数据,则可以始终使用列表理解,然后将结果列表转换为numpy数组,以便更快、更轻松地进行计算。对于您的需要,连接应该工作得很好

Intscaled = np.concatenate([Intscaled11, Intscaled12)

相关问题 更多 >

    热门问题