K최근접이웃법 (KNN, K-Nearest Neighbor)

Jane의 study note.·2022년 11월 30일
0

사이킷런 Sklearn

목록 보기
3/19

0. 핵심개념 및 사이킷런 알고리즘 API 링크

sklearn.neighbors.KNeighborsClassifier
sklearn.neighbors.KNeighborsRegressor

Part1. 분류(Classification)

1. 분석 데이터 준비

import warnings
warnings.filterwarnings("ignore")
import pandas as pd
data1=pd.read_csv('breast-cancer-wisconsin.csv', encoding='utf-8')
X=data1[data1.columns[1:10]]
y=data1[["Class"]]

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test=train_test_split(X, y, stratify=y, random_state=42)

from sklearn.preprocessing import MinMaxScaler
scaler=MinMaxScaler()
scaler.fit(X_train)
X_scaled_train=scaler.transform(X_train)
X_scaled_test=scaler.transform(X_test)

2. 기본모델 적용

from sklearn.neighbors import KNeighborsClassifier
model=KNeighborsClassifier()
model.fit(X_scaled_train, y_train)
pred_train=model.predict(X_scaled_train)
model.score(X_scaled_train, y_train)

0.984375

from sklearn.metrics import confusion_matrix
confusion_train=confusion_matrix(y_train, pred_train)
print("훈련데이터 오차행렬:\n", confusion_train)

훈련데이터 오차행렬:
 [[331   2]
 [  6 173]]
 
from sklearn.metrics import classification_report
cfreport_train=classification_report(y_train, pred_train)
print("분류예측 레포트:\n", cfreport_train)

분류예측 레포트:
               precision    recall  f1-score   support

           0       0.98      0.99      0.99       333
           1       0.99      0.97      0.98       179

    accuracy                           0.98       512
   macro avg       0.99      0.98      0.98       512
weighted avg       0.98      0.98      0.98       512

pred_test=model.predict(X_scaled_test)
model.score(X_scaled_test, y_test)

0.9532163742690059

confusion_test=confusion_matrix(y_test, pred_test)
print("테스트데이터 오차행렬:\n", confusion_test)

테스트데이터 오차행렬:
 [[106   5]
 [  3  57]]
 
from sklearn.metrics import classification_report
cfreport_test=classification_report(y_test, pred_test)
print("분류예측 레포트:\n", cfreport_test)

분류예측 레포트:
               precision    recall  f1-score   support

           0       0.97      0.95      0.96       111
           1       0.92      0.95      0.93        60

    accuracy                           0.95       171
   macro avg       0.95      0.95      0.95       171
weighted avg       0.95      0.95      0.95       171
param_grid={'n_neighbors': [1, 3, 5, 7, 9, 11]}
from sklearn.model_selection import GridSearchCV
grid_search=GridSearchCV(KNeighborsClassifier(), param_grid, cv=5)
grid_search.fit(X_scaled_train, y_train)

GridSearchCV(cv=5, estimator=KNeighborsClassifier(),
             param_grid={'n_neighbors': [1, 3, 5, 7, 9, 11]})
             
print("Best Parameter: {}".format(grid_search.best_params_))
print("Best Score: {:.4f}".format(grid_search.best_score_))
print("TestSet Score: {:.4f}".format(grid_search.score(X_scaled_test, y_test)))

Best Parameter: {'n_neighbors': 3}
Best Score: 0.9824
TestSet Score: 0.9532
from scipy.stats import randint
param_distribs = {'n_neighbors': randint(low=1, high=20)}
from sklearn.model_selection import RandomizedSearchCV
random_search=RandomizedSearchCV(KNeighborsClassifier(), 
                                 param_distributions=param_distribs, n_iter=20, cv=5)
random_search.fit(X_scaled_train, y_train)

RandomizedSearchCV(cv=5, estimator=KNeighborsClassifier(), n_iter=50,
                   param_distributions={'n_neighbors': <scipy.stats._distn_infrastructure.rv_frozen object at 0x00000177EC257250>})
                   
