<jupyter_start><jupyter_text>Importing necessary libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Load Dataset<jupyter_code>from google.colab import drive
drive.mount('/content/gdrive', force_remount = True)
root_dir = '/content/gdrive/My Drive/'
base_dir = root_dir +'Colab Notebooks/Datasets'
base_dir
%cd /content/gdrive/My\ Drive/Colab\ Notebooks/Datasets
#Verifiying present working directory
!pwd
cancer = pd.read_csv('breast_cancer_wisconsin.csv')
cancer.head()
cancer.columns<jupyter_output><empty_output><jupyter_text>We drop 'id' and 'Unnamed :32' since these are not required<jupyter_code>cancer.drop('id', axis = 1, inplace = True)
cancer.drop('Unnamed: 32', axis = 1, inplace = True)
cancer.head()<jupyter_output><empty_output><jupyter_text>Gathering information about dataset<jupyter_code>cancer.shape
cancer.groupby('diagnosis').size()<jupyter_output><empty_output><jupyter_text>From above cell we find that out of 569 data points, we have 357 as Benign and 212 as malignant<jupyter_code>cancer.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 569 entries, 0 to 568
Data columns (total 31 columns):
diagnosis                  569 non-null object
radius_mean                569 non-null float64
texture_mean               569 non-null float64
perimeter_mean             569 non-null float64
area_mean                  569 non-null float64
smoothness_mean            569 non-null float64
compactness_mean           569 non-null float64
concavity_mean             569 non-null float64
concave points_mean        569 non-null float64
symmetry_mean              569 non-null float64
fractal_dimension_mean     569 non-null float64
radius_se                  569 non-null float64
texture_se                 569 non-null float64
perimeter_se               569 non-null float64
area_se                    569 non-null float64
smoothness_se              569 non-null float64
compactness_se             569 non-null float64
concavity_se               569 non-null float64
concave points_se          569 n[...]<jupyter_text>The above cell indicates we have no Null Values<jupyter_code>import seaborn as sns
sns.countplot(cancer['diagnosis'], label = 'count')<jupyter_output><empty_output><jupyter_text>Splitting data into training and test set<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(cancer.loc[:, cancer.columns != 'diagnosis'], cancer['diagnosis'], stratify = cancer['diagnosis'], random_state = 66)<jupyter_output><empty_output><jupyter_text>Using Decision Tree Classifier<jupyter_code>from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier(random_state = 0).fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Performance Evaluation<jupyter_code>tree.score(X_train, y_train) #Acuuracy check for training set
tree.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>> We can see from the above 2 cells that accuracy on training data is 100% and on test data is 93.7%>We can conclude that our model has been over-fitted and thus, gives 100% accuracy on training set Improving the model using parameter tuning<jupyter_code>tree = DecisionTreeClassifier(max_depth = 4, random_state = 0).fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Prediction on test set<jupyter_code>tree.predict(X_test)<jupyter_output><empty_output><jupyter_text>Performance Evaluation<jupyter_code>tree.score(X_train,y_train)#For training set
tree.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>We overcome the problem of overfitting and obtain a 98.59% accuracy for training set and 93.7% accuracy for test set Feature importance in treesFeature importance rates how important each feature is for the decision a tree makes. It is a number between 0 and 1 for each feature, where 0 means “not used at all” and 1 means “perfectly predicts the target.” The feature importances always sum to 1<jupyter_code>tree.feature_importances_<jupyter_output><empty_output><jupyter_text>Visualization of Feature importances<jupyter_code>cancer_features = [k for i,k in enumerate(cancer.columns) if i!=0]
def plot_feature_importances(model):
    plt.figure(figsize=(12,12))
    n_features = 30
    plt.barh(range(n_features), model.feature_importances_, align='center')
    plt.yticks(np.arange(n_features), cancer_features)
    plt.xlabel("Feature importance")
    plt.ylabel("Feature")
    plt.ylim(-1, n_features)

plot_feature_importances(tree)
plt.savefig('feature_importance')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08422631804543507, 'fraction_numerical': 0.03214744963566224, 'mean_word_length': 5.148880105401845, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '129109', 'license': 'Apache-2.0', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1469, 'n_words': 382, 'path': 'Breast_Cancer_Detection.ipynb', 'repo_name': 'Sayan3sarkar/Breast_Cancer_Detection_using_Decsion_Tree_Classifier', 'size': '4666'}