"""
<h1>Background</h1>
<p>We are going to explore some graduate admissions data for Post-Graduate Acceptance Probablilities based upon the following features:<ol>
    <li>GRE Scores</li>
    <li>TOEFL Scores</li>
    <li>Undergraduate School Rating</li>
    <li>Application Letter (SOP)</li>
    <li>Letters of Recommendation(LOR)</li>
    <li>Undergraduate GPA (CGPA)</li>
    <li> Undergraduate Research</li>
</ol>
</p>
"""
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
df = pd.read_csv('/kaggle/input/graduate-admissions/Admission_Predict.csv')
"""
<h1>Data Exploration</h1>
"""
df.head()
"""
Now to drop some of unnecessary info. We can refer to the in-built indexing rather than using the provided Serial No. Makes calling data much simpler.
"""
df.drop(columns='Serial No.', inplace=True)
df.head()
"""
Starting to look better already
"""
df.columns = ['GRE','TOEFL','Rating','SOP','LOR','CGPA','Research','Chance']
"""
Now let's rename those columns for ease of referencing.
"""
df.head()
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
df.describe()
"""
Let's take a look for any missing values that may require imputing.
"""
df.isnull().sum()
"""
Wow did we luck out! Clean looking so far.
"""
plt.figure(figsize=(8,8))
sns.heatmap(df.corr(), annot=True, cmap='Blues')
"""
<h1>Correlation</h1>
<p>Taking a look at our correlation matrix, let's see some of the largest relators to Chance:</p>
<ul>
    <li>CGPA</li>
    <li>GRE</li>
    <li>TOEFL</li>
    </ul>
   
"""
f,(ax1,ax2) = plt.subplots(1,2, figsize=(20,5))
ax1.set_title("Admission Chance vs GRE Score")
ax2.set_title("Admission Chance vs TOEFL Score")
sns.regplot(x=df.GRE, y=df.Chance, ax=ax1)
sns.regplot(x=df.TOEFL, y=df.Chance, ax=ax2)

f,(ax1,ax2) = plt.subplots(1,2, figsize=(20,5))
ax1.set_title("Admission Chance vs GRE (Research)")
ax2.set_title("Admission Chance vs TOEFL (Research)")
sns.scatterplot(x=df.GRE, y=df.Chance, hue=df.Research, ax=ax1)
sns.scatterplot(x=df.TOEFL, y=df.Chance, hue=df.Research, ax=ax2)
sns.lmplot(x='GRE', y='Chance', hue='Research', data=df)
plt.title("Admission Chance vs GRE Score (Research)")
sns.lmplot(x='TOEFL', y='Chance', hue='Research', data=df)
plt.title("Admission Chance vs TOEFL Score (Research)")
sns.lmplot(x='CGPA', y='Chance', hue='Research', data=df)
plt.title("Admission Chance vs CGPA")
f,(ax1,ax2,ax3) = plt.subplots(1,3, figsize=(20,5))
ax1.set_title("Admission Chance vs LOR")
ax2.set_title("Admission Chance vs SOP")
ax3.set_title("Admission Chance vs School Rating")
sns.scatterplot(x=df.LOR, y=df.Chance,ax=ax1)
sns.scatterplot(x=df.SOP, y=df.Chance,ax=ax2)
sns.scatterplot(x=df.Rating, y=df.Chance, ax=ax3)

"""
Certainly a point can be made for a positive trend for increases in LOR, SOP, and Rating scores. However, for the sake of developing a quick and simple initial Linear Regression Model, let's emit these for the initial run and see what happens!
"""
"""
<p>Let's unpack some of the visuals that were just presented:</p>
<ul>
    <li>A visually apparent linear relationship between CGPA, GRE, and TOEFL with Acceptance Chance</li>
    <li>A higher Chance based on research in conjunction with GRE and TOEFL scores</li>
</ul>
<p>Certainly a point can be made for a positive trend for increases in LOR, SOP, and Rating scores. However, for the sake of developing a quick and simple initial Linear Regression Model, let's emit these for the initial run and see what happens!</p>
"""
features=['GRE','TOEFL','CGPA','Research']
X=df[features]
y=df['Chance']
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.metrics import mean_squared_error
scaler = StandardScaler()
scaler.fit(X)
X_scaled = scaler.transform(X)
X_train,X_test,y_train,y_test = train_test_split(X_scaled,y,test_size=0.25, random_state=0)
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
"""
<p>Now we have our data scaled and split into the appropriate training and testing sets. Now it's time to develop our Linear Regression Model</p>
"""
lr = LinearRegression()
lr.fit(X_train,y_train)
y_hat=lr.predict(X_test)
print("R2: ",lr.score(X_test,y_test))
print("RMSE: ",np.sqrt(mean_squared_error(y_test,y_hat)))
"""
<h1>Conclusion</h1>
<p>Using some quick feature selection and evaluation, we came up with a linear model using only 4 features (GRE, TOEFL, CGPA, and Research) to produce an effective first attempt model. This should always be a good starting point. Produce something, evaluate, and iterate!</p>

<p>Please provide feedback and insight below. Always looking to improve!</p>
"""