<jupyter_start><jupyter_text>Importing required libraries<jupyter_code>import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split,GridSearchCV
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier,GradientBoostingClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix, f1_score, recall_score
from sklearn.metrics import precision_score,classification_report,accuracy_score
from sklearn import metrics<jupyter_output><empty_output><jupyter_text>Loading Data<jupyter_code>df = pd.DataFrame(load_breast_cancer()['data'],columns=load_breast_cancer()['feature_names'])
df['y'] = load_breast_cancer()['target']
df.head()
df.shape
print("Count of Zero's in label are",len(df[df.y==0]))
print("Count of one's in label are",len(df[df.y==1]))<jupyter_output>Count of Zero's in label are 212
Count of one's in label are 357<jupyter_text>Check for missing values<jupyter_code>df.isna().any()<jupyter_output><empty_output><jupyter_text>Information about all the variables<jupyter_code>df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 569 entries, 0 to 568
Data columns (total 31 columns):
 #   Column                   Non-Null Count  Dtype  
---  ------                   --------------  -----  
 0   mean radius              569 non-null    float64
 1   mean texture             569 non-null    float64
 2   mean perimeter           569 non-null    float64
 3   mean area                569 non-null    float64
 4   mean smoothness          569 non-null    float64
 5   mean compactness         569 non-null    float64
 6   mean concavity           569 non-null    float64
 7   mean concave points      569 non-null    float64
 8   mean symmetry            569 non-null    float64
 9   mean fractal dimension   569 non-null    float64
 10  radius error             569 non-null    float64
 11  texture error            569 non-null    float64
 12  perimeter error          569 non-null    float64
 13  area error               569 non-null    float64
 14  smoothness error         5[...]<jupyter_text>Feature Selection<jupyter_code>X = df.iloc[:, :-1] # Features
y = df.iloc[:, -1] # Target variable<jupyter_output><empty_output><jupyter_text>Spliting into train and test<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,random_state = 1)<jupyter_output><empty_output><jupyter_text>Decision tree<jupyter_code>d_clf = DecisionTreeClassifier()
d_clf.fit(X_train,y_train)<jupyter_output><empty_output><jupyter_text>Model performance<jupyter_code>dy_pred = d_clf.predict(X_test)
print("confustion matrix\n",confusion_matrix(y_test, dy_pred))
print("\nAccuracy score = ",accuracy_score(y_test, dy_pred))
print("Recall score = ",recall_score(y_test, dy_pred))
print("Precision score = ",precision_score(y_test, dy_pred))
print("F1 score = ",f1_score(y_test, dy_pred))<jupyter_output>confustion matrix
 [[39  3]
 [ 2 70]]

Accuracy score =  0.956140350877193
Recall score =  0.9722222222222222
Precision score =  0.958904109589041
F1 score =  0.9655172413793104<jupyter_text>Random forest<jupyter_code>r_clf = RandomForestClassifier()
r_clf.fit(X_train,y_train)<jupyter_output><empty_output><jupyter_text>Model performance<jupyter_code>ry_pred = r_clf.predict(X_test)
print("confustion matrix \n",confusion_matrix(y_test, ry_pred))
print("\nAccuracy score = ",accuracy_score(y_test, ry_pred))
print("Recall score = ",recall_score(y_test, ry_pred))
print("Precision score = ",precision_score(y_test, ry_pred))
print("F1 score = ",f1_score(y_test, ry_pred))<jupyter_output>confustion matrix 
 [[37  5]
 [ 1 71]]

Accuracy score =  0.9473684210526315
Recall score =  0.9861111111111112
Precision score =  0.9342105263157895
F1 score =  0.9594594594594595<jupyter_text>Gradient Boosting<jupyter_code>gradient_booster = GradientBoostingClassifier()
gradient_booster.fit(X_train,y_train)<jupyter_output><empty_output><jupyter_text>Model performance<jupyter_code>gy_pred = gradient_booster.predict(X_test)
print("\n confusion matrix \n",confusion_matrix(y_test, gy_pred))
print("\nAccuracy score = ",accuracy_score(y_test, gy_pred))
print("Recall score = ",recall_score(y_test, gy_pred))
print("Precision score = ",precision_score(y_test, gy_pred))
print("F1 score = ",f1_score(y_test, gy_pred))
#print(classification_report(y_test,gradient_booster.predict(X_test)))<jupyter_output>confusion matrix 
 [[38  4]
 [ 0 72]]

