# ---
# 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/sirb2/Data-Visualization-Projects-with-Python-Libraries/blob/main/Dataset_Analysis_with_pandas_library.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="azReToS7wWX3"
# #### Goal of the Project
#
# This project is designed for you to practice and solve the activities that are based on the concepts covered in the following lessons:
#
#  1. Simple linear regression III - Model Evaluation
#  

# + [markdown] id="fR_SN7K6475D"
# ### Problem Statement
#
# The most important factor for an Insurance Company is to determine what premium charges must be paid by an individual. The charges depend on various factors like age, gender, income, etc. 
#
# Build a model that is capable of predicting the insurance charges a person has to pay depending on his/her age using simple linear regression. Also, evaluate the accuracy of your model by calculating the value of error metrics such as R-squared, MSE, RMSE, and MAE.
#
#
#

# + [markdown] id="lZt4yKiJwrUs"
#
# #### Activity 1:  Analysing the Dataset
#
# - Create a Pandas DataFrame for **Insurance** dataset using the below link. This dataset consists of following columns:
#
# |Field|Description|
# |---:|:---|
# |age|Age of primary beneficiary|
# |sex|Insurance contractor gender, female or male|
# |bmi|Body mass index|
# |children|Number of children covered by health insurance/number of dependents|
# |region|Beneficiary's residential area in the US, northeast, southeast, southwest, northwest|
# |charges|Individual medical costs billed by health insurance|
#
#
#   **Dataset Link:** https://s3-student-datasets-bucket.whjr.online/whitehat-ds-datasets/insurance_dataset.csv
#
# - Print the first five rows of the dataset. Check for null values and treat them accordingly.
#
# - Create a regression plot with `age` on X-axis and `charges` on Y-axis to identify the relationship between these two attributes.
#
#
#

# + id="6U6NaAy4WQgs" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="f3998c7d-424a-4e13-e2b3-27c8de02d422"
# Import modules
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Load the dataset
# Dataset Link: 'https://s3-student-datasets-bucket.whjr.online/whitehat-ds-datasets/insurance_dataset.csv'
df = pd.read_csv('https://s3-student-datasets-bucket.whjr.online/whitehat-ds-datasets/insurance_dataset.csv')
# Print first five rows using head() function
df.head()

# + id="jg7hAMJ4jKC5" colab={"base_uri": "https://localhost:8080/"} outputId="74373fd9-949f-4251-e838-671f5c839dc4"
# Check if there are any null values. If any column has null values, treat them accordingly
df.isnull().sum()

# + id="A8RW5WbUuR88" colab={"base_uri": "https://localhost:8080/", "height": 496} outputId="9c124bfe-fc10-481e-f419-6e8cf7ac118e"
# Create a regression plot between 'age' and 'charges'
plt.figure(figsize = (21, 7))
sns.regplot(df['age'], df['charges'], color = 'red')
plt.show()

# + [markdown] id="uG9YxYbpjgVG"
# ---

# + [markdown] id="uDTmlU-Mz0fI"
# #### Activity 2: Train-Test Split
#
# We have to determine the effect of `age` on insurance charges. Thus, `age` is the feature variable and `charges` is the target variable. 
#
# Split the dataset into training set and test set such that the training set contains 67% of the instances and the remaining instances will become the test set.

# + id="Ku_loAWZ0LXr"
# Split the DataFrame into the training and test sets.
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(df['age'], df['charges'], test_size = 0.33, random_state = 41)


# + [markdown] id="hCPg7ClP0Om1"
# ---

# + [markdown] id="ud8dLfCGjh0E"
# #### Activity 3: Model Training
#
# Implement simple linear regression using `sklearn` module in the following way:
#
# 1. Reshape the feature and the target variable arrays into two-dimensional arrays by using `reshape(-1, 1)` function of numpy module.
# 2. Deploy the model by importing the `LinearRegression` class and create an object of this class.
# 3. Call the `fit()` function on the LinearRegression object and print the slope and intercept values of the best fit line.
#

# + id="Xost35Q1XreI" colab={"base_uri": "https://localhost:8080/"} outputId="ae893bfa-c308-465b-bd49-589772fe41f7"
# 1. Create two-dimensional NumPy arrays for the feature and target variables. 
# Print the shape or dimensions of these reshaped arrays
def reshape_(frame):
  return frame.values.reshape(-1, 1)
x_train_reshaped = reshape_(x_train)
x_test_reshaped = reshape_(x_test)
y_train_reshaped = reshape_(y_train)
y_test_reshaped = reshape_(y_test)
print(x_train_reshaped.shape)
print(x_test_reshaped.shape)
print(y_train_reshaped.shape)
print(y_test_reshaped.shape)

# + id="U9iIV06LXuQP" colab={"base_uri": "https://localhost:8080/"} outputId="d4c34423-c903-40c0-c8dd-bad05e77d7ba"
# 2. Deploy linear regression model using the 'sklearn.linear_model' module.
from sklearn.linear_model import LinearRegression
# Create an object of the 'LinearRegression' class.
Anythingyoulike = LinearRegression()
# 3. Call the 'fit()' function
Anythingyoulike.fit(x_train_reshaped, y_train_reshaped)
 # Print the slope and intercept values
print(f"Slope: {Anythingyoulike.coef_[0][0]}\nIntercept: {Anythingyoulike.intercept_[0]}")

# + [markdown] id="cAPgWR45mrCo"
# ---

# + [markdown] id="CvcLZdremtHY"
# #### Activity 4: Model Prediction and Evaluation
#
# Predict the values for both training and test sets by calling the `predict()` function on the LinearRegression object. Also, calculate the $R^2$, MSE, RMSE and MAE values to evaluate the accuracy of your model.

# + id="hc3RPNgsX5-0" colab={"base_uri": "https://localhost:8080/"} outputId="bd08793d-2146-4805-adc8-deaead5fe3d8"
# Predict the target variable values for both training set and test set

# Call 'r2_score', 'mean_squared_error' & 'mean_absolute_error' functions of the 'sklearn' module. Calculate RMSE value by taking the square root of MSE.
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error

y_train_pred = Anythingyoulike.predict(x_train_reshaped)
y_test_pred = Anythingyoulike.predict(x_test_reshaped)

# Print these values for both training set and test set
print(f"Train Set\n{'-' * 50}")
print(f"R-squared: {r2_score(y_train_reshaped, y_train_pred):.3f}")
print(f"Mean Squared Error: {mean_squared_error(y_train_reshaped, y_train_pred):.3f}")
print(f"Root Mean Squared Error: {np.sqrt(mean_squared_error(y_train_reshaped, y_train_pred)):.3f}")
print(f"Mean Absolute Error: {mean_absolute_error(y_train_reshaped, y_train_pred):.3f}")
      
print(f"\n\nTest Set\n{'-' * 50}")
print(f"R-squared: {r2_score(y_test_reshaped, y_test_pred):.3f}")
print(f"Mean Squared Error: {mean_squared_error(y_test_reshaped, y_test_pred):.3f}")
print(f"Root Mean Squared Error: {np.sqrt(mean_squared_error(y_test_reshaped, y_test_pred)):.3f}")
print(f"Mean Absolute Error: {mean_absolute_error(y_test_reshaped, y_test_pred):.3f}")


# + [markdown] id="Bp0p4IT-Dn_w"
# ---

StarcoderdataJupyterScriptsDedupFiltered