문제

후 하부 처리에 있는 오디오 또는 이미지 배열을,그것은 필요가 있을 정규화된 범위 내에서 수 있습니다 전에 다시 쓸 수 있습니다.이 수행할 수 있습니다.

# 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 수 있습니다.


에서 작업을 변경하지 않 dtype 컨테이너의 배열입니다.이후 원하는 표준화 값은 수레에, audioimage 배열해야 부동 소수점 점 dtype 하기 전에 작업이 수행됩니다.되지 않은 경우의 부동 소수점 dtype 해야 합니다 그들에게 변환하여 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 )

키워드 인수 axis, with_mean, with_std 자기 설명이며 기본 상태에 표시됩니다. 논쟁 거리 copy 설정된 경우 작업을 수행합니다 False. 선적 서류 비치 여기.

"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()

간단한 솔루션은 Sklearn.proprocessing 라이브러리가 제공하는 스케일러를 사용하는 것입니다.

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는 0입니다. 필요에 맞는 feaction_range를 조정하거나 standart scaler 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)으로 (일반적으로 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 an PIL.Image 물체.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top