KNN in Practice - Classification

 Problem statement :

Finding whether a patient has Cancer or not ?? and classifying a new patient based on her reports(features)


import numpy as np

import pandas as pd

 importing data :

from sklearn.datasets import load_breast_cancer

data=load_breast_cancer()

data.data

data.feature_names

array([' malignant', 'benign'])

In this we have, two categories: 'Malignant' the one who has cancer and 'Benign' means no cancer.

data.target

data.target_names

 Create a dataframe:

df=pd.DataFrame(np.c_[data.data,data.target],columns=[list(data.feature_names)+['target']])

df.head()

df.shape

(569, 31)

Split the data:

x=df.iloc[:,0:-1]

y=df.iloc[:,-1]

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,random_state=50)

print('shape of x_train:',x_train.shape)

print('shape of y_train:',y_train.shape)

print('shape of x_test:',x_test.shape)

print('shape of y_test:',y_test.shape)

shape of x_train: (455, 30)
shape of y_train: (455,)
shape of x_test: (114, 30)
shape of y_test: (114,)

Implementing KNN Classifier :

from sklearn.neighbors import KNeighborsClassifier

classifier=KNeighborsClassifier(n_neighbors=5)

classifier.fit(x_train,y_train)

Accuracy:

classifier.score(x_test,y_test)

0.91228070175

Here we have a new patient test reports, by this we need to classify the patient whether she has cancer or not i.e is she belongs to 'Malignant' , or 'Benign'. 

patient1 = [17.99,
 10.38,
 122.8,
 1001.0,
 0.1184,
 0.2776,
 0.3001,
 0.1471,
 0.2419,
 0.07871,
 1.095,
 0.9053,
 8.589,
 153.4,
 0.006399,
 0.04904,
 0.05373,
 0.01587,
 0.03003,
 0.006193,
 25.38,
 17.33,
 184.6,
 2019.0,
 0.1622,
 0.6656,
 0.7119,
 0.2654,
 0.4601,
 0.1189]

patient1=np.array([patient1]) patient1

classifier.predict(patient1)

pred=classifier.predict(patient1) if pred[0]==0: print("patient has cancer (malignant tumor)") else: print("patient has no cancer (malignant benign)")

patient has cancer (malignant tumor)

Comments