为什么混淆矩阵在其计算的度量中不显示准确性
why confusion matrix do not show accuracy in its calculated measures
我尝试在 运行 数据集上的 knn 方法之后计算准确度。但是它的输出没有显示其中的准确性度量。我该如何修复它以显示其计算量度的准确性?
感谢您的考虑。
这是我的代码:
!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))
您可以使用以下代码计算准确率:
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, y_pred)
我尝试在 运行 数据集上的 knn 方法之后计算准确度。但是它的输出没有显示其中的准确性度量。我该如何修复它以显示其计算量度的准确性? 感谢您的考虑。
这是我的代码:
!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))
您可以使用以下代码计算准确率:
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, y_pred)