#!/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[2]: #3 features instead of 4 features: IRIS = dt.load_iris() X1 = IRIS.data Y = IRIS.target DR = dec.PCA(n_components = 3) DR.fit(X1) X2 = DR.transform(X1) # In[3]: plt.subplot(2, 2, 1) plt.scatter(X1[:, 0], X1[:, 1], s = 7, c = Y) plt.xlabel("X1") plt.ylabel("X2") plt.subplot(2, 2, 2) plt.scatter(X2[:, 0], X2[:, 1], s = 7, c = Y) plt.xlabel("PC1") plt.ylabel("PC2") plt.subplot(2, 2, 3) plt.scatter(X1[:, 2], X1[:, 3], s = 7, c = Y) plt.xlabel("X3") plt.ylabel("X4") plt.subplot(2, 2, 4) plt.scatter(X2[:, 1], X2[:, 2], s = 7, c = Y) plt.xlabel("PC2") plt.ylabel("PC3") plt.tight_layout() # In[4]: xtr1, xte1, ytr1, yte1 = ms.train_test_split(X1, Y, train_size = 0.75, random_state = 40) xtr2, xte2, ytr2, yte2 = ms.train_test_split(X2, Y, train_size = 0.75, random_state = 40) # In[5]: 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[6]: print("Train Accuracy Change : ", trACC2 - trACC1) print("Test Accuracy Change : ", teACC2 - teACC1)