# -*- coding: utf-8 -*-
"""Breast cancer detection.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/github/Azariagmt/data-science-notebooks/blob/master/Breast_cancer_detection.ipynb

**Objective:** to determine if the cancer is Malignant or Benign.
"""

import pandas as pd
breast_cancer_data = pd.read_csv('https://raw.githubusercontent.com/dphi-official/Datasets/master/breast_cancer/Training_set_breastcancer.csv', index_col='id')
df_with_id =  pd.read_csv('https://raw.githubusercontent.com/dphi-official/Datasets/master/breast_cancer/Training_set_breastcancer.csv')
data = breast_cancer_data
data.head()

X = data.drop('diagnosis', axis=1)
y = data['diagnosis']

from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, OneHotEncoder , LabelEncoder
from sklearn.linear_model import LogisticRegression
import numpy as np

le = LabelEncoder()
y = le.fit_transform(y)

y = pd.Series(data=y, name='diagnosis', index = list(df_with_id['id']))
y

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state= 101)

# Define preprocessing for numeric columns (normalize them so they're on the same scale)
numeric_features = [x for x in range(30)]
numeric_transformer = Pipeline(steps=[
    ('scaler', StandardScaler())])

# Combine preprocessing steps
preprocessor = ColumnTransformer(
    transformers=[
        ('num', numeric_transformer, numeric_features)])

# Create preprocessing and training pipeline
reg = 0.01
pipeline = Pipeline(steps=[('preprocessor', preprocessor),
                           ('logregressor', LogisticRegression(C=1/reg, solver="liblinear"))])


# fit the pipeline to train a logistic regression model on the training set
model = pipeline.fit(X_train, (y_train))
print (model)

def evaluate():
  predictions = model.predict(X_test)
  y_scores = model.predict_proba(X_test)
  cm = confusion_matrix(y_test, predictions)
  print ('Confusion Matrix:\n',cm, '\n')
  print('Accuracy:', accuracy_score(y_test, predictions))
  print("Overall Precision:",precision_score(y_test, predictions))
  print("Overall Recall:",recall_score(y_test, predictions))
  auc = roc_auc_score(y_test,y_scores[:,1])
  print('\nAUC: ' + str(auc))

  # calculate ROC curve
  fpr, tpr, thresholds = roc_curve(y_test, y_scores[:,1])

  # plot ROC curve
  fig = plt.figure(figsize=(6, 6))
  # Plot the diagonal 50% line
  plt.plot([0, 1], [0, 1], 'k--')
  # Plot the FPR and TPR achieved by our model
  plt.plot(fpr, tpr)
  plt.xlabel('False Positive Rate')
  plt.ylabel('True Positive Rate')
  plt.title('ROC Curve')
  plt.show()

evaluate()

from sklearn.ensemble import RandomForestClassifier

# Create preprocessing and training pipeline
pipeline = Pipeline(steps=[('preprocessor', preprocessor),
                           ('logregressor', RandomForestClassifier(n_estimators=100))])

# fit the pipeline to train a random forest model on the training set
model = pipeline.fit(X_train, (y_train))
print (model)

evaluate()
c9052f9929112391daff438a1cde029dee045861
<mask token>


def evaluate():
    predictions = model.predict(X_test)
    y_scores = model.predict_proba(X_test)
    cm = confusion_matrix(y_test, predictions)
    print('Confusion Matrix:\n', cm, '\n')
    print('Accuracy:', accuracy_score(y_test, predictions))
    print('Overall Precision:', precision_score(y_test, predictions))
    print('Overall Recall:', recall_score(y_test, predictions))
    auc = roc_auc_score(y_test, y_scores[:, 1])
    print('\nAUC: ' + str(auc))
    fpr, tpr, thresholds = roc_curve(y_test, y_scores[:, 1])
    fig = plt.figure(figsize=(6, 6))
    plt.plot([0, 1], [0, 1], 'k--')
    plt.plot(fpr, tpr)
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('ROC Curve')
    plt.show()


<mask token>

<mask token>
data.head()
<mask token>
y
<mask token>
print(model)


