import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# load dataset into Pandas DataFrame
df = pd.read_csv("D:\Python_programs\ML\Iris Data\KPCA\iris.csv")
#df.to_csv('iris.csv')


from sklearn.preprocessing import StandardScaler
features = ['sepal length', 'sepal width', 'petal length', 'petal width']
# Separating out the features
x = df.loc[:, features].values
# Separating out the target
y = df.loc[:,['target']].values
# Standardizing the features
x = StandardScaler().fit_transform(x)

from sklearn.decomposition import KernelPCA


## Finding the principle components
#   KERNELS : linear,rbf,poly
#

def Kernel_Pca(ker):
    kpca = KernelPCA(n_components=4, kernel=ker, gamma=15)
    x_kpca = kpca.fit_transform(x)
    kpca_transform = kpca.fit_transform(x)
    explained_variance = np.var(kpca_transform, axis=0)
    ev = explained_variance / np.sum(explained_variance)

    #--------- Bar Graph for Explained Variance Ratio ------------
    plt.bar([1,2,3,4],list(ev*100),label='Principal Components',color='b')
    plt.legend()
    plt.xlabel('Principal Components ')
    #----------------------
    n=list(ev*100)
    pc=[]
    for i in range(len(n)):
            n[i]=round(n[i],4)
            pc.append('PC-'+str(i+1)+'('+str(n[i])+')')

    #----------------------
    plt.xticks([1,2,3,4],pc, fontsize=7, rotation=30)
    plt.ylabel('Variance Ratio')
    plt.title('Variance Ratio of IRIS Dataset using kernel:'+str(ker))
    plt.show()
    #---------------------------------------------------
    # *Since the initial 2 principal components have high variance.
    #   so, we select pc-1 and pc-2.
    #---------------------------------------------------
    kpca = KernelPCA(n_components=2, kernel=ker, gamma=15)
    x_kpca = kpca.fit_transform(x)
    principalComponents = kpca.fit_transform(x)

    principalDf = pd.DataFrame(data = principalComponents
                 , columns = ['PC-1', 'PC-2'])
    # Adding lables
    finalDf = pd.concat([principalDf, df[['target']]], axis = 1)
    # Plotting pc1 & pc2
    fig = plt.figure(figsize = (8,8))
    ax = fig.add_subplot(1,1,1) 
    ax.set_xlabel('PC-1', fontsize = 15)
    ax.set_ylabel('PC-2', fontsize = 15)
    ax.set_title('KPCA on IRIS Dataset using kernel:'+str(ker), fontsize = 20)
    targets = ['Iris-setosa', 'Iris-versicolor', 'Iris-virginica']
    colors = ['r', 'g', 'b']
    for target, color in zip(targets,colors):
        indicesToKeep = finalDf['target'] == target
        ax.scatter(finalDf.loc[indicesToKeep, 'PC-1']
                   , finalDf.loc[indicesToKeep, 'PC-2']
                   , c = color
                   , s = 30)
    ax.legend(targets)
    ax.grid()
    plt.show() # FOR SHOWING THE PLOT
    #------------------- SAVING DATA INTO CSV FILE ------------
    finalDf.to_csv('iris_after_KPCA_using_'+str(ker)+'.csv')


#------------------------------------------------------
k=['linear','rbf','poly']
for i in k:
    Kernel_Pca(i)

[((114, 179), 'pandas.read_csv', 'pd.read_csv', (['"""D:\\\\Python_programs\\\\ML\\\\Iris Data\\\\KPCA\\\\iris.csv"""'], {}), "('D:\\\\Python_programs\\\\ML\\\\Iris Data\\\\KPCA\\\\iris.csv')\n", (125, 179), True, 'import pandas as pd\n'), ((659, 706), 'sklearn.decomposition.KernelPCA', 'KernelPCA', ([], {'n_components': '(4)', 'kernel': 'ker', 'gamma': '(15)'}), '(n_components=4, kernel=ker, gamma=15)\n', (668, 706), False, 'from sklearn.decomposition import KernelPCA\n'), ((810, 840), 'numpy.var', 'np.var', (['kpca_transform'], {'axis': '(0)'}), '(kpca_transform, axis=0)\n', (816, 840), True, 'import numpy as np\n'), ((1045, 1057), 'matplotlib.pyplot.legend', 'plt.legend', ([], {}), '()\n', (1055, 1057), True, 'import matplotlib.pyplot as plt\n'), ((1062, 1097), 'matplotlib.pyplot.xlabel', 'plt.xlabel', (['"""Principal Components """'], {}), "('Principal Components ')\n", (1072, 1097), True, 'import matplotlib.pyplot as plt\n'), ((1303, 1356), 'matplotlib.pyplot.xticks', 'plt.xticks', (['[1, 2, 3, 4]', 'pc'], {'fontsize': '(7)', 'rotation': '(30)'}), '([1, 2, 3, 4], pc, fontsize=7, rotation=30)\n', (1313, 1356), True, 'import matplotlib.pyplot as plt\n'), ((1357, 1385), 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""Variance Ratio"""'], {}), "('Variance Ratio')\n", (1367, 1385), True, 'import matplotlib.pyplot as plt\n'), ((1461, 1471), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (1469, 1471), True, 'import matplotlib.pyplot as plt\n'), ((1702, 1749), 'sklearn.decomposition.KernelPCA', 'KernelPCA', ([], {'n_components': '(2)', 'kernel': 'ker', 'gamma': '(15)'}), '(n_components=2, kernel=ker, gamma=15)\n', (1711, 1749), False, 'from sklearn.decomposition import KernelPCA\n'), ((1852, 1916), 'pandas.DataFrame', 'pd.DataFrame', ([], {'data': 'principalComponents', 'columns': "['PC-1', 'PC-2']"}), "(data=principalComponents, columns=['PC-1', 'PC-2'])\n", (1864, 1916), True, 'import pandas as pd\n'), ((1973, 2021), 'pandas.concat', 'pd.concat', (["[principalDf, df[['target']]]"], {'axis': '(1)'}), "([principalDf, df[['target']]], axis=1)\n", (1982, 2021), True, 'import pandas as pd\n'), ((2059, 2085), 'matplotlib.pyplot.figure', 'plt.figure', ([], {'figsize': '(8, 8)'}), '(figsize=(8, 8))\n', (2069, 2085), True, 'import matplotlib.pyplot as plt\n'), ((2686, 2696), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (2694, 2696), True, 'import matplotlib.pyplot as plt\n'), ((477, 493), 'sklearn.preprocessing.StandardScaler', 'StandardScaler', ([], {}), '()\n', (491, 493), False, 'from sklearn.preprocessing import StandardScaler\n'), ((871, 897), 'numpy.sum', 'np.sum', (['explained_variance'], {}), '(explained_variance)\n', (877, 897), True, 'import numpy as np\n')]