print("Best Parameter: {}".format(random_search.best_params_))
print("Best Score: {:.4f}".format(random_search.best_score_))
print("TestSet Score: {:.4f}".format(random_search.score(X_scaled_test, y_test)))                   
Best Parameter: {'n_neighbors': 3}
Best Score: 0.9824
TestSet Score: 0.9532

Part2. 회귀(Regression)

1. 분석 데이터 준비

data2=pd.read_csv('house_price.csv', encoding='utf-8')
X=data2[data2.columns[1:5]]
y=data2[["house_value"]]

data2.head()

housing_age	income	bedrooms	households	rooms	house_value
0	23	6.7770	0.141112	2.442244	8.103960	500000
1	49	6.0199	0.160984	2.726688	5.752412	500000
2	35	5.1155	0.249061	1.902676	3.888078	500000
3	32	4.7109	0.231383	1.913669	4.508393	500000
4	21	4.5625	0.255583	3.092664	4.667954	500000

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test=train_test_split(X, y, random_state=42)

from sklearn.preprocessing import MinMaxScaler
scaler=MinMaxScaler()
scaler.fit(X_train)
X_scaled_train=scaler.transform(X_train)
X_scaled_test=scaler.transform(X_test)

2. 기본모델 적용

from sklearn.neighbors import KNeighborsRegressor
model=KNeighborsRegressor()
model.fit(X_scaled_train, y_train)
pred_train=model.predict(X_scaled_train)
model.score(X_scaled_train, y_train)

0.6804607237174459

pred_test=model.predict(X_scaled_test)
model.score(X_scaled_test, y_test)

0.5541889571372401

# RMSE (Root Mean Squared Error)
import numpy as np
from sklearn.metrics import mean_squared_error 
MSE_train = mean_squared_error(y_train, pred_train)
MSE_test = mean_squared_error(y_test, pred_test)
print("훈련   데이터 RMSE:", np.sqrt(MSE_train))
print("테스트 데이터 RMSE:", np.sqrt(MSE_test))

훈련   데이터 RMSE: 53952.69804097723
테스트 데이터 RMSE: 63831.91662964773

3. Grid Search

param_grid={'n_neighbors': [1, 3, 5, 7, 9, 11]}
from sklearn.model_selection import GridSearchCV
grid_search=GridSearchCV(KNeighborsRegressor(), param_grid, cv=5)
grid_search.fit(X_scaled_train, y_train)

GridSearchCV(cv=5, estimator=KNeighborsRegressor(),
             param_grid={'n_neighbors': [1, 3, 5, 7, 9, 11]})
             
print("Best Parameter: {}".format(grid_search.best_params_))
print("Best Score: {:.4f}".format(grid_search.best_score_))
print("TestSet Score: {:.4f}".format(grid_search.score(X_scaled_test, y_test)))  

Best Parameter: {'n_neighbors': 11}
Best Score: 0.5638
TestSet Score: 0.5880

4. Random Search

param_distribs = {'n_neighbors': randint(low=1, high=20)}
from sklearn.model_selection import RandomizedSearchCV
random_search=RandomizedSearchCV(KNeighborsRegressor(), 
                                 param_distributions=param_distribs, n_iter=50, cv=5)
random_search.fit(X_scaled_train, y_train)

RandomizedSearchCV(cv=5, estimator=KNeighborsRegressor(), n_iter=50,
                   param_distributions={'n_neighbors': <scipy.stats._distn_infrastructure.rv_frozen object at 0x00000177EC2A3700>})

print("Best Parameter: {}".format(random_search.best_params_))
print("Best Score: {:.4f}".format(random_search.best_score_))
print("TestSet Score: {:.4f}".format(random_search.score(X_scaled_test, y_test)))

Best Parameter: {'n_neighbors': 19}
Best Score: 0.5777
TestSet Score: 0.6004

0개의 댓글