-
Python 데이터분석 기초 70 - K-NN (K-Nearest Neighber)Python 데이터 분석 2022. 11. 25. 10:23
breast_cancer dataset
# KNN from sklearn.datasets import load_breast_cancer from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier import matplotlib.pyplot as plt cancer = load_breast_cancer() x_train, x_test, y_train, y_test = train_test_split(cancer.data, cancer.target, stratify = cancer.target, random_state = 66) print(x_train.shape, x_test.shape, y_train.shape, y_test.shape) # (426, 30) (143, 30) (426,) (143,) train_acc = [] test_acc = [] neighbors_setting = range(1, 10) for n_neigh in neighbors_setting: clf = KNeighborsClassifier(n_neighbors=n_neigh) clf.fit(x_train, y_train) train_acc.append(clf.score(x_train, y_train)) test_acc.append(clf.score(x_test, y_test)) import numpy as np print('train 분류 평균 정확도 :', np.mean(train_acc)) print('test 분류 평균 정확도 :', np.mean(test_acc)) plt.plot(neighbors_setting, train_acc, label = 'train acc') plt.plot(neighbors_setting, test_acc, label = 'test acc') plt.ylabel('accuracy') plt.xlabel('k') plt.legend() plt.show() <console> (426, 30) (143, 30) (426,) (143,) train 분류 평균 정확도 : 0.9559207094418363 test 분류 평균 정확도 : 0.9191919191919192
train과 test의 분류 평균 정확도의 차이가 크지 않으면 좋다.
train, test 정확도 시각화 'Python 데이터 분석' 카테고리의 다른 글
Python 데이터분석 기초 72 - MLP(multi-layer perceptron) - 다층 신경망 (0) 2022.11.25 Python 데이터분석 기초 71 - Perceptron(퍼셉트론, 단층신경망) (0) 2022.11.25 Python 데이터분석 기초 69 - K-NN (K-Nearest Neighber) (0) 2022.11.25 NaiveBayes 분류모델 - GaussanNB 예제 (0) 2022.11.24 날씨 정보로 나이브에즈 분류기 작성 - 비 예보 (0) 2022.11.24