2017-12-02 1 views
0

2 차원 배열의 경우 행 크기 및 열 단위로 작동하는 표준화 함수를 만들려고합니다. 인수가 축 = 1 (행 방향)으로 주어 졌을 때 어떻게해야할지 모르겠습니다.행렬 행렬을 표준화하는 방법 (축 = 1)?

def standardize(x, axis=None): 
if axis == 0: 
    return (x - x.mean(axis))/x.std(axis) 
else: 
    ????? 

나는이 부분에서 axis = 1axis을 변경하려고 : (x - x.mean(axis))/x.std(axis)

을하지만 그때 나는 다음과 같은 오류 있어요 :

ValueError: operands could not be broadcast together with shapes (4,3) (4,) 

누군가가 난으로 무엇을 나에게 설명 할 수를 아직도 초보자 야?

+0

확인이 링크 https://stackoverflow.com : 당신이 mean()std()에 사용해야 keepdims=True,이 같은 일반적인 문제이기 때문에

는 NumPy와 개발자는 정확히 않는 매개 변수를 도입/questions/12525722/normalize-data-in-pandas에서 코드를 적절하게 적용 할 수 있는지 확인하십시오. 예를 들어 잘 설명되어 있습니다. – Abhishek

답변

0

당신이보고있는 오류의 이유는 우리가 어떻게 든 확인 mean()을 할 수 있다면 당신이 작업을 할 수있는, 그러나

x - x.mean(1) 

때문에

x.shape = (4, 3) 
x.mean(1).shape = (4,) # mean(), sum(), std() etc. remove the dimension they are applied to 

을 계산할 수 없다는 것입니다 치수를 유지에 적용하면

(조회 NumPy Broadcasting rules).

def standardize(x, axis=None): 
    return (x - x.mean(axis, keepdims=True))/x.std(axis, keepdims=True) 
관련 문제