<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<jupyter_output><empty_output><jupyter_text>Load Dataset<jupyter_code>df = pd.read_csv('~/Documents/GitHub/Breast-Cancer-Prediction-Analysis/Dataset/breast_cancer_dataset_wisnconsin_diagonistic.csv')
df.head(12)
df.info()
df.drop(['id'], axis=1, inplace=True)
df.drop(['Unnamed: 32'], axis=1, inplace=True)
# df.isnull().sum()
print(df['diagnosis'].value_counts())
plt.figure(figsize=(6, 6))
sns.countplot(
    x='diagnosis',
    data=df
);
df.info()
df["diagnosis"].replace({"M": 1, "B": -1}, inplace=True)<jupyter_output><empty_output><jupyter_text>Feature Importance & Correlations<jupyter_code># Numerical Input - Categorical Output
# Kendall's Method
plt.figure(figsize=(32, .8))
sns.heatmap(
    data=df.corr("kendall").iloc[:1, 1:],
    annot=True,
    fmt='.0%',
    cmap='coolwarm'
        
);
# sns.pairplot(df, hue="diagnosis")
X = df.iloc[:, 1:]
y = df.iloc[:,:1]<jupyter_output><empty_output><jupyter_text>Resampling<jupyter_code>from imblearn.over_sampling import SMOTE

resample = SMOTE(sampling_strategy='minority')
X, y = resample.fit_sample(X, y)<jupyter_output><empty_output><jupyter_text>Split The Dataset<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=0, stratify= y)<jupyter_output><empty_output><jupyter_text>Feature Scaling  Max Abosulate Scaling<jupyter_code>from sklearn.preprocessing import MaxAbsScaler

scaler = MaxAbsScaler().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>KNN - K Nearest Neighbour<jupyter_code>from sklearn.neighbors import KNeighborsClassifier<jupyter_output><empty_output><jupyter_text>Hyper Parameter Tuning  Finding Right Value of K<jupyter_code>knn = KNeighborsClassifier(n_neighbors= 5)
# from sklearn.metrics import f1_score
# score_y = []
# num_k_x = []
# i = 3
# while i <= 25:
#     model = KNeighborsClassifier(n_neighbors= i)
#     model = model.fit(X_train, y_train)
#     num_k_x.append(i)
#     y_test_pred = model.predict(X_test)
#     score_y.append(f1_score(y_test_pred, y_test))
#     #score_y.append(model.score(X_test, y_test))
#     i = i + 2
# model_acc = pd.DataFrame({"F1_Score":score_y, "K Value":num_k_x})
# sns.lineplot(x = "K Value", y = "F1_Score", data=model_acc)
# plt.show()
knn.fit(X_train, y_train)
from sklearn.metrics import confusion_matrix
y_test_pred = knn.predict(X_test)
tp, fn, fp, tn = confusion_matrix(y_test, y_test_pred).ravel()
precision = (tp)/ (tp + fp)
recall =  tp/ (tp + fn)
print("True Negative:", tn)
print("True Positve:", tp)
print("False Positive:", fp)
print("False Negative:", fn)

print("Precision: ", (tp)/ (tp + fp))
print("Recall: ", tp/ (tp + fn))
print("Accuracy: ", (tp + tn)/(tp + fp + tn + fn))
print("F1 Score:" ,  2 * (precision * recall) / (precision + recall))
knn_f1 = 2 * (precision * recall) / (precision + recall)
from sklearn.metrics import classification_report
y_test_pred = knn.predict(X_test)
print(classification_report(y_test, y_test_pred))
from yellowbrick.classifier import confusion_matrix

plt.figure(figsize=(6, 5))
visualizer = confusion_matrix(
    knn,
    X_test, y_test,
    is_fitted=True
    
)
visualizer.show();<jupyter_output><empty_output><jupyter_text>Cross validation and Learning Curve<jupyter_code>from sklearn.model_selection import StratifiedKFold

cv = StratifiedKFold(
    n_splits=10,random_state=0,
    shuffle=False
)
from yellowbrick.model_selection import LearningCurve

plt.figure(figsize=(15, 10))
visualizer = LearningCurve(
    model= knn, 
    cv=cv, 
    scoring='f1_weighted', 
    train_sizes=np.linspace(0.3, 1.0, 10), 
    n_jobs=4,
    is_fitted=True,
    stratify = True
)
visualizer.fit(X, y);
visualizer.show();
from yellowbrick.model_selection import CVScores
visualizer = CVScores(knn, 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
from yellowbrick.classifier import ROCAUC

plt.figure(figsize=(15, 10))
visualizer = ROCAUC(
    knn,
    is_fitted=True,
)
visualizer.score(X_test, y_test)
visualizer.show();<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1349755700325733, 'fraction_numerical': 0.0124185667752443, 'mean_word_length': 6.079250720461095, 'pattern_counts': {'":': 4, '<': 38, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '309928', 'license': 'MIT', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1863, 'n_words': 345, 'path': 'notebooks/KNN/knn_max_abs_scale.ipynb', 'repo_name': 'nikhilkadam18/Breast-Cancer-Diagnosis-Using-Probabilistic-Ensemble-Based-Machine-Learning-Algorithms', 'size': '4912'}