# Import Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Import Dataset
dataset = pd.read_csv('https://archive.ics.uci.edu/ml/'
            'machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data', header=None)
header = ['Sample_code_number',
         'Clump_Thickness',
         'Uniformity_of_Cell_Size',
         'Uniformity_of_Cell_Shape',
         'Marginal_Adhesion',
         'Single_Epithelial_Cell_Size',
         'Bare_Nuclei',
         'Bland_Chromatin',
         'Normal_Nucleoli',
         'Mitoses',
         'Class']
dataset.columns = header
# There is few empty or garbage character in the dataset
dataset['Bare_Nuclei'] = dataset['Bare_Nuclei'].replace(r'\?', np.nan, regex=True)
X = dataset.iloc[:,1:-1].values
y = dataset.iloc[:,-1].values
dataset.head(n=5)
"""
This is breast cancer dataset, where we will predict the tumor is benign or malignant (2 for benign, 4 for malignant).
"""
# Dataset sanity check
dataset.info()
# Check Missing value
print("Number of Missing value")
for col in dataset.columns:
    print('\t%s: %d' %(col, dataset[col].isna().sum()))
"""
This dataset is having 11 features and 699 observations. All the features are scale between 1 to 10. So no need of feature scaling. There is missing value in Bare_Nuclei
"""
# Handling Missing value
from sklearn.impute import SimpleImputer
SI = SimpleImputer(strategy='mean')
SI.fit(X[:,5:6])
X[:,5:6] = SI.transform(X[:,5:6])
# Split the dataset into train and test
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2)
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
# Apply logistic regression on train dataset
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state=0)
classifier.fit(X_train,y_train)
# Apply logistic regression on test dataset
y_pred = classifier.predict(X_test)
np.concatenate((y_test.reshape(len(y_test),1), y_pred.reshape(len(y_pred),1)),1)
# Evaluate the model performance
from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test,y_pred)
print(cm)
print(f'Accuracy score is {accuracy_score(y_test,y_pred)}')
"""
Let test with unknown data
Clump Thickness = 5
Uniformity of Cell Size = 8 
Uniformity of Cell Shape = 5
Marginal Adhesion =  4
Single Epithelial Cell Size = 3  
Bare Nuclei =  4
Bland Chromatin = 2
Normal Nucleoli = 4
Mitoses = 1
"""
# Check the new result
classifier.predict([[5,8,5,4,3,4,2,4,1]])