def evaluate():
    predictions = model.predict(X_test)
    y_scores = model.predict_proba(X_test)
    cm = confusion_matrix(y_test, predictions)
    print('Confusion Matrix:\n', cm, '\n')
    print('Accuracy:', accuracy_score(y_test, predictions))
    print('Overall Precision:', precision_score(y_test, predictions))
    print('Overall Recall:', recall_score(y_test, predictions))
    auc = roc_auc_score(y_test, y_scores[:, 1])
    print('\nAUC: ' + str(auc))
    fpr, tpr, thresholds = roc_curve(y_test, y_scores[:, 1])
    fig = plt.figure(figsize=(6, 6))
    plt.plot([0, 1], [0, 1], 'k--')
    plt.plot(fpr, tpr)
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('ROC Curve')
    plt.show()


evaluate()
<mask token>
print(model)
evaluate()

<mask token>
breast_cancer_data = pd.read_csv(
    'https://raw.githubusercontent.com/dphi-official/Datasets/master/breast_cancer/Training_set_breastcancer.csv'
    , index_col='id')
df_with_id = pd.read_csv(
    'https://raw.githubusercontent.com/dphi-official/Datasets/master/breast_cancer/Training_set_breastcancer.csv'
    )
data = breast_cancer_data
data.head()
X = data.drop('diagnosis', axis=1)
y = data['diagnosis']
<mask token>
le = LabelEncoder()
y = le.fit_transform(y)
y = pd.Series(data=y, name='diagnosis', index=list(df_with_id['id']))
y
<mask token>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
    random_state=101)
numeric_features = [x for x in range(30)]
numeric_transformer = Pipeline(steps=[('scaler', StandardScaler())])
preprocessor = ColumnTransformer(transformers=[('num', numeric_transformer,
    numeric_features)])
reg = 0.01
pipeline = Pipeline(steps=[('preprocessor', preprocessor), ('logregressor',
    LogisticRegression(C=1 / reg, solver='liblinear'))])
model = pipeline.fit(X_train, y_train)
print(model)


def evaluate():
    predictions = model.predict(X_test)
    y_scores = model.predict_proba(X_test)
    cm = confusion_matrix(y_test, predictions)
    print('Confusion Matrix:\n', cm, '\n')
    print('Accuracy:', accuracy_score(y_test, predictions))
    print('Overall Precision:', precision_score(y_test, predictions))
    print('Overall Recall:', recall_score(y_test, predictions))
    auc = roc_auc_score(y_test, y_scores[:, 1])
    print('\nAUC: ' + str(auc))
    fpr, tpr, thresholds = roc_curve(y_test, y_scores[:, 1])
    fig = plt.figure(figsize=(6, 6))
    plt.plot([0, 1], [0, 1], 'k--')
    plt.plot(fpr, tpr)
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('ROC Curve')
    plt.show()


evaluate()
<mask token>
pipeline = Pipeline(steps=[('preprocessor', preprocessor), ('logregressor',
    RandomForestClassifier(n_estimators=100))])
model = pipeline.fit(X_train, y_train)
print(model)
evaluate()

<mask token>
import pandas as pd
breast_cancer_data = pd.read_csv(
    'https://raw.githubusercontent.com/dphi-official/Datasets/master/breast_cancer/Training_set_breastcancer.csv'
    , index_col='id')
df_with_id = pd.read_csv(
    'https://raw.githubusercontent.com/dphi-official/Datasets/master/breast_cancer/Training_set_breastcancer.csv'
    )
data = breast_cancer_data
data.head()
X = data.drop('diagnosis', axis=1)
y = data['diagnosis']
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, OneHotEncoder, LabelEncoder
from sklearn.linear_model import LogisticRegression
import numpy as np
le = LabelEncoder()
y = le.fit_transform(y)
y = pd.Series(data=y, name='diagnosis', index=list(df_with_id['id']))
y
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
    random_state=101)
numeric_features = [x for x in range(30)]
numeric_transformer = Pipeline(steps=[('scaler', StandardScaler())])
preprocessor = ColumnTransformer(transformers=[('num', numeric_transformer,
    numeric_features)])
reg = 0.01
pipeline = Pipeline(steps=[('preprocessor', preprocessor), ('logregressor',
    LogisticRegression(C=1 / reg, solver='liblinear'))])
model = pipeline.fit(X_train, y_train)
print(model)


