-2

I try to calculate accuracy after running the knn method on the dataset. But its output does not show the accuracy measure in it. how can I fix it in order to show accuracy in its calculated measure? thank you for your consideration.

here is dataset: http://gitlab.rahnemacollege.com/rahnemacollege/tuning-registration-JusticeInWork/raw/master/dataset.csv

here is my code:

!pip install sklearn
!pip uninstall pandas
!pip install pandas==1.2.0
import pandas as pd
import math
import numpy as np
import matplotlib.pyplot as plt
from google.colab import files
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
#-----------------read file---------------------------
uploaded = files.upload()
with open('dataset.csv', 'r') as data:
   df3 = pd.read_csv(data , encoding = ('ansi'))
   df = pd.DataFrame(df3)
   print (df)
   df["TargetProId"]=df["TargetProId"].fillna("Unknown")
   #------new--------
   del df['TaskState']
   del df['Price']
#----------------------preprocessing------------------
#----------function definition------------------
def string_to_int(s):
    ord3 = lambda x : '%.3d' % ord(x)
    return int(''.join(map(ord3, s)))

id_cols = [k for k in df.columns if k.lower().endswith('id')]
#id_cols.append('TaskState')
df[id_cols] = df[id_cols].applymap(string_to_int)
#----------------------set data------------------------
x = df.iloc[:,0:10]
y = df.iloc[:,11]
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3)
print(X_train.shape, y_train.shape)
print(X_test.shape, y_test.shape)
#-------------------------normalize--------------------
scaler = StandardScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
#-----------------------------knn-----------------------
classifier = KNeighborsClassifier(n_neighbors=math.floor(math.sqrt(24855)))
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
#-------------------------result------------------------
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
hsi
  • 97
  • 1
  • 11

1 Answers1

0

You can use the following piece of code to calculate accuracy:

from sklearn.metrics import accuracy_score

accuracy = accuracy_score(y_test, y_pred)
mrazizi
  • 319
  • 2
  • 15