之后做了一些处理上一个声音或图像阵列,它需要进行归一化的范围内才可以写回到一个文件。这可以像这样:

# 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() 似乎并不相关。

有帮助吗?

解决方案

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的作为可以解决方案。


在放操作不改变容器阵列的D型。因为期望的归一化值是浮点数,所述audioimage阵列需要进行就地操作之前具有浮点点D型。 如果他们不是已经浮点D型的,你需要使用astype将它们转换。例如,

image = image.astype('float64')

其他提示

如果数组包含正和负数据,我去:

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
c = 255*(a - np.min(a))/np.ptp(a).astype(int)

# Normalised [-1,1]
d = 2.*(a - np.min(a))/np.ptp(a)-1

此外,值得一提的,即使它不是OP的问题,标准化

e = (a - np.mean(a)) / np.std(a)

您也可以使用重新缩放sklearn。的优点是,可以调整归一化标准偏差,除了平均定心的数据,并且可以做到这一点上任一轴,由特征,或由记录。

from sklearn.preprocessing import scale
X = scale( X, axis=0, with_mean=True, with_std=True, copy=True )

关键词参数axiswith_meanwith_std是自解释的,并且在它们的缺省状态被示出。这个论点copy执行就地如果它被设置为False操作。文档此处

您可以使用“我”(如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()

一个简单的解决方案是使用由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()

你是在试图分钟-最大规模的价值 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),这也是通常被称为正常化。

我尝试以下和得到了错误

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对象。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top