Python Breast Cancer Prediction using Random Forest














































Python Breast Cancer Prediction using Random Forest



PYTHON BREAST CANCER PREDICTION 

                        USING RANDOM FOREST

Python Breast Cancer prediction is a simple project in python which is used to classify
whether the person is having breast cancer or not i.e. by classifying the given data into
malignant or benign. This project is built using Random Forest classifier i.e. the model is
trained using Random Forest and achieved an accuracy of 97%. If the output is malignant
then the person is suffering from breast cancer, if not then the person does not have breast
cancer.

CODE

In [88]:
import pandas as pd
In [89]:
import numpy as np
In [90]:
import matplotlib.pyplot as plt
In [91]:
import seaborn as sns
In [92]:
from sklearn.datasets import load_breast_cancer
In [93]:
cancer=load_breast_cancer()
cancer
Out[93]:
{'DESCR': 'Breast Cancer Wisconsin (Diagnostic) Database\n=============================================\n\nNotes\n-----\nData Set Characteristics:\n    :Number of Instances: 569\n\n    :Number of Attributes: 30 numeric, predictive attributes and the class\n\n    :Attribute Information:\n        - radius (mean of distances from center to points on the perimeter)\n        - texture (standard deviation of gray-scale values)\n        - perimeter\n        - area\n        - smoothness (local variation in radius lengths)\n        - compactness (perimeter^2 / area - 1.0)\n        - concavity (severity of concave portions of the contour)\n        - concave points (number of concave portions of the contour)\n        - symmetry \n        - fractal dimension ("coastline approximation" - 1)\n\n        The mean, standard error, and "worst" or largest (mean of the three\n        largest values) of these features were computed for each image,\n        resulting in 30 features.  For instance, field 3 is Mean Radius, field\n        13 is Radius SE, field 23 is Worst Radius.\n\n        - class:\n                - WDBC-Malignant\n                - WDBC-Benign\n\n    :Summary Statistics:\n\n    ===================================== ====== ======\n                                           Min    Max\n    ===================================== ====== ======\n    radius (mean):                        6.981  28.11\n    texture (mean):                       9.71   39.28\n    perimeter (mean):                     43.79  188.5\n    area (mean):                          143.5  2501.0\n    smoothness (mean):                    0.053  0.163\n    compactness (mean):                   0.019  0.345\n    concavity (mean):                     0.0    0.427\n    concave points (mean):                0.0    0.201\n    symmetry (mean):                      0.106  0.304\n    fractal dimension (mean):             0.05   0.097\n    radius (standard error):              0.112  2.873\n    texture (standard error):             0.36   4.885\n    perimeter (standard error):           0.757  21.98\n    area (standard error):                6.802  542.2\n    smoothness (standard error):          0.002  0.031\n    compactness (standard error):         0.002  0.135\n    concavity (standard error):           0.0    0.396\n    concave points (standard error):      0.0    0.053\n    symmetry (standard error):            0.008  0.079\n    fractal dimension (standard error):   0.001  0.03\n    radius (worst):                       7.93   36.04\n    texture (worst):                      12.02  49.54\n    perimeter (worst):                    50.41  251.2\n    area (worst):                         185.2  4254.0\n    smoothness (worst):                   0.071  0.223\n    compactness (worst):                  0.027  1.058\n    concavity (worst):                    0.0    1.252\n    concave points (worst):               0.0    0.291\n    symmetry (worst):                     0.156  0.664\n    fractal dimension (worst):            0.055  0.208\n    ===================================== ====== ======\n\n    :Missing Attribute Values: None\n\n    :Class Distribution: 212 - Malignant, 357 - Benign\n\n    :Creator:  Dr. William H. Wolberg, W. Nick Street, Olvi L. Mangasarian\n\n    :Donor: Nick Street\n\n    :Date: November, 1995\n\nThis is a copy of UCI ML Breast Cancer Wisconsin (Diagnostic) datasets.\nhttps://goo.gl/U2Uwz2\n\nFeatures are computed from a digitized image of a fine needle\naspirate (FNA) of a breast mass.  They describe\ncharacteristics of the cell nuclei present in the image.\n\nSeparating plane described above was obtained using\nMultisurface Method-Tree (MSM-T) [K. P. Bennett, "Decision Tree\nConstruction Via Linear Programming." Proceedings of the 4th\nMidwest Artificial Intelligence and Cognitive Science Society,\npp. 97-101, 1992], a classification method which uses linear\nprogramming to construct a decision tree.  Relevant features\nwere selected using an exhaustive search in the space of 1-4\nfeatures and 1-3 separating planes.\n\nThe actual linear program used to obtain the separating plane\nin the 3-dimensional space is that described in:\n[K. P. Bennett and O. L. Mangasarian: "Robust Linear\nProgramming Discrimination of Two Linearly Inseparable Sets",\nOptimization Methods and Software 1, 1992, 23-34].\n\nThis database is also available through the UW CS ftp server:\n\nftp ftp.cs.wisc.edu\ncd math-prog/cpo-dataset/machine-learn/WDBC/\n\nReferences\n----------\n   - W.N. Street, W.H. Wolberg and O.L. Mangasarian. Nuclear feature extraction \n     for breast tumor diagnosis. IS&T/SPIE 1993 International Symposium on \n     Electronic Imaging: Science and Technology, volume 1905, pages 861-870,\n     San Jose, CA, 1993.\n   - O.L. Mangasarian, W.N. Street and W.H. Wolberg. Breast cancer diagnosis and \n     prognosis via linear programming. Operations Research, 43(4), pages 570-577, \n     July-August 1995.\n   - W.H. Wolberg, W.N. Street, and O.L. Mangasarian. Machine learning techniques\n     to diagnose breast cancer from fine-needle aspirates. Cancer Letters 77 (1994) \n     163-171.\n',
 'data': array([[  1.79900000e+01,   1.03800000e+01,   1.22800000e+02, ...,
           2.65400000e-01,   4.60100000e-01,   1.18900000e-01],
        [  2.05700000e+01,   1.77700000e+01,   1.32900000e+02, ...,
           1.86000000e-01,   2.75000000e-01,   8.90200000e-02],
        [  1.96900000e+01,   2.12500000e+01,   1.30000000e+02, ...,
           2.43000000e-01,   3.61300000e-01,   8.75800000e-02],
        ..., 
        [  1.66000000e+01,   2.80800000e+01,   1.08300000e+02, ...,
           1.41800000e-01,   2.21800000e-01,   7.82000000e-02],
        [  2.06000000e+01,   2.93300000e+01,   1.40100000e+02, ...,
           2.65000000e-01,   4.08700000e-01,   1.24000000e-01],
        [  7.76000000e+00,   2.45400000e+01,   4.79200000e+01, ...,
           0.00000000e+00,   2.87100000e-01,   7.03900000e-02]]),
 'feature_names': array(['mean radius', 'mean texture', 'mean perimeter', 'mean area',
        'mean smoothness', 'mean compactness', 'mean concavity',
        'mean concave points', 'mean symmetry', 'mean fractal dimension',
        'radius error', 'texture error', 'perimeter error', 'area error',
        'smoothness error', 'compactness error', 'concavity error',
        'concave points error', 'symmetry error', 'fractal dimension error',
        'worst radius', 'worst texture', 'worst perimeter', 'worst area',
        'worst smoothness', 'worst compactness', 'worst concavity',
        'worst concave points', 'worst symmetry', 'worst fractal dimension'], 
       dtype='<U23'),
 'target': array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
        1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1,
        1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0,
        1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1,
        1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1,
        0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
        0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1,
        0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1,
        0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
        0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1,
        1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
        1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,
        1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1,
        1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1,
        0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,
        1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
        0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1,
        1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
        0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,
        1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1,
        1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1]),
 'target_names': array(['malignant', 'benign'], 
       dtype='<U9')}
