<jupyter_start><jupyter_text>1. Problem Statement Design a predictive model with the use of machine learning algorithms to predict the charges of a medical insurance  2. Data Source The dataset is downloaded from Kaggle  3. Importing Libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
import missingno

# Load dataset
df = pd.read_csv('Train_Data.csv')
df.head()<jupyter_output><empty_output><jupyter_text>4. Data Preprocessing<jupyter_code># Data Preprocessing
df.info()
df.describe(exclude=[object]).T
df.describe(include=[object]).T<jupyter_output><empty_output><jupyter_text>5. Finding Categorical and Numerical Features in a Dataset<jupyter_code># Categorical features
categorical_features = [column_name for column_name in df.columns if df[column_name].dtype == 'O']
print(f"Number of Categorical Features : {len(categorical_features)}")
print(f"Categorical Features : {categorical_features}")
# Numerical Features
numerical_features = [column_name for column_name in df.columns if df[column_name].dtype != 'O']
print(f"Number of Numerical Features : {len(numerical_features)}")
print(f"Numerical Features : {numerical_features}")<jupyter_output>Number of Numerical Features : 4
Numerical Features : ['age', 'bmi', 'children', 'charges']<jupyter_text>6. Cardinality(no of unique values in the each categorical ) check for Categorical features:<jupyter_code>for each_feature in categorical_features:
    unique_values = len(df[each_feature].unique())
    print("Cardinality(no. of unique values) of {} are: {}".format(each_feature, unique_values))<jupyter_output>Cardinality(no. of unique values) of sex are: 2
Cardinality(no. of unique values) of smoker are: 2
Cardinality(no. of unique values) of region are: 4<jupyter_text>No high cardinality features found in the categorical features  7. Handling Missing Values<jupyter_code>print(df.isna().sum())
missingno.matrix(df)<jupyter_output>age         0
sex         0
bmi         0
smoker      0
region      0
children    0
charges     0
dtype: int64<jupyter_text>8. Exploratory Data Analysis<jupyter_code>plt.figure(figsize=(10,5))
sns.countplot('sex',data=df)

plt.figure(figsize=(10,5))
sns.lineplot(x='age',y='charges',data=df)
sns.histplot(x='charges',data=df)
sns.scatterplot(x='charges',y='bmi',data=df)
plt.figure(figsize=(10,5))
sns.scatterplot(x='age',y='bmi',hue='sex',data=df,s=100)
plt.title("Body Mass Index regarding Age with gender")
plt.xlabel("Age")
plt.ylabel("BMI")
plt.figure(figsize=(10,5))
sns.scatterplot(x='age',y='bmi',hue='smoker',data=df,s=100)
plt.title("Body Mass Index regarding Age with smoke")
plt.xlabel("Age")
plt.ylabel("BMI")<jupyter_output><empty_output><jupyter_text>10. Encoding of Categorical Features:<jupyter_code>categorical_features
df.smoker.value_counts()
df.region.value_counts()
df['sex'].replace({'male':1,'female':0},inplace=True)
df['smoker'].replace({'no':0,'yes':1},inplace=True)
df['region'].replace({'northeast':1,'southwest':2,'northwest':3,'southeast':4},inplace=True)
df.head()<jupyter_output><empty_output><jupyter_text>11.Correlation<jupyter_code>plt.figure(figsize=(15,7))
sns.heatmap(df.corr(),
           annot=True,
           cmap='magma')
# independent features and labels
X = df.drop('charges',axis=1)
y = df['charges']<jupyter_output><empty_output><jupyter_text>12. Feature Importance<jupyter_code>from sklearn.ensemble import ExtraTreesRegressor
model = ExtraTreesRegressor()
model.fit(X,y)
model.feature_importances_
feature_importance = dict(zip(df.columns,model.feature_importances_))
feature_importance 
feature_imp = pd.Series(model.feature_importances_,index=X.columns)
feature_imp.nlargest(10).plot(kind='barh')<jupyter_output><empty_output><jupyter_text>13. Splittting Data into Training and Testing set<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train,y_test = train_test_split(X,
                                                  y,
                                                  test_size=0.2,
                                                  random_state=42)