Accuracy score =  0.9649122807017544
Recall score =  1.0
Precision score =  0.9473684210526315
F1 score =  0.972972972972973<jupyter_text>K-Nearest Neighbour<jupyter_code>knn = KNeighborsClassifier()
knn.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Model performance<jupyter_code>ky_pred = knn.predict(X_test)
print("confustion matrix \n",confusion_matrix(y_test, ky_pred))
print("\nAccuracy score = ",accuracy_score(y_test, ky_pred))
print("Recall score = ",recall_score(y_test, ky_pred))
print("Precision score = ",precision_score(y_test, ky_pred))
print("F1 score = ",f1_score(y_test, ky_pred))<jupyter_output>confustion matrix 
 [[37  5]
 [ 2 70]]

Accuracy score =  0.9385964912280702
Recall score =  0.9722222222222222
Precision score =  0.9333333333333333
F1 score =  0.9523809523809524<jupyter_text>Hyper-parameters of the Decision Tree model<jupyter_code>clf = DecisionTreeClassifier()
tuned_parameters = {"criterion": ["gini", "entropy"], "max_depth": [10, 20, 50, 100],"min_samples_leaf":[10, 20, 50]}
cv_grid = GridSearchCV(clf, param_grid = tuned_parameters, scoring = 'roc_auc', verbose = 5)
cv_grid.fit(X_train, y_train)<jupyter_output>Fitting 5 folds for each of 24 candidates, totalling 120 fits
[CV] criterion=gini, max_depth=10, min_samples_leaf=10 ...............
[CV]  criterion=gini, max_depth=10, min_samples_leaf=10, score=0.972, total=   0.0s
[CV] criterion=gini, max_depth=10, min_samples_leaf=10 ...............
[CV]  criterion=gini, max_depth=10, min_samples_leaf=10, score=0.945, total=   0.0s
[CV] criterion=gini, max_depth=10, min_samples_leaf=10 ...............
[CV]  criterion=gini, max_depth=10, min_samples_leaf=10, score=0.972, total=   0.0s
[CV] criterion=gini, max_depth=10, min_samples_leaf=10 ...............
[CV]  criterion=gini, max_depth=10, min_samples_leaf=10, score=0.980, total=   0.0s
[CV] criterion=gini, max_depth=10, min_samples_leaf=10 ...............
[CV]  criterion=gini, max_depth=10, min_samples_leaf=10, score=0.969, total=   0.0s
[CV] criterion=gini, max_depth=10, min_samples_leaf=20 ...............
[CV]  criterion=gini, max_depth=10, min_samples_leaf=20, score=0.940, total=   0.0s
[CV] cri[...]<jupyter_text>Model performance<jupyter_code>y_pred = cv_grid.predict(X_test)
print("confustion matrix \n",confusion_matrix(y_test, y_pred))
print("\nAccuracy score = ",accuracy_score(y_test, y_pred))
print("Recall score = ",recall_score(y_test, y_pred))
print("Precision score = ",precision_score(y_test, y_pred))
print("F1 score = ",f1_score(y_test, y_pred))<jupyter_output>confustion matrix 
 [[29 13]
 [ 3 69]]

Accuracy score =  0.8596491228070176
Recall score =  0.9583333333333334
Precision score =  0.8414634146341463
F1 score =  0.8961038961038961<jupyter_text>Comparing the results of various models<jupyter_code>models_scores_table = pd.DataFrame({  'Decision Tree':[ accuracy_score(y_test, dy_pred),
                                                        recall_score(y_test, dy_pred),
                                                        precision_score(y_test, dy_pred),
                                                        f1_score(y_test, dy_pred)],
                                       
                                      'Random Forest':[ accuracy_score(y_test,ry_pred),
                                                        recall_score(y_test, ry_pred),
                                                        precision_score(y_test, ry_pred),
                                                        f1_score(y_test, ry_pred)],
                                       
                                      'Gradient Boosting':[ accuracy_score(y_test, gy_pred),
                                                        recall_score(y_test, gy_pred),
                                                        precision_score(y_test, gy_pred),
                                                        f1_score(y_test, gy_pred)],
                                      'KNN':[ accuracy_score(y_test,ky_pred),
                                                        recall_score(y_test, ky_pred),
                                                        precision_score(y_test, ky_pred),
                                                        f1_score(y_test, ky_pred)],
                                      'Decision Tree(tuned)':[ accuracy_score(y_test,y_pred),
                                                        recall_score(y_test, y_pred),
                                                        precision_score(y_test, y_pred),
                                                        f1_score(y_test, y_pred)]},                                      
                                   index=['Accuracy', 'Precision', 'Recall', 'F1 Score'])
models_scores_table.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1126956894702981, 'fraction_numerical': 0.06369290156551577, 'mean_word_length': 2.9521186440677964, 'pattern_counts': {'":': 3, '<': 62, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 5, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '472289', 'license': 'MIT', 'n_tokens_mistral': 3461, 'n_tokens_neox': 3015, 'n_words': 576, 'path': 'week 3/Project Week 3-20233.ipynb', 'repo_name': 'krishnakanth-G/Datascience-with-python', 'size': '9326'}