text_plain_output_1.png
from sklearn.decomposition import PCA

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

student_data_mat = pd.read_csv('../input/student_math.csv', delimiter=';')
student_data_por = pd.read_csv('../input/student_language.csv', delimiter=';')

student_data_mat.shape

student_data_por.shape

student_data = pd.merge(student_data_mat, student_data_por, how='outer')

student_data.shape

columns_string = student_data.columns[student_data.dtypes == object]
columns_string

student_data = pd.get_dummies(student_data, columns=columns_string, drop_first=True)

student_data.shape

student_data.drop(axis=1, labels=['G1'], inplace=True)

label = student_data['G3'].values
predictors = student_data.drop(axis=1, labels=['G3']).values

pca = PCA(n_components=len(student_data.columns) - 1)
pca.fit(predictors)
variance = pca.explained_variance_
variance

variance_ratio_cum_sum = np.cumsum(np.round(pca.explained_variance_ratio_, decimals=4) * 100)
print(variance_ratio_cum_sum)
plt.plot(variance_ratio_cum_sum)