시작하기무료로 시작하기

Restricted Levenshtein

stringdist 패키지에는 Levenshtein 거리를 변형한 Restricted Damerau-Levenshtein 거리가 구현되어 있음을 확인했고, 이를 시도해 보려고 해요. 강의에서의 논리를 따라 이를 사용자 정의 함수로 감싸고, Local Outlier Factor 이상치 탐지기를 학습하기 전에 거리 행렬을 미리 계산해 보세요. 성능 평가는 accuracy_score()로 측정하며, 이는 accuracy()라는 이름으로 제공돼요. 또한 stringdist, numpy(별칭 np), scipy.spatial.distancepdist()squareform(), 그리고 LocalOutlierFactorlof로 사용할 수 있어요. 데이터는 labelsequence 두 열을 가진 pandas 데이터프레임으로 미리 로드되어 있으며, 클래스는 IMMUNE SYSTEMVIRUS 두 가지예요.

이 연습은 강의의 일부입니다

Python으로 설계하는 Machine Learning 워크플로

강의 보기

연습 안내

  • 입력이 각각 문자열을 하나 담은 배열인 uv인 함수를 작성하고, 두 문자열에 rdlevenshtein() 함수를 적용하세요.
  • proteinssequence 열을 먼저 numpy 배열로 변환한 다음 .reshape()을 사용해 재구성하세요.
  • my_rdlevenshtein()을 사용해 sequences의 정방 거리 행렬을 계산하고, 이를 이용해 lof를 학습하세요.
  • 단백질이 바이러스인지 여부를 나타내는 불리언으로 predsproteins['label']을 변환한 뒤 정확도를 계산하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Wrap the RD-Levenshtein metric in a custom function
def my_rdlevenshtein(u, v):
    return ____.rdlevenshtein(____, ____)

# Reshape the array into a numpy matrix
sequences = ____(proteins['seq']).____(-1, 1)

# Compute the pairwise distance matrix in square form
M = ____

# Run a LoF algorithm on the precomputed distance matrix
preds = lof(metric=____).____(M)

# Compute the accuracy of the outlier predictions
print(accuracy(____, ____))
코드 편집 및 실행