專案

一般

配置概況

Bug #66 » Knn.py

建誠 林, 2023-11-02 00:23

 
import pandas as pd
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report

# Load the data from the csv file
df = pd.read_csv('data.csv')

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
df[['avg_r', 'avg_g', 'avg_b', 'median_r', 'median_g', 'median_b', 'mode_r', 'mode_g', 'mode_b', 'max_r']], df['result'], test_size=0.2, random_state=42)

# Create the KNN classifier
knn = KNeighborsClassifier(n_neighbors=5)

# Fit the classifier to the training data
knn.fit(X_train, y_train)

# Make predictions on the testing data
y_pred = knn.predict(X_test)

# Evaluate the performance of the classifier
print(classification_report(y_test, y_pred))
(3-3/6)