为什么混淆矩阵在其计算指标中不显示准确率

我在数据集上运行knn方法后尝试计算准确率,但其输出中没有显示准确率指标。我该如何修复这个问题以便在其计算指标中显示准确率?感谢您的考虑。

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

这是我的代码:

!pip install sklearn!pip uninstall pandas!pip install pandas==1.2.0import pandas as pdimport mathimport numpy as npimport matplotlib.pyplot as pltfrom google.colab import filesfrom sklearn.model_selection import train_test_splitfrom sklearn.preprocessing import StandardScalerfrom sklearn.neighbors import KNeighborsClassifierfrom 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_scoreaccuracy = accuracy_score(y_test, y_pred)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注