<jupyter_start><jupyter_text>Import Basic packages<jupyter_code>import numpy as np
import matplotlib.pyplot as plt 
import pandas as pd  
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Load Data-Set<jupyter_code>from sklearn.datasets import load_boston
boston_dataset = load_boston()
X = pd.DataFrame(boston_dataset.data, columns=boston_dataset.feature_names)
Y =boston_dataset.target
X.head()<jupyter_output><empty_output><jupyter_text>2D Visualization using PCA<jupyter_code>names = boston_dataset["feature_names"]
print("Column Names in Data : ",names)
print("the shape of sample data = ",X.shape)
# Data-preprocessing: Standardizing the data

from sklearn.preprocessing import StandardScaler
standardized_data = StandardScaler().fit_transform(X)
print(standardized_data.shape)

#find the co-variance matrix which is : X^T * X
sample_data = standardized_data

# matrix multiplication using numpy
covar_matrix = np.matmul(sample_data.T , sample_data)

print ( "The shape of variance matrix = ", covar_matrix.shape)

# finding the top two eigen-values and corresponding eigen-vectors 
# for projecting onto a 2-Dim space.

from scipy.linalg import eigh 

# the parameter 'eigvals' is defined (low value to heigh value) 
# eigh function will return the eigen values in asending order
# this code generates only the top 2 eigenvalues.
values, vectors = eigh(covar_matrix, eigvals=(11,12))

print("Shape of eigen vectors = ",vectors.shape)
# converting the eigen vectors into (2,d) shape for easyness of further computations
vectors = vectors.T

print("Updated shape of eigen vectors = ",vectors.shape)
# here the vectors[1] represent the eigen vector corresponding 1st principal eigen vector
# here the vectors[0] represent the eigen vector corresponding 2nd principal eigen vector
# projecting the original data sample on the plane 
# formed by two principal eigen vectors by vector-vector multiplication.
new_coordinates = np.matmul(vectors, sample_data.T)

print (" Rsultanat new data points' shape ", vectors.shape, "X", sample_data.T.shape," = ", new_coordinates.shape)


# appending label to the 2d projected data
new_coordinates = np.vstack((new_coordinates, Y)).T

# creating a new data frame for ploting the labeled points.
dataframe = pd.DataFrame(data=new_coordinates, columns=("1st_principal", "2nd_principal", "label"))
print("*"*80)
print(dataframe.head(10))
# ploting the 2d data points with seaborn
import seaborn as sns
sns.FacetGrid(dataframe, hue="label", height=6).map(plt.scatter, '1st_principal', '2nd_principal')
plt.show()<jupyter_output><empty_output><jupyter_text>PCA using Scikit-Learn<jupyter_code># initializing the pca
from sklearn.decomposition import PCA

# configuring the parameteres
# the number of components = 2
pcamodel = PCA(n_components=2)
pca = pcamodel.fit_transform(X)

# pca_reduced will contain the 2-d projects of simple data
print("shape of pca_reduced.shape = ", pca.shape)
# attaching the label for each 2-d data point 
pca_data = np.vstack((pca.T, Y)).T

# creating a new data fram which help us in ploting the result data
pca_df = pd.DataFrame(data=pca_data, columns=("1st_principal", "2nd_principal", "label"))
sns.FacetGrid(pca_df, hue="label", height=6).map(plt.scatter, '1st_principal', '2nd_principal')
plt.show()<jupyter_output><empty_output><jupyter_text>PCA for dimensionality redcution (not for visualization)<jupyter_code># PCA for dimensionality redcution (non-visualization)

pcamodel = PCA(n_components=12)
pca = pcamodel.fit_transform(X)

percentage_var_explained = pcamodel.explained_variance_ / np.sum(pcamodel.explained_variance_);

cum_var_explained = np.cumsum(percentage_var_explained)

# Plot the PCA spectrum
plt.figure(1, figsize=(6, 4))

plt.clf()
plt.plot(cum_var_explained, linewidth=2)
plt.axis('tight')
plt.grid()
plt.xlabel('n_components')
plt.ylabel('Cumulative_explained_variance')
plt.show()


# If we take 200-dimensions, approx. 90% of variance is expalined.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09974937343358396, 'fraction_numerical': 0.010776942355889725, 'mean_word_length': 6.487804878048781, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '156712', 'license': 'MIT', 'n_tokens_mistral': 1379, 'n_tokens_neox': 1304, 'n_words': 367, 'path': 'Machine-Learning/Algorithms/PCA/PCA_BOston.ipynb', 'repo_name': 'dhirenkokal/beginners-only', 'size': '3990'}