如何打印分类报告和ROC_AUC_SCORE?

时间:2019-01-31 18:04:03

标签: python machine-learning classification knn

我想在控制台屏幕上打印category_report和roc_auc_score的值,而不更改我的准确性得分。我为其他分类器导入了一些库,请不要介意这些。

我试图将这些行插入到我的代码中:

from sklearn.metrics import classification_report
knn = KNeighborsClassifier()
y_pred = knn.predict(X_test)
print(classification_report(y_test,y_pred))

但是我遇到了“不适合的错误”

  

NotFittedError:此KNeighborsClassifier实例尚未安装。使用此方法之前,请使用适当的参数调用“适合”。

我希望

的输出
KNeighborsClassifier,Accuracy,88.70, 10.75;
roc_auc_score= 0. ...

   precision   recall  f1-score  support
0    0. ...     0. ..   0. ..     0. ..
1    0. ...     0. ..   0. ..     0. ..

但是实际输出是

KNeighborsClassifier,Accuracy,88.70, 10.75;

import numpy as np
import pandas as pd

data = pd.read_csv("MelanomaData.csv")

from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X =  pd.DataFrame(sc_X.fit_transform(data.drop(["Ouytput_1M"],axis = 1),),
        columns=['H_Mean', 'S_Mean', 'V_Mean', 'H_Max', 'S_Max', 'V_Max', 'H_Std', 'S_Std', 'V_Std',
       'Correlation', 'Energy', 'Homogeneity','Ouytput_1M'])

X.head()

y = data.Ouytput_1M

from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
from sklearn.ensemble import BaggingClassifier
from sklearn.naive_bayes import BernoulliNB
from sklearn.tree import ExtraTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.linear_model import LogisticRegression
from sklearn.svm import NuSVC
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.neighbors import  RadiusNeighborsClassifier
from sklearn.model_selection import cross_val_score

crossValidationNumber=3;


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=42)

names = ["\nKNeighborsClassifier"]

classifiers = [
    KNeighborsClassifier(2)]

results = {}
for name, clf in zip(names, classifiers):#The zip() function take iterables (can be zero or more), 
    #makes iterator that aggregates elements based on the iterables passed, and returns an iterator of tuples.
    scores = cross_val_score(clf, X_test, y_test, cv=crossValidationNumber)#10cv xtest and y_test

    results[name] = scores#results has scores of all classifier methods
for name, scores in results.items():


    print("%20s,Accuracy,%0.2f, %0.2f;" % (name, 100*scores.mean(), 100*scores.std() * 2))

0 个答案:

没有答案