image_output_2.png image_output_1.png
from sklearn import preprocessing

from sklearn.feature_selection import RFE

from sklearn.svm import SVR

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

df = pd.read_csv('../input/predict-test-scores-of-students/test_scores.csv')
df

plt.style.use('ggplot')
fig, axes = plt.subplots(nrows=2)
ax1 = df.pretest.plot.kde(figsize=(16,10), ax=axes[0])
ax1 = df.posttest.plot.kde(ax=axes[0])
ax1.legend(['Pretest', 'Posttest'])
ax1.set_title('Density plot of pre/post test scores')
ax2 = df.n_student.plot.kde(ax=axes[1])
ax2.set_title('Density plot of number of students in class')
plt.tight_layout()
plt.show()

fig, axes = plt.subplots(figsize=(16,7))
ax1 = df.school.value_counts()[::-1].plot(kind='bar', ax=axes)
ax1.set_title('School distribution')
fig, axes = plt.subplots(ncols=2, figsize=(16,5))
ax2 = df.school_setting.value_counts().plot(kind='bar', ax=axes[0])
ax3 = df.school_type.value_counts().plot(kind='bar', ax=axes[1])
ax2.set_title('School setting')
ax3.set_title('School type')
plt.tight_layout()

fig, ax = plt.subplots(figsize=(15,5))
df.groupby('school').pretest.mean().sort_values().plot(kind='bar', ax=ax)
ax.set_title('Comparison of average scores by school')
plt.show()

from sklearn import preprocessing

label_decoder = dict()
for col in ['school', 'school_setting', 'school_type', 'classroom', 'teaching_method', 'gender', 'lunch']:
    le = preprocessing.LabelEncoder()
    le.fit_transform(df[col])
    label_decoder[col] = le
df_le = df[['school', 'school_setting', 'school_type', 'classroom', 'teaching_method', 'gender', 'lunch']].apply(le.fit_transform, axis='index')
df_le[['n_student', 'pretest', 'posttest']] = df[['n_student', 'pretest', 'posttest']]
df_le.insert(8, 'test_diff', df_le.posttest - df_le.pretest)

x = df_le.values
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
df_norm = pd.DataFrame(x_scaled)
df_norm.columns = df_le.columns
corr = df_norm.corr()
mask = np.zeros_like(corr)
mask[np.triu_indices_from(mask)] = True
with sns.axes_style('white'):
    fig, ax = plt.subplots(figsize=(15,10))
    sns.heatmap(corr, ax=ax, cmap=sns.color_palette('light:#e24a33', as_cmap=True), xticklabels=True, mask=mask, linewidths=.5)
    ax.set_title('Heatmap showing correlation of variables')
    plt.show()

from sklearn.feature_selection import RFE
from sklearn.svm import SVR
X, y = (df_le.iloc[:, :-1], df_le.iloc[:, -1])
estimator = SVR(kernel='linear')
selector = RFE(estimator, n_features_to_select=4)
selector = selector.fit(X, y)
selected_cols = [c for i, c in enumerate(X.columns) if selector.support_[i]]
print(f'Used recursive feature elimination to select the following columns for our training:\n\n{selected_cols}')