import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
데이터 크기의 편차가 심해 스케일링과 널데이터는 없지만 학습에 필요없는 데이터를 처리할 것이다.
purchased -> 1 구매, 0 구매안함
X 는 age와 연봉으로
y 는 구매여부로 놔둠
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X = sc.fit_transform(X)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size = 0.2,random_state=0)
from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors= 5,metric='minkowski')
n_neighbors= 5 (5개의 이웃을 확인 한다.)
metric='minkowski' (유클리드 거리 확인)
거리측정 방식 2가지 - 유클리드 - 맨허튼
classifier.fit(X_train, y_train)
y_pred=classifier.predict(X_test)
y_test = y_test.values
from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
>>> array([[55, 3],
[ 1, 21]])
accuracy_score(y_test, y_pred)
>>> 0.95
from sklearn.metrics import classification_report
# 트레이닝 결과 시각화
from matplotlib.colors import ListedColormap
X_set, y_set = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.figure(figsize=[10,7])
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
alpha = 0.6, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
c = ListedColormap(('orange', 'green'))(i), label = j)
plt.title('Logistic Regression (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()
# 테스트 결과 시각화
from matplotlib.colors import ListedColormap
X_set, y_set = X_test, y_test
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1,
stop = X_set[:, 0].max() + 1, step = 0.01),
np.arange(start = X_set[:, 1].min() - 1,
stop = X_set[:, 1].max() + 1, step = 0.01))
plt.figure(figsize=[10,7])
plt.contourf(X1, X2, classifier.predict(
np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
alpha = 0.6, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
c = ListedColormap(('orange', 'green'))(i), label = j)
plt.title('Classifier (Test set)')
plt.legend()
plt.show()
[머신러닝]SVM(Support Vector Machine) (0) | 2021.06.07 |
---|---|
[머신러닝]당뇨병 분류하는 모델 (0) | 2021.05.20 |
[머신러닝]KNN(K-Nearest Neighbor) (0) | 2021.05.19 |
[머신러닝]Logistic Regression 구매여부 확인 (0) | 2021.05.17 |
[머신러닝]Logistic Regression (0) | 2021.05.17 |