Preview:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, confusion_matrix,classification_report

# Step 1: Load Iris dataset
file_path = "/content/iris.csv"  # Replace with the actual file path
iris_data = pd.read_csv(file_path)

# Separate features (X) and target (y)
X = iris_data.drop(["Species"], axis=1)  # Drop unnecessary columns
y = iris_data["Species"]

# Step 2: Split the dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Step 3: Apply StandardScaler
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.fit_transform(X_test)

# Step 4: Load KNN model
knn = KNeighborsClassifier(n_neighbors=5)  # Default k=5

# Step 5: Train the model and make predictions
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)

# Step 6: Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
classification_report=classification_report(y_test,y_pred)

# Display results
print("Accuracy Score:", accuracy)
print("Confusion Matrix:\n", conf_matrix)
print("Confusion Matrix:\n", classification_report)
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter