【问题标题】:why confusion matrix do not show accuracy in its calculated measures [closed]为什么混淆矩阵在其计算度量中不显示准确性[关闭]
【发布时间】:2021-04-14 16:23:41
【问题描述】:

我尝试在数据集上运行 knn 方法后计算准确性。但它的输出并未显示其中的准确度度量。如何修复它以显示其计算测量的准确性? 感谢您的考虑。

这里是数据集: http://gitlab.rahnemacollege.com/rahnemacollege/tuning-registration-JusticeInWork/raw/master/dataset.csv

这是我的代码:

!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))

【问题讨论】:

  • 混淆矩阵不应该显示准确性。但是你可以直接从它的对角线得到它...
  • 所有元素对角线元素的总和...

标签: python machine-learning knn confusion-matrix


【解决方案1】:

您可以使用以下代码来计算准确性:

from sklearn.metrics import accuracy_score

accuracy = accuracy_score(y_test, y_pred)

【讨论】:

    猜你喜欢
    • 2012-11-12
    • 2018-04-16
    • 2018-04-01
    • 2019-01-11
    • 2013-12-02
    • 1970-01-01
    • 2019-09-28
    • 2019-01-01
    相关资源
    最近更新 更多