ML_kNN

조천룡·2023년 7월 29일
0

Machine Learning

목록 보기
20/26
post-thumbnail

k Nearest Neighber

  • 새로운 데이터가 있을때, 기존 데이터의 그룹 중 어떤 그룹에 속하는지를 분류하는 문제
  • k는 몇 번째 가까운 데이터까지 볼 것인가를 정하는 수치

k=5로 설정하면 5번째까지 가까운 데이터

k값에 따라 결과값이 바뀔수 있다

거리를 계산 - 유클리드 기하

단위에 따라 바뀔 수도 있다 - 표준화 필요

장단점

  • 실시간 예측을 위한 학습이 필요치 않다.
  • 결국 속도가 빨라진다.
  • 고차원 데이터에는 적합하지 않다.

Iris data

from sklearn.datasets import load_iris
iris = load_iris()

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=13, stratify=iris.target)

kNN 학습

from sklearn.neighbors import KNeighborsClassifier

knn = KNeighborsClassifier()
knn.fit(X_train, y_train)

accuracy

from sklearn.metrics import accuracy_score

pred = knn.predict(X_test)
print(accuracy_score(y_test, pred))

결과

from sklearn.metrics import classification_report, confusion_matrix

confusion_matrix(y_test, pred)

print(classification_report(y_test, pred))

profile
10√2 Data

0개의 댓글