print("Length of Training Data: {}".format(len(X_train)))
print("Length of Testing Data: {}".format(len(X_test)))<jupyter_output>Length of Training Data: 2904
Length of Testing Data: 726<jupyter_text>14. Feature Scaling<jupyter_code>from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)<jupyter_output><empty_output><jupyter_text>15. Modelling<jupyter_code>from sklearn.ensemble import RandomForestRegressor
np.random.seed(42)
random_forest = RandomForestRegressor()
random_forest.fit(X_train,y_train)
random_forest.score(X_test,y_test)
from sklearn.linear_model import ElasticNet
np.random.seed(42)
elastic_net =ElasticNet()
elastic_net.fit(X_train,y_train)
elastic_net.score(X_test,y_test)
from sklearn.tree import DecisionTreeRegressor
np.random.seed(42)
decision_tree = DecisionTreeRegressor()
decision_tree.fit(X_train,y_train)
decision_tree.score(X_test,y_test)
!pip install catboost
from catboost import CatBoostRegressor
np.random.seed(42)
cat = CatBoostRegressor(verbose=0)
cat.fit(X_train,y_train)
cat.score(X_test,y_test)

!pip install xgboost
from xgboost import XGBRegressor
np.random.seed(42)
xgb = XGBRegressor(verbsoe=0)
xgb.fit(X_train,y_train)
xgb.score(X_test,y_test)
# Cross validation for best performing model
from sklearn.model_selection import cross_val_score
np.random.seed(42)
cross_validation = cross_val_score(random_forest,X,y,cv=5)
cross_validation = np.mean(cross_validation)
cross_validation<jupyter_output><empty_output><jupyter_text>16. Evaulation Metrics<jupyter_code>from sklearn.metrics import r2_score,mean_absolute_error,mean_squared_error

# y_predictions
y_pred = random_forest.predict(X_test)
y_pred

print(f"R^2 Score: {r2_score(y_test,y_pred)}")
print(f"Mean Absolute Error : {mean_absolute_error(y_test,y_pred)}")
print(f"Mean Squared Error : {mean_squared_error(y_test,y_pred)}")

df2 = pd.DataFrame(data={"actual":y_test,
                        "predicted":y_pred})
df2['MAE'] = df2['actual'] - df2['predicted']
df2['MSE'] = np.square(df2['MAE'])
df2<jupyter_output><empty_output><jupyter_text>17. Hyperparameter Tunining<jupyter_code>random_forest.get_params()
from sklearn.model_selection import RandomizedSearchCV

param_distributions = {'n_estimators': [120,300,500,800,1200],
                     "max_depth" : [5,8,15,25,30,None],
                     "min_samples_split" : [1,2,5,10,15,100],
                     "min_samples_leaf":[1,2,5,10],
                     "max_features": ["log2","sqrt",None]}

# Randomized SerachCV
ideal_model = RandomizedSearchCV(estimator=random_forest,
                                param_distributions = param_distributions,
                                n_iter=10,
                                cv=5,
                                verbose=2)
ideal_model.fit(X_train,y_train)
ideal_model.best_params_
ideal_model.score(X_test,y_test)<jupyter_output><empty_output><jupyter_text>18. Saving a model<jupyter_code>import pickle

# saving existing model to file
pickle.dump(random_forest,open('random_forest.pkl','wb'))

# Load saved model
loaded_model = pickle.load(open('random_forest.pkl','rb'))
loaded_model
df.columns
# make prediction using loaded_model
pred = loaded_model.predict(X_test)
pred<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12111842989649146, 'fraction_numerical': 0.020298427207958058, 'mean_word_length': 5.714801444043322, 'pattern_counts': {'":': 4, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '593785', 'license': 'Apache-2.0', 'n_tokens_mistral': 2710, 'n_tokens_neox': 2553, 'n_words': 458, 'path': 'Medical_Insurance_Notebook.ipynb', 'repo_name': 'Parvez13/Name_Entity_Recognition', 'size': '7439'}