Supervised Learning - Classification Week 8 Challenge
1.Apply knn to the “Surface defects in stainless steel plates” and identify the differences.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
import pandas as pd
# Replace 'your_dataset.csv' with the actual path to your CSV file
csv_file_path = 'C:/Users/91917/OneDrive/Desktop/Python/Challenge7/faults.csv'
# Read the CSV file into a pandas DataFrame
df = pd.read_csv(csv_file_path)
# Display the first few rows of the DataFrame to inspect the data
print(df.head())
x=df[["Maximum_of_Luminosity", "Length_of_Conveyer", "Steel_Plate_Thickness"]]
y = df["SigmoidOfAreas"]
print(x)
# Split the dataset into training and testing sets
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
# Standardize the features (optional, but can improve performance)
scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(x_train)
x_test_scaled = scaler.transform(x_test)
# Create a KNN classifier with k=3
knn_classifier = KNeighborsClassifier(n_neighbors=3)
# Train the classifier on the training data
knn_classifier.fit(x_train_scaled, y_train)
# Make predictions on the test data
predictions = knn_classifier.predict(x_test_scaled)
# Calculate accuracy
accuracy = accuracy_score(y_test, predictions)
print(f"Accuracy: {accuracy:.2f}")
2.What are the pros and cons of knn?
K-Nearest Neighbors (KNN) is a machine learning algorithm used for classification and regression tasks.
It makes predictions based on the majority class or average of the k-nearest data points in the feature space.
Pros:
- Simplicity: KNN is straightforward to understand and implement.
- No Training Phase: KNN is a lazy learner, which means it does not require an explicit training phase.
- Non-Parametric: KNN makes no assumptions about the underlying data distribution.
- Adaptability to New Data: KNN can easily adapt to changes in the dataset.
- Effective for Small Datasets: KNN can perform well when the dataset is small.
- Can Handle Multiclass Classification: KNN can handle multi-class classification problems.
Cons:
- Computational Complexity: Predictions in KNN involve calculating distances between data points.
- Sensitive to Noise and Outliers: KNN can be sensitive to noisy data and outliers.
- Choice of K: The performance of KNN is influenced by the choice of the parameter 'k' (the number of neighbors).
- Curse of Dimensionality: As the number of dimensions (features) increases, the density of data points in the space decreases.
- Bias Towards Majority Class: In classification problems with imbalanced classes, KNN may have a bias toward the majority class.
- Inefficient for Large Datasets: The need to compute distances for each data point can be inefficient and slow for large datasets.
- Feature Scaling: KNN is sensitive to the scale of features, so proper feature scaling is crucial to ensure accurate predictions.
- Local Optima: KNN's predictions can be influenced by the distribution of neighbors, potentially leading to local optima and suboptimal results.