<jupyter_start><jupyter_text>![grafik.png](attachment:grafik.png)  WAIA Data Science with Python 2021 - Week 9 Exercises   Cancer dataset - Model validation<jupyter_code>!pip install -q kaggle
!pip install -q kaggle-cli
!mkdir -p ~/.kaggle
!cp "/content/drive/MyDrive/ColabNotebooks/kaggle.json" ~/.kaggle/
!cat ~/.kaggle/kaggle.json 
!chmod 600 ~/.kaggle/kaggle.json
# For competition datasets
!kaggle competitions download -c uciml/breast-cancer-wisconsin-data -p /content/drive/MyDrive/ColabNotebooks
# For other datasets
!kaggle datasets download -d uciml/breast-cancer-wisconsin-data -p /content/drive/MyDrive/ColabNotebooks
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import pylab as pl
import matplotlib.cm as cm
from matplotlib.colors import ListedColormap, BoundaryNorm
import matplotlib.patches as mpatches
import matplotlib.patches as mpatches
import matplotlib.dates as mpl_dates
import datetime
from datetime import datetime

#some clustering libraries
import sklearn
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
from scipy.cluster.hierarchy import linkage
from scipy.cluster.hierarchy import dendrogram
from scipy.cluster.hierarchy import cut_tree
from sklearn import datasets
from itertools import cycle

from sklearn import svm, datasets
from sklearn.datasets import make_classification
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn.preprocessing import label_binarize
from sklearn.multiclass import OneVsRestClassifier
from scipy import interp
from scipy.stats import randint
from sklearn.metrics import roc_auc_score
from sklearn.metrics import mean_squared_error

from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVR

from sklearn.linear_model import LinearRegression
from sklearn import datasets, linear_model

from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.cluster import KMeans
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import log_loss
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.metrics import roc_auc_score, roc_curve
from sklearn.metrics import precision_recall_curve, auc
from sklearn.metrics import precision_recall_fscore_support as score, precision_score, recall_score, f1_score
from sklearn.metrics import mean_absolute_error, mean_squared_error
from math import sqrt<jupyter_output><empty_output><jupyter_text>This exercise builds up on the work that you have been doing with the previous assigment. Let's imagine, you have already loaded and prepared the breast cancer dataset in this notebook.<jupyter_code>#1 Type code here
from sklearn.datasets import load_breast_cancer 
df = load_breast_cancer(as_frame=True)
bc=df.frame
bc.head(2)
from sklearn import datasets
import pandas as pd
breast_cancer = datasets.load_breast_cancer 
x1=pd.DataFrame(X)   #converting array into data frame
#print(x1.head())   
X = bc[: -30]
#print(X)<jupyter_output><empty_output><jupyter_text>Now perform a randomized search on two decision tree models. The parameters max depth, max features, min sample leafs and criterion should be varied. Perform one search using all features and one search using only the min features. How much does the accuracy vary between both? Which is the model with the best score?<jupyter_code>#2 Type code here
#RANDOMIZED SEARCH
# # Create feature matrix
X = bc[: -31]
# Create target vector
y = bc.target
# Creating the hyperparameter grid
from scipy.stats import randint
param_dist = {"max_depth": [3, None],
 "max_features": randint(1, 10),
 "min_samples_leaf": randint(1, 10),
 "criterion": ["gini", "entropy"]}

# Instantiating Decision Tree classifier
tree = DecisionTreeClassifier()

# Instantiating RandomizedSearchCV object
tree_cv = RandomizedSearchCV(tree, param_dist, cv = 10)

tree_cv.fit(bc, y)

# Print the tuned parameters and score
print("Tuned Decision Tree Parameters: {}".format(tree_cv.best_params_))
print("Best score is {}".format(tree_cv.best_score_))

bc.columns
type(bc)
mean_cols={''}<jupyter_output><empty_output><jupyter_text>Plot the confusion matrix for the model with the best score using train test split (80%, 20%).<jupyter_code>#3 Type code here
#3 Type code here
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
#train_X, test_X, train_y, test_y = train_test_split(bc.columns, y, test_size=0.2,random_state=0)
train_X, test_X, train_y, test_y = train_test_split(bc[[  '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' ]], y, test_size=0.2,random_state=0)

#print(train_X)
#print(test_X)

best_tree = DecisionTreeClassifier(criterion= 'gini', max_depth= None, max_features= 6, min_samples_leaf= 4)
best_tree.fit(train_X, train_y)
best_tree_pred = best_tree.predict(test_X)
cm = confusion_matrix(test_y, best_tree_pred)
print(cm)

# so only 6 false predictions and accuracy= 45+62= 108, 6 false predictions so accuracy=108/114


import seaborn as sns 
sns.heatmap(cm,annot = True)
fig=plt.figure()
ax=fig.add_subplot(111)
cax=ax.matshow(cm)
plt.title('confusion matrix')
fig.colorbar(cax)

plt.xlabel ('predicted')
plt.ylabel('true')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0787131032328596, 'fraction_numerical': 0.00937060752772138, 'mean_word_length': 6.244343891402715, 'pattern_counts': {'":': 4, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '307242', 'license': 'MIT', 'n_tokens_mistral': 2084, 'n_tokens_neox': 1977, 'n_words': 635, 'path': 'dsb32021-9w-assignment-Breastcancer.ipynb', 'repo_name': 'BanuTK/Classification', 'size': '6403'}