In [94]:
cancer.keys()
Out[94]:
dict_keys(['data', 'target', 'target_names', 'DESCR', 'feature_names'])

DATASET DESCRIPTION

In [95]:
print(cancer["DESCR"])
#Data set description
Breast Cancer Wisconsin (Diagnostic) Database
=============================================

Notes
-----
Data Set Characteristics:
    :Number of Instances: 569

    :Number of Attributes: 30 numeric, predictive attributes and the class

    :Attribute Information:
        - radius (mean of distances from center to points on the perimeter)
        - texture (standard deviation of gray-scale values)
        - perimeter
        - area
        - smoothness (local variation in radius lengths)
        - compactness (perimeter^2 / area - 1.0)
        - concavity (severity of concave portions of the contour)
        - concave points (number of concave portions of the contour)
        - symmetry 
        - fractal dimension ("coastline approximation" - 1)

        The mean, standard error, and "worst" or largest (mean of the three
        largest values) of these features were computed for each image,
        resulting in 30 features.  For instance, field 3 is Mean Radius, field
        13 is Radius SE, field 23 is Worst Radius.

        - class:
                - WDBC-Malignant
                - WDBC-Benign

    :Summary Statistics:

    ===================================== ====== ======
                                           Min    Max
    ===================================== ====== ======
    radius (mean):                        6.981  28.11
    texture (mean):                       9.71   39.28
    perimeter (mean):                     43.79  188.5
    area (mean):                          143.5  2501.0
    smoothness (mean):                    0.053  0.163
    compactness (mean):                   0.019  0.345
    concavity (mean):                     0.0    0.427
    concave points (mean):                0.0    0.201
    symmetry (mean):                      0.106  0.304
    fractal dimension (mean):             0.05   0.097
    radius (standard error):              0.112  2.873
    texture (standard error):             0.36   4.885
    perimeter (standard error):           0.757  21.98
    area (standard error):                6.802  542.2
    smoothness (standard error):          0.002  0.031
    compactness (standard error):         0.002  0.135
    concavity (standard error):           0.0    0.396
    concave points (standard error):      0.0    0.053
    symmetry (standard error):            0.008  0.079
    fractal dimension (standard error):   0.001  0.03
    radius (worst):                       7.93   36.04
    texture (worst):                      12.02  49.54
    perimeter (worst):                    50.41  251.2
    area (worst):                         185.2  4254.0
    smoothness (worst):                   0.071  0.223
    compactness (worst):                  0.027  1.058
    concavity (worst):                    0.0    1.252
    concave points (worst):               0.0    0.291
    symmetry (worst):                     0.156  0.664
    fractal dimension (worst):            0.055  0.208
    ===================================== ====== ======

    :Missing Attribute Values: None

    :Class Distribution: 212 - Malignant, 357 - Benign

    :Creator:  Dr. William H. Wolberg, W. Nick Street, Olvi L. Mangasarian

    :Donor: Nick Street

    :Date: November, 1995

