#!/usr/bin/env python # coding: utf-8 # In[1]: import numpy as np import matplotlib.pyplot as plt import sklearn.datasets as dt import sklearn.decomposition as dec import sklearn.neighbors as ne import sklearn.model_selection as ms # In[3]: #1 feature instead of 4 features: IRIS = dt.load_iris() x1 = IRIS.data y = IRIS.target DR = dec.PCA(n_components = 1) DR.fit(x1) x2 = DR.transform(x1) # In[4]: plt.subplot(2, 2, 1) plt.scatter(x1[:, 0], x1[:, 1], s = 5, c = y) plt.xlabel("X1") plt.ylabel("X2") plt.subplot(2, 2, 2) plt.scatter(x2[:, 0], x2[:, 0], s = 5) plt.xlabel("PC1") plt.subplot(2, 2, 3) plt.scatter(x1[:, 2], x1[:, 3], s = 5, c = y) plt.xlabel("X3") plt.ylabel("X4") plt.subplot(2, 2, 4) plt.scatter(x2[:, 0], x2[:, 0], s = 5) plt.xlabel("PC1") plt.tight_layout() # In[5]: xtr1, xte1, ytr1, yte1 = ms.train_test_split(x1, y, train_size = 0.7, random_state = 23) xtr2, xte2, ytr2, yte2 = ms.train_test_split(x2, y, train_size = 0.7, random_state = 23) # In[6]: KNN1 = ne.KNeighborsClassifier(n_neighbors = 5) KNN1.fit(xtr1, ytr1) trACC1 = KNN1.score(xtr1, ytr1) teACC1 = KNN1.score(xte1, yte1) KNN2 = ne.KNeighborsClassifier(n_neighbors = 5) KNN2.fit(xtr2, ytr2) trACC2 = KNN2.score(xtr2, ytr2) teACC2 = KNN2.score(xte2, yte2) # In[7]: print("Train Accuracy Change : ", trACC2 - trACC1) print("Test Accuracy Change : ", teACC2 - teACC1)