# load wine dataset

import pandas as pd
from sklearn.model_selection import train_test_split

df_wine = pd.read_csv('https://archive.ics.uci.edu/ml/'
                      'machine-learning-databases/wine/wine.data',
                      header=None)

X, y = df_wine.iloc[:, 1:].values, df_wine.iloc[:, 0].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

"""
Explanation: Compressing Data via Dimensionality Reduction
An alternative approach to feature selection for dimensionality reduction is feature extraction. In this notebook, I'll implement dimensionality reduction techniques through numpy and scikit-learn.

Principal component analysis (PCA) for unsupervised data compression
Linear Discriminant Analysis (LDA) as a supervised dimensionality reduction technique for maximizing class separability

Implement PCA step by step via Numpy

Standardize the d-dimensional dataset.
Construct the covariance matrix.
Decompose the covariance matrix into its eigenvectors and eigenvalues.
Select k eigenvectors that correspond to the k largest eigenvalues, where k is the dimensionality of the new feature subspace (k<=d).
Construct a projection matrix W from the "top" k eigenvectors.
Transform the d-dimensional input dataset X using the projectionmatrix W to obtain the new k-dimensional feature subspace.
End of explanation
"""


from sklearn.preprocessing import StandardScaler

sc = StandardScaler()
X_train_std = sc.fit_transform(X_train)
X_test_std = sc.transform(X_test)

"""
Explanation: 1) Standardize the d-dimensional dataset
End of explanation
"""


import numpy as np

cov_mat = np.cov(X_train_std.T)


"""
Explanation: 2) Construct the covariance matrix.
End of explanation
"""


eigen_vals, eigen_vecs = np.linalg.eig(cov_mat)

print('\nEigenvalues \n%s' % eigen_vals)

"""
Explanation: 3) Decompose the covariance matrix into its eigenvectors and eigenvalues.
End of explanation
"""


# Make a list of (eigenvalue, eigenvector) tuples
eigen_pairs = [(np.abs(eigen_vals[i]), eigen_vecs[:, i]) for i in range(len(eigen_vals))]

# Sort the (eigenvalue, eigenvector) tuples from high to low
eigen_pairs.sort(key=lambda k: k[0], reverse=True)


"""
Explanation: 4) Sort eigenvalues and select k eigenvectors that correspond to the k largest eigenvalues, where k is the dimensionality of the new feature subspace (k<=d)
End of explanation
"""


w = np.hstack((eigen_pairs[0][1][:, np.newaxis],
               eigen_pairs[1][1][:, np.newaxis]))
print('Matrix W:\n', w)

"""
Explanation: 5) Construct a projection matrix W from the "top" k eigenvectors.
End of explanation
"""


X_train_pca = X_train_std.dot(w)

import matplotlib.pyplot as plt
colors = ['r', 'b', 'g']
markers = ['s', 'x', 'o']

for l, c, m in zip(np.unique(y_train), colors, markers):
    plt.scatter(X_train_pca[y_train == l, 0], 
                X_train_pca[y_train == l, 1], 
                c=c, label=l, marker=m)

plt.xlabel('PC 1')
plt.ylabel('PC 2')
plt.legend(loc='lower left')
plt.show()

"""
Explanation: 6) Transform the d-dimensional input dataset X using the projection matrix W to obtain the new k-dimensional feature subspace
End of explanation
"""


from sklearn.decomposition import PCA

pca = PCA(n_components=2)
X_train_pca = pca.fit_transform(X_train_std)
X_test_pca = pca.transform(X_test_std)


plt.scatter(X_train_pca[:, 0], X_train_pca[:, 1])
plt.xlabel('PC 1')
plt.ylabel('PC 2')
plt.show()

"""
Explanation: Implement PCA via scikit-learn
End of explanation
"""


pca = PCA(n_components=None)
X_train_pca = pca.fit_transform(X_train_std)
pca.explained_variance_ratio_

"""
Explanation: If we are interested in the explained variance ratios of the different principal
components, we can simply initialize the PCA class with the n_components parameter
set to None, so all principal components are kept and the explained variance ratio can
then be accessed via the 'explained_variance_ratio_':
End of explanation
"""


np.set_printoptions(precision=4)

mean_vecs = []
for label in range(1, 4):
    mean_vecs.append(np.mean(X_train_std[y_train == label], axis=0))


"""
Explanation: Implement LDA step by step via Numpy

Standardize the d-dimensional dataset (d is the number of features).
For each class, compute the d-dimensional mean vector.
Construct the between-class scatter matrix S_B and the within-class scatter matrix.
Compute the eigenvectors and corresponding eigenvalues of the matrix $S_W^{-1}S_B$.
Choose the k eigenvectors that correspond to the k largest eigenvalues to construct a d×k-dimensional transformation matrix W; the eigenvectors are the columns of this matrix.
Project the samples onto the new feature subspace using the transformation matrix W

2) Calculate the mean vectors for each class:
End of explanation
"""


d = 13  # number of features
S_W = np.zeros((d, d))
for label, mv in zip(range(1, 4), mean_vecs):
    class_scatter = np.cov(X_train_std[y_train == label].T)
    S_W += class_scatter

"""
Explanation: 3a) Compute the within-class scatter matrix S_W (same as computing the covariance matrix):
End of explanation
"""


mean_overall = np.mean(X_train_std, axis=0)
d = 13  # number of features
S_B = np.zeros((d, d))
for i, mean_vec in enumerate(mean_vecs):
    n = X_train[y_train == i + 1, :].shape[0]
    mean_vec = mean_vec.reshape(d, 1)  # make column vector
    mean_overall = mean_overall.reshape(d, 1)  # make column vector
    S_B += n * (mean_vec - mean_overall).dot((mean_vec - mean_overall).T)

"""
Explanation: 3b) Compute the between-class scatter matrix:
End of explanation
"""


eigen_vals, eigen_vecs = np.linalg.eig(np.linalg.inv(S_W).dot(S_B))

# Make a list of (eigenvalue, eigenvector) tuples
eigen_pairs = [(np.abs(eigen_vals[i]), eigen_vecs[:, i])
               for i in range(len(eigen_vals))]

# Sort the (eigenvalue, eigenvector) tuples from high to low
eigen_pairs = sorted(eigen_pairs, key=lambda k: k[0], reverse=True)

# Visually confirm that the list is correctly sorted by decreasing eigenvalues

print('Eigenvalues in decreasing order:\n')
for eigen_val in eigen_pairs:
    print(eigen_val[0])

"""
Explanation: 4) Solve the generalized eigenvalue problem for the matrix $S_W^{-1}S_B$:
End of explanation
"""


w = np.hstack((eigen_pairs[0][1][:, np.newaxis].real,
              eigen_pairs[1][1][:, np.newaxis].real))
print('Matrix W:\n', w)

"""
Explanation: We only have two nonzero eigenvalues (rank 2). The first two linear discriminants capture
about 100 percent of the useful information in the Wine training dataset.
5) Choose the k eigenvectors that correspond to the k largest eigenvalues to construct a d × k-dimensional transformation matrix W
End of explanation
"""


X_train_lda = X_train_std.dot(w)

"""
Explanation: 6) Project the samples onto the new feature subspace using the transformation matrix W
End of explanation
"""


from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
lda = LDA(n_components=2)
X_train_lda = lda.fit_transform(X_train_std, y_train)
X_test_lda = lda.transform(X_test_std)

"""
Explanation: Implement LDA via scikit-learn
End of explanation
"""


