如何将 NumPy 数组标准化到一定范围内?
对音频或图像数组进行一些处理后,需要在一定范围内对其进行标准化,然后才能写回文件。这可以像这样完成:
# Normalize audio channels to between -1.0 and +1.0
audio[:,0] = audio[:,0]/abs(audio[:,0]).max()
audio[:,1] = audio[:,1]/abs(audio[:,1]).max()
# Normalize image to between 0 and 255
image = image/(image.max()/255.0)
是否有一种不太冗长、方便的函数方法来做到这一点? matplotlib.colors.Normalize()
似乎不相关。
After doing some processing on an audio or image array, it needs to be normalized within a range before it can be written back to a file. This can be done like so:
# Normalize audio channels to between -1.0 and +1.0
audio[:,0] = audio[:,0]/abs(audio[:,0]).max()
audio[:,1] = audio[:,1]/abs(audio[:,1]).max()
# Normalize image to between 0 and 255
image = image/(image.max()/255.0)
Is there a less verbose, convenience function way to do this? matplotlib.colors.Normalize()
doesn't seem to be related.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用
/=
和*=
可以消除中间临时数组,从而节省一些内存。乘法比除法便宜,因此比除法稍微快一些,
因为我们在这里使用基本的 numpy 方法,我认为这在 numpy 中是尽可能有效的解决方案。
就地操作不会更改容器数组的数据类型。由于所需的标准化值是浮点数,因此在执行就地操作之前,
audio
和image
数组需要具有浮点点数据类型。如果它们还不是浮点数据类型,则需要使用
astype
对其进行转换。例如,Using
/=
and*=
allows you to eliminate an intermediate temporary array, thus saving some memory. Multiplication is less expensive than division, sois marginally faster than
Since we are using basic numpy methods here, I think this is about as efficient a solution in numpy as can be.
In-place operations do not change the dtype of the container array. Since the desired normalized values are floats, the
audio
andimage
arrays need to have floating-point point dtype before the in-place operations are performed.If they are not already of floating-point dtype, you'll need to convert them using
astype
. For example,如果数组同时包含正数和负数数据,我会选择:
如果数组包含 nan,一种解决方案可能是将它们删除为:
但是,根据您可能想要处理的上下文
nan
有所不同。例如,插入值,将其替换为 0,或者引发错误。最后,值得一提的是,即使这不是OP的问题,标准化:
If the array contains both positive and negative data, I'd go with:
If the array contains
nan
, one solution could be to just remove them as:However, depending on the context you might want to treat
nan
differently. E.g. interpolate the value, replacing in with e.g. 0, or raise an error.Finally, worth mentioning even if it's not OP's question, standardization:
您还可以使用
sklearn.preprocessing.scale< 重新缩放/代码>
。优点是,除了以数据均值为中心之外,您还可以调整标准化标准差,并且可以在任一轴上、按要素或按记录执行此操作。
关键字参数
axis
、with_mean
、with_std
是不言自明的,并以其默认状态显示。如果参数copy
设置为False
,则就地执行操作。You can also rescale using
sklearn.preprocessing.scale
. The advantages are that you can adjust normalize the standard deviation, in addition to mean-centering the data, and that you can do this on either axis, by features, or by records.The keyword arguments
axis
,with_mean
,with_std
are self explanatory, and are shown in their default state. The argumentcopy
performs the operation in-place if it is set toFalse
.您正在尝试在 -1 和 +1 之间缩放
audio
的值,在 0 到 255 之间缩放image
的值。使用
sklearn.preprocessing.minmax_scale
,应该很容易解决你的问题。例如:
和
注意:不要与缩放的操作混淆将向量的范数(长度)调整为某个值(通常为 1),这通常也称为归一化。
You are trying to min-max scale the values of
audio
between -1 and +1 andimage
between 0 and 255.Using
sklearn.preprocessing.minmax_scale
, should easily solve your problem.e.g.:
and
note: Not to be confused with the operation that scales the norm (length) of a vector to a certain value (usually 1), which is also commonly referred to as normalization.
这个答案对类似的问题为我解决了问题
This answer to a similar question solved the problem for me with
您可以使用“i”(如 idiv、imul..)版本,它看起来还不错:
对于另一种情况,您可以编写一个函数来按列标准化 n 维数组:
You can use the "i" (as in idiv, imul..) version, and it doesn't look half bad:
For the other case you can write a function to normalize an n-dimensional array by colums:
一个简单的解决方案是使用 sklearn.preprocessing 库提供的缩放器。
错误 X_rec-X 将为零。您可以根据需要调整 feature_range,甚至使用标准缩放器 sk.StandardScaler()
A simple solution is using the scalers offered by the sklearn.preprocessing library.
The error X_rec-X will be zero. You can adjust the feature_range for your needs, or even use a standart scaler sk.StandardScaler()
我尝试按照 this 进行操作,并收到错误
The
numpy
array I was试图标准化是一个整数
数组。看来他们不赞成在版本 > 中进行类型转换1.10
,你必须使用numpy.true_divide()
来解决这个问题。img
是一个PIL.Image
对象。I tried following this, and got the error
The
numpy
array I was trying to normalize was aninteger
array. It seems they deprecated type casting in versions >1.10
, and you have to usenumpy.true_divide()
to resolve that.img
was anPIL.Image
object.