问题:如何将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.
回答 0
audio /= np.max(np.abs(audio),axis=0)
image *= (255.0/image.max())
使用/=
和*=
可以消除中间的临时阵列,从而节省了一些内存。乘法比除法便宜,所以
image *= 255.0/image.max() # Uses 1 division and image.size multiplications
比…快一点
image /= image.max()/255.0 # Uses 1+image.size divisions
由于我们在这里使用基本的numpy方法,因此我认为这是尽可能有效的numpy解决方案。
就地操作不会更改容器数组的dtype。由于所需的标准化值是浮点型,因此在执行就地操作之前,audio
and image
数组需要具有浮点dtype。如果它们还不是浮点dtype,则需要使用进行转换astype
。例如,
image = image.astype('float64')
audio /= np.max(np.abs(audio),axis=0)
image *= (255.0/image.max())
Using /=
and *=
allows you to eliminate an intermediate temporary array, thus saving some memory. Multiplication is less expensive than division, so
image *= 255.0/image.max() # Uses 1 division and image.size multiplications
is marginally faster than
image /= image.max()/255.0 # Uses 1+image.size divisions
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
and image
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,
image = image.astype('float64')
回答 1
如果数组同时包含正数和负数,我将使用:
import numpy as np
a = np.random.rand(3,2)
# Normalised [0,1]
b = (a - np.min(a))/np.ptp(a)
# Normalised [0,255] as integer: don't forget the parenthesis before astype(int)
c = (255*(a - np.min(a))/np.ptp(a)).astype(int)
# Normalised [-1,1]
d = 2.*(a - np.min(a))/np.ptp(a)-1
如果数组包含nan
,则一种解决方案是将其删除为:
def nan_ptp(a):
return np.ptp(a[np.isfinite(a)])
b = (a - np.nanmin(a))/nan_ptp(a)
但是,根据上下文,您可能需要nan
不同的对待。例如,插值,用例如0代替,或引发错误。
最后,值得一提的是,即使不是OP的问题,也要标准化:
e = (a - np.mean(a)) / np.std(a)
If the array contains both positive and negative data, I’d go with:
import numpy as np
a = np.random.rand(3,2)
# Normalised [0,1]
b = (a - np.min(a))/np.ptp(a)
# Normalised [0,255] as integer: don't forget the parenthesis before astype(int)
c = (255*(a - np.min(a))/np.ptp(a)).astype(int)
# Normalised [-1,1]
d = 2.*(a - np.min(a))/np.ptp(a)-1
If the array contains nan
, one solution could be to just remove them as:
def nan_ptp(a):
return np.ptp(a[np.isfinite(a)])
b = (a - np.nanmin(a))/nan_ptp(a)
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:
e = (a - np.mean(a)) / np.std(a)
回答 2
您也可以使用重新缩放sklearn
。优势在于,除了对数据进行均值居中之外,还可以调整标准差的归一化,并且可以在任一轴上,通过要素或按记录进行校准。
from sklearn.preprocessing import scale
X = scale( X, axis=0, with_mean=True, with_std=True, copy=True )
关键词参数axis
,with_mean
,with_std
是自我解释,并且在默认状态显示。如果该参数copy
设置为,则执行就地操作False
。这里的文件。
You can also rescale using sklearn
. 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.
from sklearn.preprocessing import scale
X = scale( X, axis=0, with_mean=True, with_std=True, copy=True )
The keyword arguments axis
, with_mean
, with_std
are self explanatory, and are shown in their default state. The argument copy
performs the operation in-place if it is set to False
. Documentation here.
回答 3
您可以使用“ i”版本(如idiv中的imul ..),它看起来还不错:
image /= (image.max()/255.0)
在另一种情况下,您可以编写一个函数来通过colums标准化n维数组:
def normalize_columns(arr):
rows, cols = arr.shape
for col in xrange(cols):
arr[:,col] /= abs(arr[:,col]).max()
You can use the “i” (as in idiv, imul..) version, and it doesn’t look half bad:
image /= (image.max()/255.0)
For the other case you can write a function to normalize an n-dimensional array by colums:
def normalize_columns(arr):
rows, cols = arr.shape
for col in xrange(cols):
arr[:,col] /= abs(arr[:,col]).max()
回答 4
您正在尝试最小-最大比例缩放audio
介于-1和+1 image
之间以及0和255之间的值。
使用sklearn.preprocessing.minmax_scale
,应该可以轻松解决您的问题。
例如:
audio_scaled = minmax_scale(audio, feature_range=(-1,1))
和
shape = image.shape
image_scaled = minmax_scale(image.ravel(), feature_range=(0,255)).reshape(shape)
注意:不要与将向量的范数(长度)缩放到某个值(通常为1)的操作相混淆,该操作通常也称为归一化。
You are trying to min-max scale the values of audio
between -1 and +1 and image
between 0 and 255.
Using sklearn.preprocessing.minmax_scale
, should easily solve your problem.
e.g.:
audio_scaled = minmax_scale(audio, feature_range=(-1,1))
and
shape = image.shape
image_scaled = minmax_scale(image.ravel(), feature_range=(0,255)).reshape(shape)
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.
回答 5
一个简单的解决方案是使用sklearn.preprocessing库提供的缩放器。
scaler = sk.MinMaxScaler(feature_range=(0, 250))
scaler = scaler.fit(X)
X_scaled = scaler.transform(X)
# Checking reconstruction
X_rec = scaler.inverse_transform(X_scaled)
错误X_rec-X将为零。您可以根据需要调整feature_range,甚至可以使用标准缩放器sk.StandardScaler()
A simple solution is using the scalers offered by the sklearn.preprocessing library.
scaler = sk.MinMaxScaler(feature_range=(0, 250))
scaler = scaler.fit(X)
X_scaled = scaler.transform(X)
# Checking reconstruction
X_rec = scaler.inverse_transform(X_scaled)
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()
回答 6
我尝试按照此操作,但出现了错误
TypeError: ufunc 'true_divide' output (typecode 'd') could not be coerced to provided output parameter (typecode 'l') according to the casting rule ''same_kind''
在numpy
我试图正常化阵列是一个integer
数组。似乎他们不赞成在版本>中进行类型转换1.10
,而您必须使用它numpy.true_divide()
来解决该问题。
arr = np.array(img)
arr = np.true_divide(arr,[255.0],out=None)
img
是一个PIL.Image
对象。
I tried following this, and got the error
TypeError: ufunc 'true_divide' output (typecode 'd') could not be coerced to provided output parameter (typecode 'l') according to the casting rule ''same_kind''
The numpy
array I was trying to normalize was an integer
array. It seems they deprecated type casting in versions > 1.10
, and you have to use numpy.true_divide()
to resolve that.
arr = np.array(img)
arr = np.true_divide(arr,[255.0],out=None)
img
was an PIL.Image
object.
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。