This is a copy of UCI ML Breast Cancer Wisconsin (Diagnostic) datasets.
https://goo.gl/U2Uwz2

Features are computed from a digitized image of a fine needle
aspirate (FNA) of a breast mass.  They describe
characteristics of the cell nuclei present in the image.

Separating plane described above was obtained using
Multisurface Method-Tree (MSM-T) [K. P. Bennett, "Decision Tree
Construction Via Linear Programming." Proceedings of the 4th
Midwest Artificial Intelligence and Cognitive Science Society,
pp. 97-101, 1992], a classification method which uses linear
programming to construct a decision tree.  Relevant features
were selected using an exhaustive search in the space of 1-4
features and 1-3 separating planes.

The actual linear program used to obtain the separating plane
in the 3-dimensional space is that described in:
[K. P. Bennett and O. L. Mangasarian: "Robust Linear
Programming Discrimination of Two Linearly Inseparable Sets",
Optimization Methods and Software 1, 1992, 23-34].

This database is also available through the UW CS ftp server:

ftp ftp.cs.wisc.edu
cd math-prog/cpo-dataset/machine-learn/WDBC/

References
----------
   - W.N. Street, W.H. Wolberg and O.L. Mangasarian. Nuclear feature extraction 
     for breast tumor diagnosis. IS&T/SPIE 1993 International Symposium on 
     Electronic Imaging: Science and Technology, volume 1905, pages 861-870,
     San Jose, CA, 1993.
   - O.L. Mangasarian, W.N. Street and W.H. Wolberg. Breast cancer diagnosis and 
     prognosis via linear programming. Operations Research, 43(4), pages 570-577, 
     July-August 1995.
   - W.H. Wolberg, W.N. Street, and O.L. Mangasarian. Machine learning techniques
     to diagnose breast cancer from fine-needle aspirates. Cancer Letters 77 (1994) 
     163-171.

In [96]:
print(cancer["target"])
#target values
#0 indicates malignant
#1 indicates benign
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 1 0 0 0 0 0 0 0 0 1 0 1 1 1 1 1 0 0 1 0 0 1 1 1 1 0 1 0 0 1 1 1 1 0 1 0 0
 1 0 1 0 0 1 1 1 0 0 1 0 0 0 1 1 1 0 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1 1 0 1 1
 1 1 1 1 1 1 0 0 0 1 0 0 1 1 1 0 0 1 0 1 0 0 1 0 0 1 1 0 1 1 0 1 1 1 1 0 1
 1 1 1 1 1 1 1 1 0 1 1 1 1 0 0 1 0 1 1 0 0 1 1 0 0 1 1 1 1 0 1 1 0 0 0 1 0
 1 0 1 1 1 0 1 1 0 0 1 0 0 0 0 1 0 0 0 1 0 1 0 1 1 0 1 0 0 0 0 1 1 0 0 1 1
 1 0 1 1 1 1 1 0 0 1 1 0 1 1 0 0 1 0 1 1 1 1 0 1 1 1 1 1 0 1 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 1 1 1 1 1 1 0 1 0 1 1 0 1 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1
 1 0 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 1 1
 1 1 0 1 0 1 0 1 1 1 0 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0 0
 0 1 0 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1
 1 0 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 0 1 1 1 1 1 0 1 1
 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1
 1 1 1 1 1 1 0 1 0 1 1 0 1 1 1 1 1 0 0 1 0 1 0 1 1 1 1 1 0 1 1 0 1 0 1 0 0
 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 0 0 0 0 0 0 1]
In [97]:
cancer["target_names"]
#target names
Out[97]:
array(['malignant', 'benign'], 
      dtype='<U9')
