<jupyter_start><jupyter_text>Importing Necessary Modules<jupyter_code>import sys
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use("ggplot")
plt.rcParams['figure.figsize'] = (12, 8)
import seaborn as sns
sns.set(style='whitegrid', color_codes=True)
import warnings
warnings.filterwarnings('ignore')
from sklearn.feature_selection import chi2,f_classif, mutual_info_classif, SelectKBest
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.model_selection import StratifiedKFold
from yellowbrick.model_selection import CVScores
from sklearn.metrics import confusion_matrix<jupyter_output><empty_output><jupyter_text>Load Dataset<jupyter_code>df = pd.read_csv('../../datasets/PCOS_clean_data_without_infertility.csv')
df.head(12)
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 538 entries, 0 to 537
Data columns (total 42 columns):
 #   Column                  Non-Null Count  Dtype  
---  ------                  --------------  -----  
 0   PCOS (Y/N)              538 non-null    int64  
 1   Age (yrs)               538 non-null    int64  
 2   Weight (Kg)             538 non-null    float64
 3   Height(Cm)              538 non-null    float64
 4   BMI                     538 non-null    float64
 5   Blood Group             538 non-null    int64  
 6   Pulse rate(bpm)         538 non-null    int64  
 7   RR (breaths/min)        538 non-null    int64  
 8   Hb(g/dl)                538 non-null    float64
 9   Cycle(R/I)              538 non-null    int64  
 10  Cycle length(days)      538 non-null    int64  
 11  Marraige Status (Yrs)   538 non-null    float64
 12  Pregnant(Y/N)           538 non-null    int64  
 13  No. of aborptions       538 non-null    int64  
 14  I   beta-HCG(mIU/mL)    538 non-null    fl[...]<jupyter_text>Feature Selection For Dependant & Independant Variable<jupyter_code>X = df.drop(["PCOS (Y/N)",
            "Blood Group",
            "Height(Cm)",
            "Pregnant(Y/N)",
             "PRG(ng/mL)",
            "RR (breaths/min)",
            "No. of aborptions",
            "FSH/LH",
            "I   beta-HCG(mIU/mL)",
            "II    beta-HCG(mIU/mL)",
            "TSH (mIU/L)",
            "FSH(mIU/mL)",
            "LH(mIU/mL)",
            "Waist:Hip Ratio",
            "PRL(ng/mL)",
            "BP _Diastolic (mmHg)",
            "BP _Systolic (mmHg)",
            "Reg.Exercise(Y/N)",
            "RBS(mg/dl)"
            ],axis=1)
y = df[["PCOS (Y/N)"]]
X.shape
y.shape<jupyter_output><empty_output><jupyter_text>Testing Function<jupyter_code>def test_results(model, X_test, y_test):
    from sklearn.metrics import confusion_matrix
    y_pred = model.predict(X_test)
    tn, fp, fn, tp = confusion_matrix(y_test, y_pred).ravel()

    accuracy = (tp + tn)/(tp + fp + tn + fn)
    print("Accuracy: ", '{:.2f}'.format(accuracy * 100))
    print("True Negative:", tn)
    print("True Positve:", tp)
    print("False Positive:", fp)
    print("False Negative:", fn)
    print()
    print("-------------------------------------------------------")
    print("Negative Class Results")
    precision = (tp / (tp + fp)) 
    recall =  (tp  / (tp + fn)) 
    f1_score = (2 * (precision * recall) / (precision + recall)) 
    print("Precision (N): ", '{:.2f}'.format(precision * 100))
    print("Recall (N): ", '{:.2f}'.format(recall * 100))
    print("F1 Score (N):" ,  '{:.2f}'.format(f1_score * 100))
    print()
    print("-------------------------------------------------------")
    print("Positive Class Results")
    precision = (tn / (tn + fn))
    recall =  (tn  / (tn + fp))
    f1_score = (2 * (precision * recall) / (precision + recall))
    print("Precision (P): ", '{:.2f}'.format(precision * 100))
    print("Recall (P): ", '{:.2f}'.format(recall * 100))
    print("F1 Score (P):" , '{:.2f}'.format(f1_score * 100))<jupyter_output><empty_output><jupyter_text>Confusion Matrix Visualizer<jupyter_code>from yellowbrick.classifier import confusion_matrix

def vis_conf(model, X_test, y_test):
    plt.figure(figsize=(6, 5))
    visualizer = confusion_matrix(
        model,
        X_test, y_test,
        is_fitted=True,
        classes=['Negative', 'Positive']
    )
    visualizer.show();<jupyter_output><empty_output><jupyter_text>Data Augmentation  Resampling on Complete Dataset<jupyter_code>from imblearn.combine import SMOTEENN

resample = SMOTEENN(sampling_strategy=1/1, random_state =0)
X, y = resample.fit_sample(X, y)<jupyter_output><empty_output><jupyter_text>Splitting<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0, stratify= y)<jupyter_output><empty_output><jupyter_text>Scaling<jupyter_code>from sklearn.preprocessing import MinMaxScaler

scaler = MinMaxScaler().fit(X_train)

X_train = scaler.transform(X_train)
X_train = pd.DataFrame(X_train)

X_test = scaler.transform(X_test)
X_test = pd.DataFrame(X_test)


