22
22
import numpy as np
import os
from glob import glob
import random
import matplotlib.pylab as plt
../input/breast-histopathology-images
mypaths=[]
for name in glob('../input/breast-histopathology-
images/IDC_regular_ps50_idx5/*',recursive=True):
mypaths.append(name)
print(mypaths[:5])
['../input/breast-histopathology-images/IDC_regular_ps50_idx5/10295',
'../input/breast-histopathology-images/IDC_regular_ps50_idx5/10304',
'../input/breast-histopathology-images/IDC_regular_ps50_idx5/12868',
'../input/breast-histopathology-images/IDC_regular_ps50_idx5/10274',
'../input/breast-histopathology-images/IDC_regular_ps50_idx5/12818']
len(mypaths)
279
mypaths[:5]
['../input/breast-histopathology-images/IDC_regular_ps50_idx5/10295',
'../input/breast-histopathology-images/IDC_regular_ps50_idx5/10304',
'../input/breast-histopathology-images/IDC_regular_ps50_idx5/12868',
'../input/breast-histopathology-images/IDC_regular_ps50_idx5/10274',
'../input/breast-histopathology-images/IDC_regular_ps50_idx5/12818']
mp=mypaths[60:100]
imagePatches=[]
for i in mp:
imagePatches+=glob(i+'/*/*.png', recursive=True)
len(imagePatches)
41604
# Two arrays holding images by class type
class0 = [] # 0 = no cancer
class1 = [] # 1 = cancer
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
y_pred=model.predict(X_test)
Y_pred=[]
for i in y_pred:
if i[0]>i[1]:
Y_pred.append(0)
else:
Y_pred.append(1)
Y_test=[]
for i in y_test:
if i[0]>i[1]:
Y_test.append(0)
else:
Y_test.append(1)
from sklearn.metrics import classification_report, confusion_matrix
print('Confusion Matrix')
print(confusion_matrix(Y_test, Y_pred))
print('Classification Report')
print(classification_report(Y_test, Y_pred, target_names=['Negative','Positive']))
Confusion Matrix
[[4928 0]
[2432 0]]
Classification Report
precision recall f1-score support
/opt/conda/lib/python3.7/site-packages/sklearn/metrics/_classification.py:1245:
UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0
in labels with no predicted samples. Use `zero_division` parameter to control this
behavior.
_warn_prf(average, modifier, msg_start, len(result))
/opt/conda/lib/python3.7/site-packages/sklearn/metrics/_classification.py:1245:
UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0
in labels with no predicted samples. Use `zero_division` parameter to control this
behavior.
_warn_prf(average, modifier, msg_start, len(result))
/opt/conda/lib/python3.7/site-packages/sklearn/metrics/_classification.py:1245:
UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0
in labels with no predicted samples. Use `zero_division` parameter to control this
behavior.
_warn_prf(average, modifier, msg_start, len(result))
confusionmatrix=confusion_matrix(Y_test, Y_pred)
classes=[0,1]
con_mat_df = pd.DataFrame(confusion_matrix(Y_test, Y_pred),
index = classes,
columns = classes)
con_mat_df
0 1
0 4928 0
1 2432 0
import seaborn as sns
figure = plt.figure(figsize=(6, 6))
sns.heatmap(con_mat_df, annot=True,cmap=plt.cm.cool,fmt='d')
plt.tight_layout()
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show()
l=[[1501,88],[125,176]]
cm=np.array(l)
cm
array([[1501, 88],
[ 125, 176]])
classes=[0,1]
con_mat_df = pd.DataFrame(cm,
index = classes,
columns = classes)
import seaborn as sns
figure = plt.figure(figsize=(6, 6))
sns.heatmap(con_mat_df, annot=True,cmap=plt.cm.cool,fmt='d')
plt.tight_layout()
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show()