<jupyter_start><jupyter_text>Find Most Correlated Variables with Final Grade<jupyter_code>def format_data(df):
    # Targets are final grade of student
    labels = df['Score']
    # Drop the school and the grades from features
    df = df.drop(columns=['school', 'G1', 'G2', 'Score', 'percentile'])
    
    # One-Hot Encoding of Categorical Variables
    df = pd.get_dummies(df)
    
    df['y'] = list(labels)
    
    most_correlated = df.corr().abs()['y'].sort_values(ascending=False)
    
    # Maintain the top 8 most correlation features with score
    most_correlated = most_correlated[:10]
    
    df = df.ix[:, most_correlated.index]
    df = df.drop(columns = 'higher_no')
    
    # Split into training/testing sets with 25% split
    X_train, X_test, y_train, y_test = train_test_split(df, labels, 
                                                        test_size = 0.25,
                                                        random_state=42)
    
    return X_train, X_test, y_train, y_test
X_train, X_test, y_train, y_test = format_data(df)
X_train.head()<jupyter_output><empty_output><jupyter_text>Pair Plots of Variables<jupyter_code># Function to calculate correlation coefficients
def corrfunc(x, y, **kws):
    r, _ = stats.pearsonr(x, y)
    ax = plt.gca()
    ax.annotate("r = {:.2f}".format(r), size = 12,
                xy=(.1, .9), xycoords=ax.transAxes)

# Pair grid with correlation coefficients
g = sns.PairGrid(X_train, palette=["red"])
g.map_upper(plt.scatter, s=10)
g.map_diag(sns.distplot, kde=False)
g.map_lower(sns.kdeplot, cmap="Blues_d")
g.map_lower(corrfunc);<jupyter_output><empty_output><jupyter_text>Baseline Models<jupyter_code>def evaluate_predictions(predictions, true):
    rmse = np.sqrt(mean_squared_error(predictions, true))
    mae = mean_absolute_error(predictions, true)
    
    return rmse, mae
median_pred = X_train['y'].median()
median_preds = [median_pred for _ in range(len(X_test))]
true = X_test['y']
mb_rmse, mb_mae = evaluate_predictions(median_preds, true)
print('Median Baseline RMSE: {:.4f}.'.format(mb_rmse))
print('Median Baseline MAE:  {:.4f}.'.format(mb_mae))
X_train.head()
lm = LinearRegression()
lm.fit(X_train.iloc[:, 1:], X_train['y'])
lm_pred = lm.predict(X_test.iloc[:, 1:])
lm_rmse, lm_mae = evaluate_predictions(lm_pred, true)
print('Linear Regression RMSE: {:.4f}.'.format(lm_rmse))
print('Linear Regression MAE:  {:.4f}.'.format(lm_mae))<jupyter_output>Linear Regression RMSE: 2.4922.
Linear Regression MAE:  2.0367.
{'fraction_non_alphanumeric': 0.1364, 'fraction_numerical': 0.014, 'mean_word_length': 3.74573055028463, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '345424', 'license': 'MIT', 'n_tokens_mistral': 959, 'n_tokens_neox': 908, 'n_words': 185, 'path': 'bayesian_lr/.ipynb_checkpoints/Bayesian Linear Regression-checkpoint.ipynb', 'repo_name': 'stuartzong/Data-Analysis', 'size': '2500'}