<jupyter_start><jupyter_text>Project Notebook -Model Quality & Improvement  Problem StatementAs a data professional working for a pharmaceutical company, you need to develop amodel that predicts whether a patient will be diagnosed with diabetes. The model needsto have an accuracy score greater than 0.85.  1.Data Importation<jupyter_code>#We will now need the required libraries to read our csv file 
# from an external  source. 

import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler


diabetes2 = pd.read_csv('/content/diabetes2.csv')
                 
diabetes2.head()<jupyter_output><empty_output><jupyter_text>2. Data Exploration<jupyter_code># explore the size of a dataset
diabetes2.shape

#explore the first and last records of a dataset

diabetes2[0:-5] 

#explore the data types 

diabetes2.dtypes<jupyter_output><empty_output><jupyter_text>3.Data Cleaning & Preparation<jupyter_code># Looking for null values in the  dataset
diabetes2.isnull()
# Confirming that dataset does not have any missing values

diabetes2[diabetes2.isnull()]

# Removing duplicated rows from the  Dataframe

diabetes2.drop_duplicates()

#checking if duplicates have been droppoed
diabetes2.duplicated().sum()
#previewing the dataframe after cleaning

diabetes2.sample(5)
#We start by Standardizing the numeric features.
#Then we create an instance of the StandardScaler() class and tune it using the training data. 

#data loading and splitting
diabetes2 = pd.read_csv('/content/diabetes2.csv')

target = diabetes2['Outcome']
features = diabetes2.drop('Outcome', axis=1)
features_train, features_valid, target_train, target_valid = train_test_split(
    features, target, test_size=0.25, random_state=12345
)

numeric = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness','Insulin',	'BMI','DiabetesPedigreeFunction',	'Age']

#stanardizing numeric features
scaler = StandardScaler()
scaler.fit(features_train[numeric])
features_train[numeric] = scaler.transform(features_train[numeric])

#transform validation set 
features_valid[numeric] = scaler.transform(features_valid[numeric])


#print(features_train.head())
features_train.head()<jupyter_output><empty_output><jupyter_text>4.Data Modeling (Using Decision Trees, Random Forest and Logistic Regression)<jupyter_code>#Decision Tree
#spliting dataset into train & test 

diabetes2 = pd.read_csv('/content/diabetes2.csv')

diabetes2_train  = train_test_split(diabetes2, test_size=0.25, random_state=12345)

test_features = diabetes2.drop(['Outcome'], axis=1)
test_target = diabetes2['Outcome'] 

#create model
model = DecisionTreeRegressor (random_state=12345)
#training model
model.fit(features, target)

#predictions
predictions = model.predict(test_features)

mse = mean_squared_error(test_target, predictions)

#Calculate RMSE

print('RMSE:', mse**0.5)


#Random Forest
#spliting dataset into train & test 


diabetes2 = pd.read_csv('/content/diabetes2.csv')

diabetes2_train  = train_test_split(diabetes2, test_size=0.25, random_state=12345)

test_features = diabetes2.drop(['Outcome'], axis=1)
test_target = diabetes2['Outcome'] 


#create model
model = RandomForestRegressor (random_state=12345, n_estimators=3)
#training model
model.fit(features, target)

#predictions
predictions = model.predict(test_features)

mse = mean_squared_error(test_target, predictions)

#Calculate RMSE

print('RMSE:', mse**0.5)
#Logistic Regression
#spliting dataset into train & test 

diabetes2 = pd.read_csv('/content/diabetes2.csv')

diabetes2_train  = train_test_split(diabetes2, test_size=0.25, random_state=12345)

test_features = diabetes2.drop(['Outcome'], axis=1)
test_target = diabetes2['Outcome'] 


features_train = diabetes2.drop(['Outcome'], axis=1)
target_train = diabetes2['Outcome'] 

#create model
model = LogisticRegression(random_state=12345, solver='liblinear')
#train model
model.fit(features_train,target_train)

#checking score accuracy
mse =(model.score(features_train, target_train))

print('RMSE:', mse**0.5)<jupyter_output>RMSE: 0.8809322713277489<jupyter_text>5.Model Evaluation<jupyter_code>#Training the best model with optimal hyperparameters on the entire dataset. 

#reading dataset and spliting data
diabetes2 = pd.read_csv('/content/diabetes2.csv')
diabetes2_train  = train_test_split(diabetes2, test_size=0.25, random_state=12345)


test_features = diabetes2.drop(['Outcome'], axis=1)
test_target = diabetes2['Outcome'] 

#create model
final_model = DecisionTreeRegressor (random_state=12345)
#training model
final_model.fit(features, target)

#predictions
predictions = final_model.predict(test_features)

mse = mean_squared_error(test_target, predictions)

#Calculate RMSE

mse = mean_squared_error(test_target, predictions)

rmse = mse ** 0.5
print('RMSE:', mse**0.5)<jupyter_output>RMSE: 0.0<jupyter_text>6.Hyparameter Tuning<jupyter_code>#reading daaframe and spliting dataset

diabetes2 = pd.read_csv('/content/diabetes2.csv')

diabetes2_train  = train_test_split(diabetes2, test_size=0.25, random_state=12345)

test_features = diabetes2.drop(['Outcome'], axis=1)
test_target = diabetes2['Outcome'] 

features_valid = diabetes2.drop(['Outcome'], axis=1)
target_valid = diabetes2['Outcome'] 

#create model
model = DecisionTreeRegressor (random_state=12345)
#training model
model.fit(features, target)

#predictions
#predictions = model.predict(test_features)

#mse = mean_squared_error(test_target, predictions)

for depth in range(1, 6):
        model =  DecisionTreeClassifier(random_state=12345,max_depth=depth)

        model.fit(features_train, target_train)

       
        predictions_valid = model.predict(features_valid)

        print("max_depth =", depth, ": ", end='')
        print(accuracy_score(target_valid, predictions_valid))<jupyter_output>max_depth = 1 : 0.3502604166666667
max_depth = 2 : 0.3502604166666667
max_depth = 3 : 0.5234375
max_depth = 4 : 0.6497395833333334
max_depth = 5 : 0.6510416666666666
{'fraction_non_alphanumeric': 0.09988848175880198, 'fraction_numerical': 0.041580372789549146, 'mean_word_length': 6.731527093596059, 'pattern_counts': {'":': 1, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '612710', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2256, 'n_tokens_neox': 2013, 'n_words': 471, 'path': 'Delight_Model_Quality&_ImprovementsL.ipynb', 'repo_name': 'Deli8t/data', 'size': '6277'}