import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from sklearn.metrics import accuracy_score
from sklearn.metrics import f1_score,confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
sns.set()
%matplotlib inline
#import data (2017 Wisconsin Breast Cancer Diagnostic dataset from UCI machine learning repository)
data = pd.read_csv(r'/Users/shivambadkas/Downloads/data.csv')
data.head()
id | diagnosis | radius_mean | texture_mean | perimeter_mean | area_mean | smoothness_mean | compactness_mean | concavity_mean | concave points_mean | ... | texture_worst | perimeter_worst | area_worst | smoothness_worst | compactness_worst | concavity_worst | concave points_worst | symmetry_worst | fractal_dimension_worst | Unnamed: 32 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 842302 | M | 17.99 | 10.38 | 122.80 | 1001.0 | 0.11840 | 0.27760 | 0.3001 | 0.14710 | ... | 17.33 | 184.60 | 2019.0 | 0.1622 | 0.6656 | 0.7119 | 0.2654 | 0.4601 | 0.11890 | NaN |
1 | 842517 | M | 20.57 | 17.77 | 132.90 | 1326.0 | 0.08474 | 0.07864 | 0.0869 | 0.07017 | ... | 23.41 | 158.80 | 1956.0 | 0.1238 | 0.1866 | 0.2416 | 0.1860 | 0.2750 | 0.08902 | NaN |
2 | 84300903 | M | 19.69 | 21.25 | 130.00 | 1203.0 | 0.10960 | 0.15990 | 0.1974 | 0.12790 | ... | 25.53 | 152.50 | 1709.0 | 0.1444 | 0.4245 | 0.4504 | 0.2430 | 0.3613 | 0.08758 | NaN |
3 | 84348301 | M | 11.42 | 20.38 | 77.58 | 386.1 | 0.14250 | 0.28390 | 0.2414 | 0.10520 | ... | 26.50 | 98.87 | 567.7 | 0.2098 | 0.8663 | 0.6869 | 0.2575 | 0.6638 | 0.17300 | NaN |
4 | 84358402 | M | 20.29 | 14.34 | 135.10 | 1297.0 | 0.10030 | 0.13280 | 0.1980 | 0.10430 | ... | 16.67 | 152.20 | 1575.0 | 0.1374 | 0.2050 | 0.4000 | 0.1625 | 0.2364 | 0.07678 | NaN |
5 rows × 33 columns
col = data.columns
print(col)
Index(['id', 'diagnosis', 'radius_mean', 'texture_mean', 'perimeter_mean', 'area_mean', 'smoothness_mean', 'compactness_mean', 'concavity_mean', 'concave points_mean', 'symmetry_mean', 'fractal_dimension_mean', 'radius_se', 'texture_se', 'perimeter_se', 'area_se', 'smoothness_se', 'compactness_se', 'concavity_se', 'concave points_se', 'symmetry_se', 'fractal_dimension_se', 'radius_worst', 'texture_worst', 'perimeter_worst', 'area_worst', 'smoothness_worst', 'compactness_worst', 'concavity_worst', 'concave points_worst', 'symmetry_worst', 'fractal_dimension_worst', 'Unnamed: 32'], dtype='object')
#removing unnecesary features
y = data.diagnosis
list = ['Unnamed: 32','id','diagnosis']
x = data.drop(list,axis = 1 )
#identifying classifier, M is malignant and B is benign
ax = sns.countplot(y, label = 'Count')
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/seaborn/_decorators.py:36: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation. warnings.warn(
#checking correlation between features
f,ax = plt.subplots(figsize=(18, 18))
sns.heatmap(x.corr(), annot=True, linewidths=.5, fmt= '.1f',ax=ax)
<AxesSubplot:>
# feature selection, identifying which features are correlated and dropping extra ones
droplist1 = ['perimeter_mean','radius_mean','compactness_mean','concave points_mean','radius_se','perimeter_se','radius_worst','perimeter_worst','compactness_worst','concave points_worst','compactness_se','concave points_se','texture_worst','area_worst']
x1 = x.drop(droplist1,axis = 1 )
x1.head()
texture_mean | area_mean | smoothness_mean | concavity_mean | symmetry_mean | fractal_dimension_mean | texture_se | area_se | smoothness_se | concavity_se | symmetry_se | fractal_dimension_se | smoothness_worst | concavity_worst | symmetry_worst | fractal_dimension_worst | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 10.38 | 1001.0 | 0.11840 | 0.3001 | 0.2419 | 0.07871 | 0.9053 | 153.40 | 0.006399 | 0.05373 | 0.03003 | 0.006193 | 0.1622 | 0.7119 | 0.4601 | 0.11890 |
1 | 17.77 | 1326.0 | 0.08474 | 0.0869 | 0.1812 | 0.05667 | 0.7339 | 74.08 | 0.005225 | 0.01860 | 0.01389 | 0.003532 | 0.1238 | 0.2416 | 0.2750 | 0.08902 |
2 | 21.25 | 1203.0 | 0.10960 | 0.1974 | 0.2069 | 0.05999 | 0.7869 | 94.03 | 0.006150 | 0.03832 | 0.02250 | 0.004571 | 0.1444 | 0.4504 | 0.3613 | 0.08758 |
3 | 20.38 | 386.1 | 0.14250 | 0.2414 | 0.2597 | 0.09744 | 1.1560 | 27.23 | 0.009110 | 0.05661 | 0.05963 | 0.009208 | 0.2098 | 0.6869 | 0.6638 | 0.17300 |
4 | 14.34 | 1297.0 | 0.10030 | 0.1980 | 0.1809 | 0.05883 | 0.7813 | 94.44 | 0.011490 | 0.05688 | 0.01756 | 0.005115 | 0.1374 | 0.4000 | 0.2364 | 0.07678 |
f,ax = plt.subplots(figsize=(15, 15))
sns.heatmap(x1.corr(), annot=True, linewidths=.5, fmt= '.1f',ax=ax)
<AxesSubplot:>
# split data 80/20
x_train, x_test, y_train, y_test = train_test_split(x1, y, test_size=0.3, random_state=42)
#use random forest and find accuracy
clf_rf = RandomForestClassifier(random_state=43)
clr_rf = clf_rf.fit(x_train,y_train)
ac = accuracy_score(y_test,clf_rf.predict(x_test))
print('Accuracy: ',ac)
cm = confusion_matrix(y_test,clf_rf.predict(x_test))
sns.heatmap(cm,annot=True,fmt="d")
Accuracy: 0.9649122807017544
<AxesSubplot:>
#SelectKBest feature selection
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
select = SelectKBest(chi2, k=5).fit(x_train, y_train)
print('Score list:', select.scores_)
print('Feature list:', x_train.columns)
Score list: [6.06916433e+01 3.66899557e+04 1.00015175e-01 1.30547650e+01 1.95982847e-01 3.42575072e-04 4.07131026e-02 6.12741067e+03 1.32470372e-03 6.92896719e-01 1.39557806e-03 2.65927071e-03 2.63226314e-01 2.58858117e+01 1.00635138e+00 1.23087347e-01] Feature list: Index(['texture_mean', 'area_mean', 'smoothness_mean', 'concavity_mean', 'symmetry_mean', 'fractal_dimension_mean', 'texture_se', 'area_se', 'smoothness_se', 'concavity_se', 'symmetry_se', 'fractal_dimension_se', 'smoothness_worst', 'concavity_worst', 'symmetry_worst', 'fractal_dimension_worst'], dtype='object')
#best 5 features according to to SelectKBest are texture_mean, area_mean,smoothness_mean, concavity_mean,symmetry_mean
x_train_2 = select.transform(x_train)
x_test_2 = select.transform(x_test)
#random forest classifier with n_estimators=10 (default)
clf_rf_2 = RandomForestClassifier()
clr_rf_2 = clf_rf_2.fit(x_train_2,y_train)
ac_2 = accuracy_score(y_test,clf_rf_2.predict(x_test_2))
print('Accuracy is: ',ac_2)
cm_2 = confusion_matrix(y_test,clf_rf_2.predict(x_test_2))
sns.heatmap(cm_2,annot=True,fmt="d")
Accuracy is: 0.9766081871345029
<AxesSubplot:>
#Recursive Feature Elimination (RFE) uses random forest to assign weights to features and then prune small weights
#recursively until desired number of features is listed
from sklearn.feature_selection import RFE
clf_rf_3 = RandomForestClassifier()
rfe = RFE(estimator=clf_rf_3, n_features_to_select=5, step=1)
rfe = rfe.fit(x_train, y_train)
#yields same features as SelectKBest
#RFE with cross validation and random forest classification
from sklearn.feature_selection import RFECV
clf_rf_4 = RandomForestClassifier()
rfecv = RFECV(estimator = clf_rf_4, step = 1, cv = 5, scoring = 'accuracy')
rfecv = rfecv.fit(x_train, y_train)
#show how many features are optimal for model accuracy and which ones they are
print('Optimal number of features :', rfecv.n_features_)
print('Best features :', x_train.columns[rfecv.support_])
Optimal number of features : 13 Best features : Index(['texture_mean', 'area_mean', 'smoothness_mean', 'concavity_mean', 'fractal_dimension_mean', 'area_se', 'smoothness_se', 'concavity_se', 'fractal_dimension_se', 'smoothness_worst', 'concavity_worst', 'symmetry_worst', 'fractal_dimension_worst'], dtype='object')
#Use PCA for feature extraction
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42)
#normalize
x_train_N = (x_train-x_train.mean())/(x_train.max()-x_train.min())
x_test_N = (x_test-x_test.mean())/(x_test.max()-x_test.min())
from sklearn.decomposition import PCA
pca = PCA()
pca.fit(x_train_N)
#plot
plt.figure(1, figsize=(14, 13))
plt.clf()
plt.axes([.2, .2, .7, .7])
plt.plot(pca.explained_variance_ratio_, linewidth=2)
plt.axis('tight')
plt.xlabel('n_components')
plt.ylabel('explained_variance_ratio_')
Text(0, 0.5, 'explained_variance_ratio_')