<jupyter_start><jupyter_text>Project by: <NAME>*<jupyter_code>#import the libraries
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
#import the datasets
df=pd.read_csv('insurance.csv')
df.head()
df.tail()
df.describe(include='all')
#check for the null values
df.isnull().sum()
#visualize the null values
sns.heatmap(df.isnull()==True, cbar=False, yticklabels=False)
#It shows that there is no null values in the datasets
df.shape
df.info()
# x and y variables
x=df.drop('expenses', axis=1)
y=df['expenses'].values
print(y)
#Now convert the catagorical variables to numeric variables
cols=['sex', 'smoker', 'region']
x= pd.get_dummies(data=x, columns=cols, drop_first=True)
x.head()
#scale the features age and bmi to the same scale as of the other features
from sklearn.preprocessing import MinMaxScaler
scaler=MinMaxScaler()
scale_cols=['age', 'bmi']
x[scale_cols]=scaler.fit_transform(x[scale_cols])
x.head()
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test=train_test_split(x, y, test_size=0.25, random_state=0)
tree_reg=DecisionTreeRegressor(max_depth=2, criterion='mse')
tree_reg.fit(x_train, y_train)
y_pred_reg=tree_reg.predict(x_test)
y_pred_reg.T<jupyter_output><empty_output><jupyter_text>How to Check the Model Variabiity and Bias-Variance Trade-Off??<jupyter_code>from sklearn.model_selection import cross_validate, KFold
K_Fold=KFold(n_splits=10, shuffle=True, random_state=42)
depth={}

for i in range(2,11):
    tree_cv=cross_validate(DecisionTreeRegressor(max_depth=i), x, y, cv=K_Fold, scoring=['r2'])
    depth['depth_' +str(i)]=tree_cv['test_r2']
depth
#Now convert it into dataframe
df=pd.DataFrame(depth)
df
# Now choose the best depth through visualozing boxplots
plt.figure(figsize=(10,10))
sns.set_style('whitegrid')
sns.boxplot(data=df)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
plt.ylabel("R2 score", fontsize=14)
plt.title("Decision Tree variability and Bias-Variance Trade-off")
plt.show()<jupyter_output><empty_output><jupyter_text>Random Forest Regressor<jupyter_code>from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test=train_test_split(x, y, test_size=0.30, random_state=42)
rf=RandomForestRegressor(random_state=42)
rf.fit(x_train, y_train)
y_pred=rf.predict(x_test)
y_pred
from sklearn.metrics import accuracy_score, mean_squared_error, r2_score
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import explained_variance_score
mean_squared_error(y_test, y_pred)
mean_absolute_error(y_test, y_pred)
r2_score(y_test, y_pred)
explained_variance_score(y_test, y_pred)
from sklearn.metrics import mean_squared_error
from math import sqrt
rms = sqrt(mean_squared_error(y_test, y_pred))
print("Root Mean Squared Error::", rms)
#other method
import math
from sklearn.metrics import mean_squared_error
rmse = math.sqrt(mean_squared_error(y_test, y_pred))
rmse
#other method
RMSD = sqrt(mean_squared_error(y_test, y_pred))
print(RMSD)<jupyter_output>4664.029824514447<jupyter_text>let's try by scaling of the features<jupyter_code>df=pd.read_csv('insurance.csv')
df.head(2)
df2=df.copy()
df2.head()<jupyter_output><empty_output><jupyter_text>Solve the catagorical variables<jupyter_code>df2['sex']=pd.get_dummies(df2['sex'])
df2['smoker']=pd.get_dummies(df2['smoker'])
df2['region']=pd.get_dummies(df2['region'])
df2.head(2)
x=df2.drop('expenses', axis=1).values
from sklearn.preprocessing import StandardScaler
scaler=StandardScaler().fit(x)
x_scaled=scaler.transform(x)
x_scaled
y=df2['expenses'].values
x_scaled.ndim
y.ndim
y=y.reshape(-1,1)
y.ndim
import warnings
warnings.filterwarnings('ignore')
from sklearn.ensemble import RandomForestRegressor
rfg=RandomForestRegressor()
rfr.fit(x_scaled,y)
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test=train_test_split(x_scaled, y, test_size=0.33, random_state=42)
rfr.fit(x_train, y_train)
y_pred1=rfr.predict(x_test)
y_pred1
from sklearn.metrics import accuracy_score, mean_squared_error, r2_score
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import explained_variance_score
from sklearn.metrics import mean_squared_error
from math import sqrt
import math
from sklearn.metrics import mean_squared_error
rmse = math.sqrt(mean_squared_error(y_test, y_pred1))
rmse
mean_squared_error(y_test, y_pred1)
mean_absolute_error(y_test, y_pred1)
r2_score(y_test, y_pred1)<jupyter_output><empty_output><jupyter_text>Support Vector Machine<jupyter_code>from sklearn.svm import SVR
svr=SVR()
svr.fit(x_train, y_train)
y_pred2=svr.predict(x_test)
y_pred2
rmse = math.sqrt(mean_squared_error(y_test, y_pred2))
rmse
mean_absolute_error(y_test, y_pred2)
mean_squared_error(y_test, y_pred2)
explained_variance_score(y_test, y_pred2)
r2_score(y_test, y_pred2)<jupyter_output><empty_output><jupyter_text>Explanation of r2_score: r2_score. R^2 (coefficient of determination) regression score function. Best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse)<jupyter_code># Reference:
# machine-learning_Dr.B. (2020, March 18). Lesson 17 Decision Trees. 
# YouTube. https://www.youtube.com/watch?v=KIuB9nsVKqY&t=709s  
#https://towardsdatascience.com/hyperparameter-tuning-the-random-forest-in-python-using-scikit-learn-28d2aa77dd74<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11040029245110583, 'fraction_numerical': 0.020471577408152076, 'mean_word_length': 8.967213114754099, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '433441', 'license': 'MIT', 'n_tokens_mistral': 2169, 'n_tokens_neox': 2060, 'n_words': 412, 'path': 'Multiple Linear Regression in Machine Learning(Explanation of Best R2_score).ipynb', 'repo_name': 'Sudip-Pandit/Regression-in-Machine-Learning', 'size': '5477'}