博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
COMP7404 Machine Learing——Confusion Matrix & Precision/Recall/F1
阅读量:2136 次
发布时间:2019-04-30

本文共 4737 字,大约阅读时间需要 15 分钟。

Confusion Matrix

import pandas as pdimport numpy as npdf = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data', header=None)from sklearn.preprocessing import LabelEncoderfrom sklearn.model_selection import train_test_splitX = df.loc[:, 2:].valuesy = df.loc[:, 1].valuesle = LabelEncoder()y = le.fit_transform(y)X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, stratify=y, random_state=1)from sklearn.model_selection import GridSearchCVfrom sklearn.svm import SVCfrom sklearn.pipeline import make_pipelinefrom sklearn.preprocessing import StandardScalerfrom sklearn.model_selection import cross_val_scorepipe_svc = make_pipeline(StandardScaler(), SVC(random_state=1))from sklearn.metrics import confusion_matrixpipe_svc.fit(X_train, y_train)y_pred = pipe_svc.predict(X_test)confmat = confusion_matrix(y_true=y_test, y_pred=y_pred)print(confmat)

如果想要改变位置

confmat = confusion_matrix(y_true=y_test, y_pred=y_pred, labels=[1, 0])

 

 

import pandas as pdimport numpy as npdf = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data', header=None)from sklearn.preprocessing import LabelEncoderfrom sklearn.model_selection import train_test_splitX = df.loc[:, 2:].valuesy = df.loc[:, 1].valuesle = LabelEncoder()y = le.fit_transform(y)X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, stratify=y, random_state=1)from sklearn.model_selection import GridSearchCVfrom sklearn.svm import SVCfrom sklearn.pipeline import make_pipelinefrom sklearn.preprocessing import StandardScalerfrom sklearn.model_selection import cross_val_scorepipe_svc = make_pipeline(StandardScaler(), SVC(random_state=1))from sklearn.metrics import confusion_matrixpipe_svc.fit(X_train, y_train)y_pred = pipe_svc.predict(X_test)confmat = confusion_matrix(y_true=y_test, y_pred=y_pred)print(confmat)import matplotlib.pyplot as pltfig, ax = plt.subplots(figsize=(2.5, 2.5))ax.matshow(confmat, cmap=plt.cm.Blues, alpha=0.3)for i in range(confmat.shape[0]):    for j in range(confmat.shape[1]):        ax.text(x=j, y=i, s=confmat[i, j], va='center', ha='center')plt.xlabel('Predicted label')plt.ylabel('True label')plt.tight_layout()plt.show()

 

Precision/Recall/F1

import pandas as pdimport numpy as npdf = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data', header=None)from sklearn.preprocessing import LabelEncoderfrom sklearn.model_selection import train_test_splitX = df.loc[:, 2:].valuesy = df.loc[:, 1].valuesle = LabelEncoder()y = le.fit_transform(y)X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, stratify=y, random_state=1)from sklearn.model_selection import GridSearchCVfrom sklearn.svm import SVCfrom sklearn.pipeline import make_pipelinefrom sklearn.preprocessing import StandardScalerfrom sklearn.model_selection import cross_val_scorepipe_svc = make_pipeline(StandardScaler(), SVC(random_state=1))from sklearn.metrics import confusion_matrixpipe_svc.fit(X_train, y_train)y_pred = pipe_svc.predict(X_test)from sklearn.metrics import precision_score, recall_score, f1_scoreprint('Precision: %.3f' % precision_score(y_true=y_test, y_pred=y_pred))print('Recall: %.3f' % recall_score(y_true=y_test, y_pred=y_pred))print('F1: %.3f' % f1_score(y_true=y_test, y_pred=y_pred))

 

 

Scoring Metric(Precision/Recall/F1) in GridSearchCV
 

import pandas as pdimport numpy as npdf = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data', header=None)from sklearn.preprocessing import LabelEncoderfrom sklearn.model_selection import train_test_splitX = df.loc[:, 2:].valuesy = df.loc[:, 1].valuesle = LabelEncoder()y = le.fit_transform(y)X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, stratify=y, random_state=1)from sklearn.model_selection import GridSearchCVfrom sklearn.svm import SVCfrom sklearn.pipeline import make_pipelinefrom sklearn.preprocessing import StandardScalerfrom sklearn.model_selection import cross_val_scorepipe_svc = make_pipeline(StandardScaler(), SVC(random_state=1))from sklearn.metrics import confusion_matrixpipe_svc.fit(X_train, y_train)from sklearn.metrics import make_scorerfrom sklearn.metrics import precision_score, recall_score, f1_scorescorer = make_scorer(f1_score, pos_label=0)c_gamma_range = [0.01, 0.1, 1.0, 10.0]param_grid = [{'svc__C': c_gamma_range, 'svc__kernel': ['linear']},              {'svc__C': c_gamma_range, 'svc__gamma': c_gamma_range, 'svc__kernel': ['rbf']}]gs = GridSearchCV(estimator=pipe_svc, param_grid=param_grid, scoring=scorer, cv=10, n_jobs=-1)gs = gs.fit(X_train, y_train)print(gs.best_score_)print(gs.best_params_)

 

 

转载地址:http://emygf.baihongyu.com/

你可能感兴趣的文章
Windows下编译x264
查看>>
visual studio调试内存泄漏工具
查看>>
开源Faac实现PCM编码AAC
查看>>
Windows下wave API 音频采集
查看>>
借船过河:一个据说能看穿你的人性和欲望的心理测试
查看>>
AndroidStudio 导入三方库使用
查看>>
Ubuntu解决gcc编译报错/usr/bin/ld: cannot find -lstdc++
查看>>
解决Ubuntu14.04 - 16.10版本 cheese摄像头灯亮却黑屏问题
查看>>
解决Ubuntu 64bit下使用交叉编译链提示error while loading shared libraries: libz.so.1
查看>>
VS生成DLL文件供第三方调用
查看>>
Android Studio color和font设置
查看>>
Python 格式化打印json数据(展开状态)
查看>>
Centos7 安装curl(openssl)和libxml2
查看>>
Centos7 离线安装RabbitMQ,并配置集群
查看>>
Centos7 or Other Linux RPM包查询下载
查看>>
运行springboot项目出现:Type javax.xml.bind.JAXBContext not present
查看>>
Java中多线程向mysql插入同一条数据冲突问题
查看>>
Idea Maven项目使用jar包,添加到本地库使用
查看>>
FastDFS集群架构配置搭建(转载)
查看>>
HTM+CSS实现立方体图片旋转展示效果
查看>>