def evaluate():
    predictions = model.predict(X_test)
    y_scores = model.predict_proba(X_test)
    cm = confusion_matrix(y_test, predictions)
    print('Confusion Matrix:\n', cm, '\n')
    print('Accuracy:', accuracy_score(y_test, predictions))
    print('Overall Precision:', precision_score(y_test, predictions))
    print('Overall Recall:', recall_score(y_test, predictions))
    auc = roc_auc_score(y_test, y_scores[:, 1])
    print('\nAUC: ' + str(auc))
    fpr, tpr, thresholds = roc_curve(y_test, y_scores[:, 1])
    fig = plt.figure(figsize=(6, 6))
    plt.plot([0, 1], [0, 1], 'k--')
    plt.plot(fpr, tpr)
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('ROC Curve')
    plt.show()


evaluate()
from sklearn.ensemble import RandomForestClassifier
pipeline = Pipeline(steps=[('preprocessor', preprocessor), ('logregressor',
    RandomForestClassifier(n_estimators=100))])
model = pipeline.fit(X_train, y_train)
print(model)
evaluate()

# -*- coding: utf-8 -*-
"""Breast cancer detection.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/github/Azariagmt/data-science-notebooks/blob/master/Breast_cancer_detection.ipynb

**Objective:** to determine if the cancer is Malignant or Benign.
"""

import pandas as pd
breast_cancer_data = pd.read_csv('https://raw.githubusercontent.com/dphi-official/Datasets/master/breast_cancer/Training_set_breastcancer.csv', index_col='id')
df_with_id =  pd.read_csv('https://raw.githubusercontent.com/dphi-official/Datasets/master/breast_cancer/Training_set_breastcancer.csv')
data = breast_cancer_data
data.head()

X = data.drop('diagnosis', axis=1)
y = data['diagnosis']

from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, OneHotEncoder , LabelEncoder
from sklearn.linear_model import LogisticRegression
import numpy as np

le = LabelEncoder()
y = le.fit_transform(y)

y = pd.Series(data=y, name='diagnosis', index = list(df_with_id['id']))
y

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state= 101)

# Define preprocessing for numeric columns (normalize them so they're on the same scale)
numeric_features = [x for x in range(30)]
numeric_transformer = Pipeline(steps=[
    ('scaler', StandardScaler())])

# Combine preprocessing steps
preprocessor = ColumnTransformer(
    transformers=[
        ('num', numeric_transformer, numeric_features)])

# Create preprocessing and training pipeline
reg = 0.01
pipeline = Pipeline(steps=[('preprocessor', preprocessor),
                           ('logregressor', LogisticRegression(C=1/reg, solver="liblinear"))])


# fit the pipeline to train a logistic regression model on the training set
model = pipeline.fit(X_train, (y_train))
print (model)

def evaluate():
  predictions = model.predict(X_test)
  y_scores = model.predict_proba(X_test)
  cm = confusion_matrix(y_test, predictions)
  print ('Confusion Matrix:\n',cm, '\n')
  print('Accuracy:', accuracy_score(y_test, predictions))
  print("Overall Precision:",precision_score(y_test, predictions))
  print("Overall Recall:",recall_score(y_test, predictions))
  auc = roc_auc_score(y_test,y_scores[:,1])
  print('\nAUC: ' + str(auc))

  # calculate ROC curve
  fpr, tpr, thresholds = roc_curve(y_test, y_scores[:,1])

  # plot ROC curve
  fig = plt.figure(figsize=(6, 6))
  # Plot the diagonal 50% line
  plt.plot([0, 1], [0, 1], 'k--')
  # Plot the FPR and TPR achieved by our model
  plt.plot(fpr, tpr)
  plt.xlabel('False Positive Rate')
  plt.ylabel('True Positive Rate')
  plt.title('ROC Curve')
  plt.show()

evaluate()

from sklearn.ensemble import RandomForestClassifier

# Create preprocessing and training pipeline
pipeline = Pipeline(steps=[('preprocessor', preprocessor),
                           ('logregressor', RandomForestClassifier(n_estimators=100))])

# fit the pipeline to train a random forest model on the training set
model = pipeline.fit(X_train, (y_train))
print (model)

evaluate()