<jupyter_start><jupyter_text>****Decision Tree Classfier****<jupyter_code># Function to perform training with giniIndex. 
def train_using_gini(X_train, X_test, Y_train): 
  
    # Creating the classifier object 
    clf_gini = DecisionTreeClassifier(criterion = "gini", 
            random_state = 100,max_depth=10, min_samples_leaf=5) 
  
    # Performing training 
    clf_gini.fit(X_train, Y_train) 
    return clf_gini 
# Function to perform training with entropy. 
def train_using_entropy(X_train, X_test, Y_train): 
  
    # Decision tree with entropy 
    clf_entropy = DecisionTreeClassifier( 
            criterion = "entropy", random_state = 100, 
            max_depth = 10, min_samples_leaf = 5) 
  
    # Performing training 
    clf_entropy.fit(X_train, Y_train) 
    return clf_entropy 
# Function to make predictions 
def prediction(X_test, clf_object): 
  
    # Predicton on test with giniIndex 
    Y_pred = clf_object.predict(X_test) 
    print("Predicted values:") 
    print(Y_pred) 
    return Y_pred  
# Function to calculate accuracy 
def cal_accuracy(y_test, Y_pred): 
              
    print ("Accuracy : ", 
    accuracy_score(y_test,Y_pred)*100) 
      
def printTree(classifier):
    
    feature_names = ['Age','Ethnicity','BMI','Diastolic BP','Systolic BP', 'Pregnancies','Miscarriage','PreviousGDM',
                     'Family History','Exercise']
    target_names = ['GDM-Yes','GDM-No']
    
    #Build the tree
    dot_data = tree.export_graphviz(classifier, out_file=None,
                                    feature_names=feature_names,
                                    class_names=target_names, filled = True)
    #Draw tree
    tr = graphviz.Source(dot_data, format ="png")
    return tr
# Building Phase 
data = importdata() 

#Shuffle the dataset 
#data = data.sample(frac = 1)

X, Y, X_train, X_test, Y_train, Y_test = splitdataset(data) 
clf_gini = train_using_gini(X_train, X_test, Y_train) 
clf_entropy = train_using_entropy(X_train, X_test, Y_train) 

# Operational Phase 
print("Results Using Gini Index:") 

# Prediction using gini 
y_pred_gini = prediction(X_test, clf_gini) 
cal_accuracy(Y_test, y_pred_gini) 


print("Results Using Entropy:") 
# Prediction using entropy 
y_pred_entropy = prediction(X_test, clf_entropy) 
cal_accuracy(Y_test, y_pred_entropy) 


#Print Gini tree
tr_gini = printTree(clf_gini)
tr_gini
#Print entropy tree
tr_entropy = printTree(clf_entropy)
tr_entropy
#Feature importance using GINI
importance = clf_gini.feature_importances_

feature_importance = pd.Series(importance, index=X.columns)
feature_importance.nlargest(10).plot(kind='barh')
#Feature Importance using Entropy
importance_en = clf_entropy.feature_importances_

feature_importance_en = pd.Series(importance_en, index=X.columns)
feature_importance_en.nlargest(10).plot(kind='barh')
from sklearn import linear_model

model = linear_model.LinearRegression()

model.fit(X_train,Y_train)
y_predict = model.predict(X_test)
y_predict[0]
model.score (X_test, Y_test)
#DecisionTree Regression

from sklearn import tree
dtr = tree.DecisionTreeRegressor()
dtr.fit(X_train, Y_train)

predict_dtr = dtr.predict(X_test)
predict_dtr

dtr.score(X_test, Y_test)<jupyter_output><empty_output><jupyter_text>****Random Forest Classifier****<jupyter_code>RF = RandomForestClassifier(n_estimators=10,random_state=0)
RF.fit(X_train, Y_train) 
pred = RF.predict(X_test)
pred
#Import scikit-learn metrics module for accuracy calculation
from sklearn import metrics
# Model Accuracy, how often is the classifier correct?
print("Accuracy:",metrics.accuracy_score(Y_test, pred))
#Print tree
feature_names = ['Age','Ethnicity','BMI','Diastolic BP','Systolic BP', 'Pregnancies','Miscarriage','PreviousGDM',
                     'Family History','Exercise']
target_names = ['GDM-Yes','GDM-No']
    
dot_data = tree.export_graphviz(RF.estimators_[0], out_file=None,
                                    feature_names=feature_names,
                                    class_names=target_names, filled = True)
#Draw tree
tr = graphviz.Source(dot_data, format ="png")
tr
  
#Build all the trees
for i in range(len(RF.estimators_)):
    print(tree.export_text(RF.estimators_[i]))
#Feature importance using Random forest
importance_RF = RF.feature_importances_
feature_importance_RF = pd.Series(importance_RF, index=X.columns)
feature_importance_RF.nlargest(10).plot(kind='barh')
print(metrics.classification_report(Y_test, pred))
precision, recall, _ = metrics.precision_recall_curve(Y_test, pred, pos_label=1)
average_precision = metrics.average_precision_score(Y_test, pred)
print (precision)<jupyter_output>precision    recall  f1-score   support

           0       0.90      1.00      0.95        75
           1       0.00      0.00      0.00         8

    accuracy                           0.90        83
   macro avg       0.45      0.50      0.47        83
weighted avg       0.82      0.90      0.86        83

[0.09638554 1.        ]<jupyter_text>****Bagging Classifier****<jupyter_code>from sklearn.ensemble import BaggingClassifier
bag_clf=BaggingClassifier(base_estimator= clf_gini, n_estimators=100, bootstrap=True, n_jobs=-1, random_state=42)
bag_clf.fit(X_train,Y_train)
print ("Accuracy : ", accuracy_score(Y_test,bag_clf.predict(X_test)))<jupyter_output>Accuracy :  0.9036144578313253
{'fraction_non_alphanumeric': 0.10043257476020312, 'fraction_numerical': 0.021064510062065073, 'mean_word_length': 3.8389444949954505, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '180355', 'license': 'MIT', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1812, 'n_words': 359, 'path': 'Gestational Diabetes Prediction/gdm-stitched.ipynb', 'repo_name': 'gbhuvanes/DataScienceProjects', 'size': '5317'}