# Setting Column Names from dataset
X_train.columns = X.columns
X_test.columns = X.columns<jupyter_output><empty_output><jupyter_text>Resampling on Training Dataset Only<jupyter_code># from imblearn.over_sampling import SMOTE, ADASYN

# resample = ADASYN(sampling_strategy='auto', random_state =0)
# X_train, y_train = resample.fit_sample(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Shape of Final Training Testing Set<jupyter_code>X_train.shape
X_test.shape
X.shape<jupyter_output><empty_output><jupyter_text>ML Models  SVM<jupyter_code>from sklearn.svm import SVC
svm = SVC(kernel="linear", gamma='auto', probability= True, random_state=0)
svm.fit(X_train, y_train)
test_results(svm, X_test, y_test)
vis_conf(svm, X_test, y_test)
# cv = StratifiedKFold(
#     n_splits=15,random_state=0,
#     shuffle=True
# ) 
# visualizer = CVScores(svm, cv=cv, scoring='f1_weighted', is_fitted=False, stratify = True)
# visualizer.fit(X, y)        # Fit the data to the visualizer
# visualizer.show()           # Finalize and render the figure<jupyter_output><empty_output><jupyter_text>KNN<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors= 9, p = 1, metric= 'minkowski')
knn.fit(X_train, y_train)
test_results(knn, X_test, y_test)
vis_conf(knn, X_test, y_test)<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(n_estimators=1500, criterion="entropy", random_state=0) 
rf.fit(X_train, y_train)
test_results(rf, X_test, y_test)
vis_conf(rf, X_test, y_test)
# cv = StratifiedKFold(
#     n_splits=15,random_state=0,
#     shuffle=True
# ) 
# visualizer = CVScores(rf, cv=cv, scoring='f1_weighted', is_fitted=False, stratify = True)
# visualizer.fit(X, y)        # Fit the data to the visualizer
# visualizer.show()           # Finalize and render the figure<jupyter_output><empty_output><jupyter_text>Adaboost<jupyter_code>from sklearn.ensemble import AdaBoostClassifier 
from sklearn.tree import DecisionTreeClassifier
ada = AdaBoostClassifier(n_estimators= 11, random_state=0)
ada.fit(X_train, y_train)
test_results(ada, X_test, y_test)
vis_conf(ada, X_test, y_test)<jupyter_output><empty_output><jupyter_text>XgBoost<jupyter_code>import xgboost as xgb
xgb = xgb.XGBClassifier(max_depth=5, n_estimators=1500, learning_rate=0.3,scale_pos_weight=10,
                            random_state= 0, n_jobs=-1)<jupyter_output><empty_output><jupyter_text>Cross Validation<jupyter_code>sum(cross_val_score(xgb, X, y, cv=10))/10<jupyter_output><empty_output><jupyter_text>Final Model Build<jupyter_code>xgb = xgb.fit(X_train, y_train)
test_results(xgb, X_test, y_test)
vis_conf(xgb, X_test, y_test)
cv = StratifiedKFold(
    n_splits=10,random_state=0,
    shuffle=False
) 
visualizer = CVScores(xgb, cv=cv, scoring='f1_weighted', is_fitted=True, stratify = True)
visualizer.fit(X, y)        # Fit the data to the visualizer
visualizer.show()           # Finalize and render the figure<jupyter_output><empty_output><jupyter_text>Naive Bayes'<jupyter_code>from sklearn.naive_bayes import ComplementNB, GaussianNB, MultinomialNB
nb = GaussianNB()
nb.fit(X_train, y_train)
test_results(nb, X_test, y_test)
vis_conf(nb, X_test, y_test)<jupyter_output><empty_output><jupyter_text>MLP<jupyter_code>from sklearn.neural_network import MLPClassifier
mlp = MLPClassifier(hidden_layer_sizes=(60, 60), activation='tanh', solver='adam', alpha=0.0001, learning_rate='adaptive', learning_rate_init=0.001, power_t=0.5, max_iter=200, shuffle=False, random_state=0, tol=0.0001, verbose=False, warm_start=False, momentum=0.9, nesterovs_momentum=True, early_stopping=False, validation_fraction=0.1, beta_1=0.9, beta_2=0.999, epsilon=1e-08, n_iter_no_change=10, max_fun=15000)
mlp.fit(X_train, y_train)
test_results(mlp, X_test, y_test)
print(y['PCOS (Y/N)'].value_counts())
plt.figure(figsize=(6, 6))
sns.countplot(
    x='PCOS (Y/N)',
    data=y
);<jupyter_output>1    270
0    209
Name: PCOS (Y/N), dtype: int64
{'fraction_non_alphanumeric': 0.13962721958751517, 'fraction_numerical': 0.02757251571633396, 'mean_word_length': 3.8285410010649628, 'pattern_counts': {'":': 0, '<': 76, '<?xml version=': 0, '>': 76, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '353896', 'license': 'MIT', 'n_tokens_mistral': 3599, 'n_tokens_neox': 3320, 'n_words': 636, 'path': 'notebooks/Final/PCOS_ML.ipynb', 'repo_name': 'skinan/Improved-Sampling-and-Feature-Selection-to-Support-Extreme-Gradient-Boosting-For-PCOS-Diagnosis', 'size': '9067'}