DSASSign 4
DSASSign 4
1. Dataset Selection:
import numpy as np
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
print("\n\n")
print("KNN Model Performance:")
print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
print("F1-score:", f1)
print("\n\n")
Output:
6. Model Optimization and Hyperparameter Tuning:
print("\n\nAfter optimization")
Output:
6. Conclusion and Discussion
Summary:
The KNN algorithm was applied to the breast cancer dataset using Python.
The initial KNN model achieved an accuracy, precision, recall, and F1-score
of 94.74%.
Hyperparameter tuning using grid search found the optimal value of K to be
3.
The optimized KNN model achieved an accuracy, precision, recall, and F1-
score of 95.74%.
The breast cancer dataset contained 569 samples and 30 features, with class
labels of "malignant" and "benign".
The KNN algorithm is relevant and applicable in real-world scenarios for
classification tasks.
The algorithm has strengths in its simplicity, versatility, and ability to handle
various datasets.
Limitations of KNN include computational complexity, sensitivity to
parameter choices, and equal weighting of features.
Potential improvements include using advanced distance metrics, feature
selection, and dimensionality reduction techniques.
The optimized KNN model showed promising results in classifying breast
cancer cases.
Further research and experimentation can be done to explore other
techniques and algorithms for improved performance.
All Source Code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 5 18:39:26 2023
@author: nasir
"""
print("\n\n")
print("KNN Model Performance:")
print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
print("F1-score:", f1)
print("\n\n")
print("\n\nAfter optimization")