# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/cccaaannn/machine_learning_colab/blob/master/feature_selection/data_mining_hw3.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="ae-V4qaGhO4R"
# Feature selection methods

# + [markdown] id="OwSK-WXIipBC"
# ![](https://3qeqpr26caki16dnhd19sv6by6v-wpengine.netdna-ssl.com/wp-content/uploads/2019/11/How-to-Choose-Feature-Selection-Methods-For-Machine-Learning.png)  

# + [markdown] id="E15TtspDorbq"
# download-unzip data

# + id="XOcFSUa2oUxh"
# !wget https://archive.ics.uci.edu/ml/machine-learning-databases/00320/student.zip
# !unzip student.zip

# + [markdown] id="YTehCzGZiXLt"
# imports

# + id="4dlKYCOs2pAz"
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

from scipy.stats import stats

# + [markdown] id="EPMZKeXEiNMK"
# load data

# + id="w5loWGNn2pDO"
df = pd.read_csv("student-mat.csv", index_col=0, delimiter = ";")

# + [markdown] id="NLFwWh0ViUc8"
# print head

# + id="FsiL7Mwu-AwG"
df.head(5)

# + [markdown] id="B3CJoxg93zVU"
# Options

# + id="yxf6mjCQ2r_Q"
p_value_threshold = 0.05

# + [markdown] id="4jug-6aTnI3N"
# Pearson 

# + id="PifvzS0lsJ7c"
num_cols = df.drop(["G3","G2","G1"], axis=1)._get_numeric_data().columns
pearson_corrs = []
for col in num_cols:
    s = stats.pearsonr(df[col], df['G3'])
    if(s[1] < p_value_threshold):
        pearson_corrs.append((col, s[0], s[1]))

pearson_corrs = sorted(pearson_corrs, reverse=True, key=lambda tup: abs(tup[1]))

for i in pearson_corrs:
    print("columns: {0:<12} correlation: {1:<22} p-values: {2:<22}".format(i[0], i[1], i[2]))

# + [markdown] id="w5gahTxMm1Dl"
# Spearman

# + id="23AnZeVVwomu"
num_cols = df.drop(["G3","G2","G1"], axis=1)._get_numeric_data().columns
spearmar_corrs = []
for col in num_cols:
    s = stats.spearmanr(df[col], df['G3'])
    if(s[1] < p_value_threshold):
        spearmar_corrs.append((col, s[0], s[1]))

spearmar_corrs = sorted(spearmar_corrs, reverse=True, key=lambda tup: abs(tup[1]))

for i in spearmar_corrs:
    print("columns: {0:<12} correlation: {1:<22} p-values: {2:<22}".format(i[0], i[1], i[2]))

# + [markdown] id="lYjg7eXWmzOF"
# Kendall

# + id="0uIN2LYE2GyH"
num_cols = df.drop(["G3","G2","G1"], axis=1)._get_numeric_data().columns
kendall_corrs = []
for col in num_cols:
    s = stats.kendalltau(df[col], df['G3'])
    if(s[1] < p_value_threshold):
        kendall_corrs.append((col, s[0], s[1]))
        
kendall_corrs = sorted(kendall_corrs, reverse=True, key=lambda tup: abs(tup[1]))

for i in kendall_corrs:
    print("columns: {0:<12} correlation: {1:<22} p-values: {2:<22}".format(i[0], i[1], i[2]))

# + [markdown] id="Un6iQrNdKvPi"
# f-value

# + id="PLXhf-jkFTfw"
cols = df.columns
num_cols = df._get_numeric_data().columns

categorical_cols = list(set(cols) - set(num_cols))

f_value_corrs = []
for categorical_col in categorical_cols:
    groups = []
    column_categories = df[categorical_col].unique()
    for column_category in column_categories:
        groups.append(df[df[categorical_col] == column_category].age)

    f, p = stats.f_oneway(*groups)
    if(p < p_value_threshold):
        f_value_corrs.append((categorical_col, f, p, ", ".join(list(column_categories))))



f_value_corrs = sorted(f_value_corrs, reverse=True, key=lambda tup: abs(tup[1]))

for i in f_value_corrs:
    print("columns: {0:<12} correlation: {1:<20} p-values: {2:<22} categories: {3:<22}".format(i[0], i[1], i[2], i[3]))


StarcoderdataJupyterScriptsDedupFiltered