# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# Import the libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# %matplotlib inline

#import sklearn package details
from sklearn.model_selection import train_test_split   #GridSearchCV is imported later from sklearn.model_selection
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.metrics import classification_report, confusion_matrix

#load the dataset for Breast Cancer Detection
# using the UCI repository data
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data"


df = pd.read_csv(url)

df.head()

# giving the names of the cols as the data has no col. names. There are 11 cols excluding the ID
names = ['id','clump_thickness','uniform_cell_size','uniform_cell_shape',
        'marginal_adhesion','single_epithelial_size','bare_nuclei',
        'bland_chromatin','normal_nucleoli','mitoses','class']

df = pd.read_csv(url, names=names)
df.head()

#performing data exploration to understand and preprocess the data
    #using a heat map to see if there are blanks in the dataset
    #sns.heatmap(df.isnull(), yticklabels = False, cbar = False, cmap = 'viridis') -- df.info() also shows there are no nulls
df.info()

#barenuclei col has some items that are ?. Need to be replaced
df.isin(['?']).any()
df[df.eq('?').any(1)] #.eq is same as == operator

# replacing ? with -99999
df.replace('?',-99999, inplace = True)
df.isin(['?']).any()

#print the shape of the dataset
print(df.shape)

#lets drop the id col. as ML wont be needing this
df.drop(['id'],axis = 1, inplace = True)
df.head()

print(df.shape)

###PERFORMING DATA VISUALIZATION
print(df.iloc[0]) #shows first row of the dataset. Class value = 2 Benign, 4=Melignant
print(df.describe()) # if I show include ='all' then it will also include bare_nuclei col as that has NaN values
#Good part is all the features are standardized between 1 and 10. So, I can directly use KNN without using StandardScaler

#Plotting histogram for each variable or col
df.hist(figsize =(10,10))
plt.show()

# +
#sns.pairplot(df)
#scatter_matrix(df,figsize = (18,18))

temp = df.drop('bare_nuclei',axis = 1) #pairplot was errorring for bare_nuclei as it has imputed values of -99999
sns.pairplot(temp)
#data does not seem to be having any standout relationship between the features to classify 
#the cell class to be melignant or benign

# +
#Implementing the ML models

X = df.drop('class',axis =1)
y = df['class']
# -

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=8)

#IMPLEMENTING KNN
knn = KNeighborsClassifier(n_neighbors =1)
knn.fit(X_train, y_train)
pred = knn.predict(X_test)
print (classification_report (y_test, pred))

#Using elbow method to predict k value for better results
error_rate=[]
for i in range(1,40):
    knn = KNeighborsClassifier(n_neighbors =i)
    knn.fit(X_train, y_train)
    pred_i = knn.predict(X_test)
    error_rate.append(np.mean(pred_i != y_test))

plt.figure(figsize=(10,8))
plt.plot(range(1,40), error_rate, color = 'blue',linestyle ='--',marker='o')
plt.title('Error Rate vs. K Value')
plt.xlabel('K')
plt.ylabel('Error Rate')

#From above seems like 11 is a good value for K. Using K=11 and checking for the results -> f1 score improved from 95 to 97
knn = KNeighborsClassifier(n_neighbors =11)
knn.fit(X_train, y_train)
pred = knn.predict(X_test)
print (classification_report (y_test, pred))

#IMPLEMENTING SVC
model = SVC()
model.fit(X_train, y_train)
pred_svc = model.predict(X_test)
print (confusion_matrix (y_test, pred_svc))
print ('\n')
print (classification_report (y_test, pred_svc))

#KNN has better results vs SVC. Lets try to tune C and gamma values and see if performance improves. 
#Precision has a much lower score
from sklearn.model_selection import GridSearchCV
param_grid = {'C':[0.1,1,10,100,1000], 'gamma':[1,0.1,0.01,0.001,0.0001]}
grid = GridSearchCV(SVC(),param_grid,verbose =3)
grid.fit(X_train, y_train)

grid.best_params_

grid.best_estimator_

grid_predictions = grid.predict(X_test)
print (confusion_matrix (y_test, grid_predictions))
print ('\n')
print (classification_report (y_test, grid_predictions))

# +
##Now we see SVM is very close to KNN

StarcoderdataJupyterScriptsDedupFiltered