In: Computer Science
In Python. Create a function called predictKNN(). Your function will return the classification of your data-pointIn addition to any parameters you see fit, your function should accept:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import classification_report, confusion_matrix
# In os.chdir() provide the address of your working directory which is where your dataset is stored.
import os
os.chdir(r"C:\Users\LENOVO\Documents\Data Science\DataSets")
def predictKNN(k,dataframe):
#Loading the dataframe
dataframe = pd.read_csv(dataframe)
# Dividing dataframe into indepent variable(X) and dependent or target variable(Y)
X = dataframe.iloc[:, :-1].values
y = dataframe.iloc[:, -1].values
# Dividing dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)
# Preprocessing the data
scaler = StandardScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
#Applying KNN
classifier = KNeighborsClassifier(n_neighbors=2)
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
# Checking the performance
from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
return
Note :
I am attaching the code where I used diabetes dataset saved in my pc