In [98]:
cancer["feature_names"]
#feature names
Out[98]:
array(['mean radius', 'mean texture', 'mean perimeter', 'mean area',
       'mean smoothness', 'mean compactness', 'mean concavity',
       'mean concave points', 'mean symmetry', 'mean fractal dimension',
       'radius error', 'texture error', 'perimeter error', 'area error',
       'smoothness error', 'compactness error', 'concavity error',
       'concave points error', 'symmetry error', 'fractal dimension error',
       'worst radius', 'worst texture', 'worst perimeter', 'worst area',
       'worst smoothness', 'worst compactness', 'worst concavity',
       'worst concave points', 'worst symmetry', 'worst fractal dimension'], 
      dtype='<U23')
In [99]:
cancer["data"].shape
#data shape i.e 569 rows and 30 columns
Out[99]:
(569, 30)
In [100]:
df_cancer=pd.DataFrame(np.c_[cancer["data"],cancer["target"]],columns=np.append(cancer["feature_names"],["target"]))
df_cancer
#data set after forming it into dataframe
Out[100]:
mean radiusmean texturemean perimetermean areamean smoothnessmean compactnessmean concavitymean concave pointsmean symmetrymean fractal dimension...worst textureworst perimeterworst areaworst smoothnessworst compactnessworst concavityworst concave pointsworst symmetryworst fractal dimensiontarget
017.99010.38122.801001.00.118400.277600.3001000.1471000.24190.07871...17.33184.602019.00.162200.665600.711900.265400.46010.118900.0
120.57017.77132.901326.00.084740.078640.0869000.0701700.18120.05667...23.41158.801956.00.123800.186600.241600.186000.27500.089020.0
219.69021.25130.001203.00.109600.159900.1974000.1279000.20690.05999...25.53152.501709.00.144400.424500.450400.243000.36130.087580.0
311.42020.3877.58386.10.142500.283900.2414000.1052000.25970.09744...26.5098.87567.70.209800.866300.686900.257500.66380.173000.0
420.29014.34135.101297.00.100300.132800.1980000.1043000.18090.05883...16.67152.201575.00.137400.205000.400000.162500.23640.076780.0
512.45015.7082.57477.10.127800.170000.1578000.0808900.20870.07613...23.75103.40741.60.179100.524900.535500.174100.39850.124400.0
618.25019.98119.601040.00.094630.109000.1127000.0740000.17940.05742...27.66153.201606.00.144200.257600.378400.193200.30630.083680.0
713.71020.8390.20577.90.118900.164500.0936600.0598500.21960.07451...28.14110.60897.00.165400.368200.267800.155600.31960.115100.0
813.00021.8287.50519.80.127300.193200.1859000.0935300.23500.07389...30.73106.20739.30.170300.540100.539000.206000.43780.107200.0
912.46024.0483.97475.90.118600.239600.2273000.0854300.20300.08243...40.6897.65711.40.185301.058001.105000.221000.43660.207500.0
1016.02023.24102.70797.80.082060.066690.0329900.0332300.15280.05697...33.88123.801150.00.118100.155100.145900.099750.29480.084520.0
1115.78017.89103.60781.00.097100.129200.0995400.0660600.18420.06082...27.28136.501299.00.139600.560900.396500.181000.37920.104800.0
1219.17024.80132.401123.00.097400.245800.2065000.1118000.23970.07800...29.94151.701332.00.103700.390300.363900.176700.31760.102300.0
1315.85023.95103.70782.70.084010.100200.0993800.0536400.18470.05338...27.66112.00876.50.113100.192400.232200.111900.28090.062870.0
1413.73022.6193.60578.30.113100.229300.2128000.0802500.20690.07682...32.01108.80697.70.165100.772500.694300.220800.35960.143100.0
1514.54027.5496.73658.80.113900.159500.1639000.0736400.23030.07077...37.13124.10943.20.167800.657700.702600.171200.42180.134100.0
1614.68020.1394.74684.50.098670.072000.0739500.0525900.15860.05922...30.88123.401138.00.146400.187100.291400.160900.30290.082160.0
1716.13020.68108.10798.80.117000.202200.1722000.1028000.21640.07356...31.48136.801315.00.178900.423300.478400.207300.37060.114200.0
1819.81022.15130.001260.00.098310.102700.1479000.0949800.15820.05395...30.88186.802398.00.151200.315000.537200.238800.27680.076150.0
1913.54014.3687.46566.30.097790.081290.0666400.0478100.18850.05766...19.2699.70711.20.144000.177300.239000.128800.29770.072591.0
2013.08015.7185.63520.00.107500.127000.0456800.0311000.19670.06811...20.4996.09630.50.131200.277600.189000.072830.31840.081831.0
219.50412.4460.34273.90.102400.064920.0295600.0207600.18150.06905...15.6665.13314.90.132400.114800.088670.062270.24500.077731.0
2215.34014.26102.50704.40.107300.213500.2077000.0975600.25210.07032...19.08125.10980.90.139000.595400.630500.239300.46670.099460.0
2321.16023.04137.201404.00.094280.102200.1097000.0863200.17690.05278...35.59188.002615.00.140100.260000.315500.200900.28220.075260.0
2416.65021.38110.00904.60.112100.145700.1525000.0917000.19950.06330...31.56177.002215.00.180500.357800.469500.209500.36130.095640.0
2517.14016.40116.00912.70.118600.227600.2229000.1401000.30400.07413...21.40152.401461.00.154500.394900.385300.255000.40660.105900.0
2614.58021.5397.41644.80.105400.186800.1425000.0878300.22520.06924...33.21122.40896.90.152500.664300.553900.270100.42640.127500.0
2718.61020.25122.101094.00.094400.106600.1490000.0773100.16970.05699...27.26139.901403.00.133800.211700.344600.149000.23410.074210.0
2815.30025.27102.40732.40.108200.169700.1683000.0875100.19260.06540...36.71149.301269.00.164100.611000.633500.202400.40270.098760.0
2917.57015.05115.00955.10.098470.115700.0987500.0795300.17390.06149...19.52134.901227.00.125500.281200.248900.145600.27560.079190.0
..................................................................
5397.69125.4448.34170.40.086680.119900.0925200.0136400.20370.07751...31.8954.49223.60.159600.306400.339300.050000.27900.106601.0
54011.54014.4474.65402.90.099840.112000.0673700.0259400.18180.06782...19.6878.78457.80.134500.211800.179700.069180.23290.081341.0
54114.47024.9995.81656.40.088370.123000.1009000.0389000.18720.06341...31.73113.50808.90.134000.420200.404000.120500.31870.102301.0
54214.74025.4294.70668.60.082750.072140.0410500.0302700.18400.05680...32.29107.40826.40.106000.137600.161100.109500.27220.069561.0
54313.21028.0684.88538.40.086710.068770.0298700.0327500.16280.05781...37.1792.48629.60.107200.138100.106200.079580.24730.064431.0
54413.87020.7089.77584.80.095780.101800.0368800.0236900.16200.06688...24.7599.17688.60.126400.203700.137700.068450.22490.084921.0
54513.62023.2387.19573.20.092460.067470.0297400.0244300.16640.05801...29.0997.58729.80.121600.151700.104900.071740.26420.069531.0
54610.32016.3565.31324.90.094340.049940.0101200.0054950.18850.06201...21.7771.12384.90.128500.088420.043840.023810.26810.073991.0
54710.26016.5865.85320.80.088770.080660.0435800.0243800.16690.06714...22.0471.08357.40.146100.224600.178300.083330.26910.094791.0
5489.68319.3461.05285.70.084910.050300.0233700.0096150.15800.06235...25.5969.10364.20.119900.095460.093500.038460.25520.079201.0
54910.82024.2168.89361.60.081920.066020.0154800.0081600.19760.06328...31.4583.90505.60.120400.163300.061940.032640.30590.076261.0
55010.86021.4868.51360.50.074310.042270.0000000.0000000.16610.05948...24.7774.08412.30.100100.073480.000000.000000.24580.065921.0
55111.13022.4471.49378.40.095660.081940.0482400.0225700.20300.06552...28.2677.80436.60.108700.178200.156400.064130.31690.080321.0
55212.77029.4381.35507.90.082760.042340.0199700.0149900.15390.05637...36.0088.10594.70.123400.106400.086530.064980.24070.064841.0
5539.33321.9459.01264.00.092400.056050.0399600.0128200.16920.06576...25.0562.86295.80.110300.082980.079930.025640.24350.073931.0
55412.88028.9282.50514.30.081230.058240.0619500.0234300.15660.05708...35.7488.84595.70.122700.162000.243900.064930.23720.072421.0
55510.29027.6165.67321.40.090300.076580.0599900.0273800.15930.06127...34.9169.57357.60.138400.171000.200000.091270.22260.082831.0
55610.16019.5964.73311.70.100300.075040.0050250.0111600.17910.06331...22.8867.88347.30.126500.120000.010050.022320.22620.067421.0
5579.42327.8859.26271.30.081230.049710.0000000.0000000.17420.06059...34.2466.50330.60.107300.071580.000000.000000.24750.069691.0
55814.59022.6896.39657.10.084730.133000.1029000.0373600.14540.06147...27.27105.90733.50.102600.317100.366200.110500.22580.080041.0
55911.51023.9374.52403.50.092610.102100.1112000.0410500.13880.06570...37.1682.28474.20.129800.251700.363000.096530.21120.087321.0
56014.05027.1591.38600.40.099290.112600.0446200.0430400.15370.06171...33.17100.20706.70.124100.226400.132600.104800.22500.083211.0
56111.20029.3770.67386.00.074490.035580.0000000.0000000.10600.05502...38.3075.19439.60.092670.054940.000000.000000.15660.059051.0
56215.22030.62103.40716.90.104800.208700.2550000.0942900.21280.07152...42.79128.70915.00.141700.791701.170000.235600.40890.140900.0
56320.92025.09143.001347.00.109900.223600.3174000.1474000.21490.06879...29.41179.101819.00.140700.418600.659900.254200.29290.098730.0
56421.56022.39142.001479.00.111000.115900.2439000.1389000.17260.05623...26.40166.102027.00.141000.211300.410700.221600.20600.071150.0
56520.13028.25131.201261.00.097800.103400.1440000.0979100.17520.05533...38.25155.001731.00.116600.192200.321500.162800.25720.066370.0
56616.60028.08108.30858.10.084550.102300.0925100.0530200.15900.05648...34.12126.701124.00.113900.309400.340300.141800.22180.078200.0
56720.60029.33140.101265.00.117800.277000.3514000.1520000.23970.07016...39.42184.601821.00.165000.868100.938700.265000.40870.124000.0
5687.76024.5447.92181.00.052630.043620.0000000.0000000.15870.05884...30.3759.16268.60.089960.064440.000000.000000.28710.070391.0

