#!/usr/bin/env python
# coding: utf-8

# # Simple lin regression

# In[1]:


import pandas as pd
import numpy as np


# In[2]:


df = pd.read_csv("train.csv")
df.sample(5)


# In[3]:


# preprocess
# figure out where missing
df.isna().sum()


# In[4]:


ndf = df[["Loan Amount Request (USD)", "Loan Sanction Amount (USD)", "Income (USD)"]].copy()
ndf.isna().sum()


# In[5]:


ndf = ndf.dropna()
ndf.isna().sum()


# In[6]:


ndf["Income (USD)"].max


# In[7]:


df.select_dtypes(include=['number']).columns


# In[8]:


import matplotlib.pyplot as plt

for col in ndf.columns:
    plt.title(f"Distribuition of {col}", fontweight="bold")
    plt.hist(df[col], bins=100)
    plt.xlabel("Amount")
    plt.ylabel("Frequency")
    plt.show()


# In[9]:


plt.title("Feature vs Target scatter plot")
plt.ylabel("Request")
plt.xlabel("Sanctioned")
plt.scatter(ndf["Loan Amount Request (USD)"], ndf["Loan Sanction Amount (USD)"], alpha=0.2)
plt.show()


# In[10]:


# train test split
from sklearn.model_selection import train_test_split

X = ndf[["Loan Amount Request (USD)", "Income (USD)"]]
Y = ndf["Loan Sanction Amount (USD)"]

x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=69)


# In[11]:


from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score

def eval_model(true, pred):
    mae = mean_absolute_error(true, pred)
    mse = mean_squared_error(true, pred)
    r2 = r2_score(true, pred)
    rmse = np.sqrt(mse)

    print(f"MAE: {mae}")
    print(f"MSE: {mse}")
    print(f"RMSE: {rmse}")
    print(f"R2: {r2}")
    print()


# In[20]:


from sklearn.linear_model import LinearRegression

model = LinearRegression()

model.fit(x_train, y_train)

y_train_pred = model.predict(x_train)
y_test_pred = model.predict(x_test)

print("Train")
eval_model(y_train_pred, y_train)

print("Test")
eval_model(y_test_pred, y_test)


# In[21]:


from sklearn.linear_model import Ridge
ridge_model = Ridge()

ridge_model.fit(x_train, y_train)

y_train_pred = ridge_model.predict(x_train)
y_test_pred = ridge_model.predict(x_test)

print("Train")
eval_model(y_train_pred, y_train)

print("Test")
eval_model(y_test_pred, y_test)


# In[24]:


from sklearn.linear_model import Lasso

lasso_model = Lasso()

lasso_model.fit(x_train, y_train)

y_train_pred = lasso_model.predict(x_train)
y_test_pred = lasso_model.predict(x_test)

print("Train")
eval_model(y_train_pred, y_train)

print("Test")
eval_model(y_test_pred, y_test)

