이상치에 강한 특성 스케일링

Python으로 배우는 이상치 탐지

Bekhruz (Bex) Tuychiev

Kaggle Master, Data Science Content Creator

유클리드 거리

A = np.array([9, 1, 6])
B = np.array([25, 44, 85])


diffs = (B - A) ** 2
dist_AB = np.sqrt(np.sum(diffs)) print(dist_AB)
91.35644476444998

유클리드 거리를 계산하는 공식과 이를 설명하는 시각 자료.

Python으로 배우는 이상치 탐지

SciPy의 유클리드

from scipy.spatial.distance import \
euclidean

dist_AB = euclidean(A, B)
dist_AB
91.35644476444998

유클리드 거리를 계산하는 공식과 이를 설명하는 시각 자료.

Python으로 배우는 이상치 탐지

표준화

  • 평균을 빼고 표준편차로 나눕니다
  • 결과: 평균 0, 표준편차 1
Python으로 배우는 이상치 탐지

StandardScaler

from sklearn.preprocessing import StandardScaler

ss = StandardScaler()

# 특성과 타깃 추출 X = males.drop("weightkg", axis=1) y = males[['weightkg']]
# 적합 ss.fit(X)
Python으로 배우는 이상치 탐지

변환하기

X_transformed = ss.transform(X)

X_transformed[:5]
array([[-1.05174523],
       [-0.29289108],
       [ 1.3446363 ],
       [-1.21654894],
       [0.056451235]])
Python으로 배우는 이상치 탐지

fit_transform

ss = StandardScaler()

X_transformed = ss.fit_transform(X)
Python으로 배우는 이상치 탐지

QuantileTransformer

from sklearn.preprocessing import QuantileTransformer


# 초기화 qt = QuantileTransformer() X = males.drop("weightkg", axis=1) y = males[['weightkg']]
X_transformed = qt.fit_transform(X) X_transformed.shape
(4082, 94)
Python으로 배우는 이상치 탐지

열 이름 유지

qt = QuantileTransformer()

X.loc[:, :] = qt.fit_transform(X)

X.head()

변환된 Ansur 남성 신체 치수 데이터셋의 상위 5개 행.

Python으로 배우는 이상치 탐지

균등 히스토그램

plt.hist(X['footlength'], color='red')

plt.xlabel("Foot length")
plt.title("Histogram of foot lengths")

plt.show()

균등 분포를 보이는 발길이 열의 히스토그램

Python으로 배우는 이상치 탐지

정규 히스토그램

qt = QuantileTransformer(
  output_distribution='normal')

# 덮어쓴 특성 배열 재생성
X = males.drop("weightkg", axis=1)
X.loc[:, :] = qt.fit_transform(X)

plt.hist(X['footlength'], color='r')
plt.xlabel("Foot length")
plt.title("Histogram of foot lengths")

plt.show()

정규 분포에 가까운 발길이 열의 히스토그램

Python으로 배우는 이상치 탐지

Ayo berlatih!

Python으로 배우는 이상치 탐지

Preparing Video For Download...