569 rows × 31 columns

In [101]:
sns.pairplot(df_cancer,hue="target",vars=['mean smoothness','mean perimeter','mean texture','mean area','mean radius'])
#pairplots between 4 features mean radius,mean texture,mean perimeter , mean area
Out[101]:
<seaborn.axisgrid.PairGrid at 0x1e630a36780>
In [102]:
sns.countplot(df_cancer["target"])
#histogram representing number of 0's and 1's in target
Out[102]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e630d37e10>
In [103]:
plt.figure(figsize=(30,10))
sns.heatmap(df_cancer.corr(),annot=True)
#heat map representing corelations between each feature
Out[103]:
<matplotlib.axes._subplots.AxesSubplot at 0x1e631642438>
In [104]:
x=df_cancer.drop(["target"],axis=1)
x
Out[104]:
mean radiusmean texturemean perimetermean areamean smoothnessmean compactnessmean concavitymean concave pointsmean symmetrymean fractal dimension...worst radiusworst textureworst perimeterworst areaworst smoothnessworst compactnessworst concavityworst concave pointsworst symmetryworst fractal dimension
017.99010.38122.801001.00.118400.277600.3001000.1471000.24190.07871...25.38017.33184.602019.00.162200.665600.711900.265400.46010.11890
120.57017.77132.901326.00.084740.078640.0869000.0701700.18120.05667...24.99023.41158.801956.00.123800.186600.241600.186000.27500.08902
219.69021.25130.001203.00.109600.159900.1974000.1279000.20690.05999...23.57025.53152.501709.00.144400.424500.450400.243000.36130.08758
311.42020.3877.58386.10.142500.283900.2414000.1052000.25970.09744...14.91026.5098.87567.70.209800.866300.686900.257500.66380.17300
420.29014.34135.101297.00.100300.132800.1980000.1043000.18090.05883...22.54016.67152.201575.00.137400.205000.400000.162500.23640.07678
512.45015.7082.57477.10.127800.170000.1578000.0808900.20870.07613...15.47023.75103.40741.60.179100.524900.535500.174100.39850.12440
618.25019.98119.601040.00.094630.109000.1127000.0740000.17940.05742...22.88027.66153.201606.00.144200.257600.378400.193200.30630.08368
713.71020.8390.20577.90.118900.164500.0936600.0598500.21960.07451...17.06028.14110.60897.00.165400.368200.267800.155600.31960.11510
813.00021.8287.50519.80.127300.193200.1859000.0935300.23500.07389...15.49030.73106.20739.30.170300.540100.539000.206000.43780.10720
912.46024.0483.97475.90.118600.239600.2273000.0854300.20300.08243...15.09040.6897.65711.40.185301.058001.105000.221000.43660.20750
1016.02023.24102.70797.80.082060.066690.0329900.0332300.15280.05697...19.19033.88123.801150.00.118100.155100.145900.099750.29480.08452
1115.78017.89103.60781.00.097100.129200.0995400.0660600.18420.06082...20.42027.28136.501299.00.139600.560900.396500.181000.37920.10480
1219.17024.80132.401123.00.097400.245800.2065000.1118000.23970.07800...20.96029.94151.701332.00.103700.390300.363900.176700.31760.10230
1315.85023.95103.70782.70.084010.100200.0993800.0536400.18470.05338...16.84027.66112.00876.50.113100.192400.232200.111900.28090.06287
1413.73022.6193.60578.30.113100.229300.2128000.0802500.20690.07682...15.03032.01108.80697.70.165100.772500.694300.220800.35960.14310
1514.54027.5496.73658.80.113900.159500.1639000.0736400.23030.07077...17.46037.13124.10943.20.167800.657700.702600.171200.42180.13410
1614.68020.1394.74684.50.098670.072000.0739500.0525900.15860.05922...19.07030.88123.401138.00.146400.187100.291400.160900.30290.08216
1716.13020.68108.10798.80.117000.202200.1722000.1028000.21640.07356...20.96031.48136.801315.00.178900.423300.478400.207300.37060.11420
1819.81022.15130.001260.00.098310.102700.1479000.0949800.15820.05395...27.32030.88186.802398.00.151200.315000.537200.238800.27680.07615
1913.54014.3687.46566.30.097790.081290.0666400.0478100.18850.05766...15.11019.2699.70711.20.144000.177300.239000.128800.29770.07259
2013.08015.7185.63520.00.107500.127000.0456800.0311000.19670.06811...14.50020.4996.09630.50.131200.277600.189000.072830.31840.08183
219.50412.4460.34273.90.102400.064920.0295600.0207600.18150.06905...10.23015.6665.13314.90.132400.114800.088670.062270.24500.07773
2215.34014.26102.50704.40.107300.213500.2077000.0975600.25210.07032...18.07019.08125.10980.90.139000.595400.630500.239300.46670.09946
2321.16023.04137.201404.00.094280.102200.1097000.0863200.17690.05278...29.17035.59188.002615.00.140100.260000.315500.200900.28220.07526
2416.65021.38110.00904.60.112100.145700.1525000.0917000.19950.06330...26.46031.56177.002215.00.180500.357800.469500.209500.36130.09564
2517.14016.40116.00912.70.118600.227600.2229000.1401000.30400.07413...22.25021.40152.401461.00.154500.394900.385300.255000.40660.10590
2614.58021.5397.41644.80.105400.186800.1425000.0878300.22520.06924...17.62033.21122.40896.90.152500.664300.553900.270100.42640.12750
2718.61020.25122.101094.00.094400.106600.1490000.0773100.16970.05699...21.31027.26139.901403.00.133800.211700.344600.149000.23410.07421
2815.30025.27102.40732.40.108200.169700.1683000.0875100.19260.06540...20.27036.71149.301269.00.164100.611000.633500.202400.40270.09876
2917.57015.05115.00955.10.098470.115700.0987500.0795300.17390.06149...20.01019.52134.901227.00.125500.281200.248900.145600.27560.07919
..................................................................
5397.69125.4448.34170.40.086680.119900.0925200.0136400.20370.07751...8.67831.8954.49223.60.159600.306400.339300.050000.27900.10660
54011.54014.4474.65402.90.099840.112000.0673700.0259400.18180.06782...12.26019.6878.78457.80.134500.211800.179700.069180.23290.08134
54114.47024.9995.81656.40.088370.123000.1009000.0389000.18720.06341...16.22031.73113.50808.90.134000.420200.404000.120500.31870.10230
54214.74025.4294.70668.60.082750.072140.0410500.0302700.18400.05680...16.51032.29107.40826.40.106000.137600.161100.109500.27220.06956
54313.21028.0684.88538.40.086710.068770.0298700.0327500.16280.05781...14.37037.1792.48629.60.107200.138100.106200.079580.24730.06443
54413.87020.7089.77584.80.095780.101800.0368800.0236900.16200.06688...15.05024.7599.17688.60.126400.203700.137700.068450.22490.08492
54513.62023.2387.19573.20.092460.067470.0297400.0244300.16640.05801...15.35029.0997.58729.80.121600.151700.104900.071740.26420.06953
54610.32016.3565.31324.90.094340.049940.0101200.0054950.18850.06201...11.25021.7771.12384.90.128500.088420.043840.023810.26810.07399
54710.26016.5865.85320.80.088770.080660.0435800.0243800.16690.06714...10.83022.0471.08357.40.146100.224600.178300.083330.26910.09479
5489.68319.3461.05285.70.084910.050300.0233700.0096150.15800.06235...10.93025.5969.10364.20.119900.095460.093500.038460.25520.07920
54910.82024.2168.89361.60.081920.066020.0154800.0081600.19760.06328...13.03031.4583.90505.60.120400.163300.061940.032640.30590.07626
55010.86021.4868.51360.50.074310.042270.0000000.0000000.16610.05948...11.66024.7774.08412.30.100100.073480.000000.000000.24580.06592
55111.13022.4471.49378.40.095660.081940.0482400.0225700.20300.06552...12.02028.2677.80436.60.108700.178200.156400.064130.31690.08032
55212.77029.4381.35507.90.082760.042340.0199700.0149900.15390.05637...13.87036.0088.10594.70.123400.106400.086530.064980.24070.06484
5539.33321.9459.01264.00.092400.056050.0399600.0128200.16920.06576...9.84525.0562.86295.80.110300.082980.079930.025640.24350.07393
55412.88028.9282.50514.30.081230.058240.0619500.0234300.15660.05708...13.89035.7488.84595.70.122700.162000.243900.064930.23720.07242
55510.29027.6165.67321.40.090300.076580.0599900.0273800.15930.06127...10.84034.9169.57357.60.138400.171000.200000.091270.22260.08283
55610.16019.5964.73311.70.100300.075040.0050250.0111600.17910.06331...10.65022.8867.88347.30.126500.120000.010050.022320.22620.06742
5579.42327.8859.26271.30.081230.049710.0000000.0000000.17420.06059...10.49034.2466.50330.60.107300.071580.000000.000000.24750.06969
55814.59022.6896.39657.10.084730.133000.1029000.0373600.14540.06147...15.48027.27105.90733.50.102600.317100.366200.110500.22580.08004
55911.51023.9374.52403.50.092610.102100.1112000.0410500.13880.06570...12.48037.1682.28474.20.129800.251700.363000.096530.21120.08732
56014.05027.1591.38600.40.099290.112600.0446200.0430400.15370.06171...15.30033.17100.20706.70.124100.226400.132600.104800.22500.08321
56111.20029.3770.67386.00.074490.035580.0000000.0000000.10600.05502...11.92038.3075.19439.60.092670.054940.000000.000000.15660.05905
56215.22030.62103.40716.90.104800.208700.2550000.0942900.21280.07152...17.52042.79128.70915.00.141700.791701.170000.235600.40890.14090
56320.92025.09143.001347.00.109900.223600.3174000.1474000.21490.06879...24.29029.41179.101819.00.140700.418600.659900.254200.29290.09873
56421.56022.39142.001479.00.111000.115900.2439000.1389000.17260.05623...25.45026.40166.102027.00.141000.211300.410700.221600.20600.07115
56520.13028.25131.201261.00.097800.103400.1440000.0979100.17520.05533...23.69038.25155.001731.00.116600.192200.321500.162800.25720.06637
56616.60028.08108.30858.10.084550.102300.0925100.0530200.15900.05648...18.98034.12126.701124.00.113900.309400.340300.141800.22180.07820
56720.60029.33140.101265.00.117800.277000.3514000.1520000.23970.07016...25.74039.42184.601821.00.165000.868100.938700.265000.40870.12400
5687.76024.5447.92181.00.052630.043620.0000000.0000000.15870.05884...9.45630.3759.16268.60.089960.064440.000000.000000.28710.07039

569 rows × 30 columns

In [105]:
y=df_cancer["target"]
y
Out[105]:
0      0.0
1      0.0
2      0.0
3      0.0
4      0.0
5      0.0
6      0.0
7      0.0
8      0.0
9      0.0
10     0.0
11     0.0
12     0.0
13     0.0
14     0.0
15     0.0
16     0.0
17     0.0
18     0.0
19     1.0
20     1.0
21     1.0
22     0.0
23     0.0
24     0.0
25     0.0
26     0.0
27     0.0
28     0.0
29     0.0
      ... 
539    1.0
540    1.0
541    1.0
542    1.0
543    1.0
544    1.0
545    1.0
546    1.0
547    1.0
548    1.0
549    1.0
550    1.0
551    1.0
552    1.0
553    1.0
554    1.0
555    1.0
556    1.0
557    1.0
558    1.0
559    1.0
560    1.0
561    1.0
562    0.0
563    0.0
564    0.0
565    0.0
566    0.0
567    0.0
568    1.0
Name: target, Length: 569, dtype: float64
In [106]:
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=5)
#spliting data into test and train data
In [107]:
from sklearn.ensemble import RandomForestClassifier
RF_MODEL= RandomForestClassifier(n_estimators=100)
RF_MODEL=RF_MODEL.fit(X_train,y_train)
#Training model using Random Forest Classifier
In [108]:
from sklearn.metrics import classification_report
In [109]:
y_predict=RF_MODEL.predict(X_test)
In [110]:
y_predict
#predicted output by RF model 
Out[110]:
array([ 0.,  1.,  1.,  1.,  1.,  0.,  1.,  1.,  0.,  1.,  1.,  1.,  0.,
        1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.,  1.,  1.,  1.,  1.,  1.,
        1.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  1.,  1.,  0.,  1.,  1.,
        0.,  1.,  1.,  1.,  0.,  1.,  1.,  0.,  0.,  1.,  0.,  1.,  1.,
        1.,  1.,  1.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  1.,  1.,  1.,
        1.,  1.,  1.,  1.,  0.,  1.,  0.,  1.,  1.,  1.,  1.,  0.,  1.,
        0.,  0.,  0.,  1.,  0.,  0.,  0.,  1.,  0.,  1.,  0.,  0.,  0.,
        0.,  1.,  1.,  0.,  0.,  1.,  1.,  1.,  1.,  1.,  0.,  1.,  1.,
        0.,  0.,  1.,  0.,  1.,  1.,  1.,  0.,  0.,  0.])
In [113]:
print(classification_report(y_test,y_predict))
             precision    recall  f1-score   support

        0.0       0.98      0.94      0.96        48
        1.0       0.96      0.98      0.97        66

avg / total       0.97      0.96      0.96       114

Here, we can see we trained the Random Forest model for predicting breast
cancer with an accuracy of 97%.


Comments