Scikit-plot画图

在机器学习过程中画图是一个重要的步骤,例如在分类任务中需要画P-R曲线,AUC曲线,混淆曲线等,使用matpotlib, Seaborn等类库作图需要多写几行代码,例如设置titlexlimylimlengend等,如果有一个工具库可以封装这些操作的话可以帮助我们节省时间,提升开发效率,从而专注在算法/业务的改进上。

安装

conda install -c conda-forge scikit-plot
# 或者
pip install scikit-plot

导包

from sklearn.datasets import load_digits, load_breast_cancer, load_iris, make_classification
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.cluster import KMeans
from sklearn.svm import LinearSVC
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import scikitplot as skplt
import warningswarnings.filterwarnings('ignore')

混淆矩阵

X, y = load_digits(return_X_y=True)
rf = RandomForestClassifier()
rf.fit(X, y)
preds = rf.predict(X)
skplt.metrics.plot_confusion_matrix(y_true=y, y_pred=preds)
plt.show()


ROC曲线

X, y = load_digits(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
nb = GaussianNB()
nb.fit(X_train, y_train)
predicted_probas = nb.predict_proba(X_test)skplt.metrics.plot_roc(y_test, predicted_probas)
plt.show()


KS统计

X, y = load_breast_cancer(return_X_y=True)
lr = LogisticRegression()
lr.fit(X, y)
probas = lr.predict_proba(X)
skplt.metrics.plot_ks_statistic(y_true=y, y_probas=probas)
plt.show()


Precision-Recall

X, y = load_digits(return_X_y=True)
nb = GaussianNB()
nb.fit(X, y)
probas = nb.predict_proba(X)
skplt.metrics.plot_precision_recall(y_true=y, y_probas=probas)
plt.show()

聚类

X, y = load_iris(return_X_y=True)
kmeans = KMeans(n_clusters=4, random_state=1)
cluster_labels = kmeans.fit_predict(X)
skplt.metrics.plot_silhouette(X, cluster_labels)
plt.show()

Calibration Curve(校准曲线)

X, y = make_classification(n_samples=100000, n_features=20,n_informative=2, n_redundant=2,random_state=20)X_train, y_train, X_test, y_test = X[:1000], y[:1000], X[1000:], y[1000:]rf_probas = RandomForestClassifier().fit(X_train, y_train).predict_proba(X_test)
lr_probas = LogisticRegression().fit(X_train, y_train).predict_proba(X_test)
nb_probas = GaussianNB().fit(X_train, y_train).predict_proba(X_test)
sv_scores = LinearSVC().fit(X_train, y_train).decision_function(X_test)probas_list = [rf_probas, lr_probas, nb_probas, sv_scores]
clf_names=['Random Forest','Logistic Regression','Gaussian Naive Bayes','Support Vector Machine']skplt.metrics.plot_calibration_curve(y_test,probas_list=probas_list,clf_names=clf_names,n_bins=10)
plt.show()

Cumulative Gain(累计增益)

根据标签和分数/概率生成累积增益图

X, y = load_breast_cancer(return_X_y=True)
lr = LogisticRegression()
lr.fit(X, y)
probas = lr.predict_proba(X)
skplt.metrics.plot_cumulative_gain(y_true=y, y_probas=probas)
plt.show()


Lift Curve(提升曲线)

从标签和分数/概率生成提升曲线

X, y = load_breast_cancer(return_X_y=True)
lr = LogisticRegression()
lr.fit(X, y)
probas = lr.predict_proba(X)
skplt.metrics.plot_lift_curve(y_true=y, y_probas=probas)
plt.show()

Learning Curve(学习曲线)

X, y = load_breast_cancer(return_X_y=True)
rf = RandomForestClassifier()
skplt.estimators.plot_learning_curve(rf, X, y)
plt.show()

Feature Importances(特征重要性)

X, y = load_iris(return_X_y=True)
rf = RandomForestClassifier()
rf.fit(X, y)
skplt.estimators.plot_feature_importances(rf,feature_names=['petal length','petal width','sepal length','sepal width'])
plt.show()

Elbow Curve

为 KMeans 聚类绘制不同 K 值的肘部曲线。

X, y = load_iris(return_X_y=True)
kmeans = KMeans(random_state=1)
skplt.cluster.plot_elbow_curve(kmeans, X, cluster_ranges=range(1, 11))
plt.show()


PCA Component Variance

绘制 PCA 组件的解释方差比。

X, y = load_digits(return_X_y=True)
pca = PCA(random_state=1)
pca.fit(X)
skplt.decomposition.plot_pca_component_variance(pca)
plt.show()


PCA 2d Projection

在给定数据集上绘制 PCA 的二维投影。

X, y = load_digits(return_X_y=True)
pca = PCA(random_state=1)
pca.fit(X)
skplt.decomposition.plot_pca_2d_projection(pca, X, y)
plt.show()


Scikit-plot画图相关推荐

  1. matlab plot画图指定线型和颜色

    matlab plot画图指定线型和颜色 plot(x,y1,'.b'); %b代表蓝色,.代表点; plot(x,y2,'-g'); %g代表绿色,-代表线

  2. MATLAB plot画图后横轴去除空白

    MATLAB plot画图后横轴去除空白 MATLAB plot原来画图都是填充满的,今天不知道怎么了,横轴留有一部分空白,看起来十分不美观,可以修改一下上下限使其美观一些. 方法 x轴上下限设定 x ...

  3. MATLAB 利用plot 画图,加标题,保存图片

    MATLAB plot 画图,加标题,保存图片 plot(x,y,'.'); %画图 title("标题"); %设置标题 xlabel("X轴"); %设置X ...

  4. 为什么python画不了图-解决python中使用plot画图,图不显示的问题

    python matplotlib画的图怎么显示不出来 初学python,matplotlib库画图不显示分享助别熬夜了,你等不来的人,等到多晚都不会来的. 见图.python matplotlib ...

  5. pandas合并concatmerge和plot画图

    3.6,3.7pandas合并concat&merge 头文件: import pandas as pd import numpy as np concat基础合并用法 df1= pd.Dat ...

  6. python怎么画参数函数图像_详解pandas.DataFrame.plot() 画图函数

    首先看官网的DataFrame.plot( )函数 DataFrame.plot(x=None, y=None, kind='line', ax=None, subplots=False, share ...

  7. maltab利用plot画图后更改线条颜色

    在matlab中,利用plot画图默认好像只有八种颜色,分别为:b--蓝色,r--红色,m--品红,k--黑色,g--绿色,c--青色,y--黄色,w--白色.有时候我们画的线条比较多,这几种颜色可能 ...

  8. Matlab plot画图 坐标字体、字号、范围、间隔等的设置

    Matlab plot画图 坐标字体.字号.范围.间隔等的设置 MATLAB 坐标的数字.范围.间隔调整 matlab绘图的时候只用plot函数出来的图不一定符合自己最想要的格式, 经常要对坐标的数字 ...

  9. python画图程序没有图_解决python中使用plot画图,图不显示的问题

    解决python中使用plot画图,图不显示的问题 对以下数据画图结果图不显示,修改过程如下 df3 = {'chinese':109, 'American':88, 'German': 66, 'K ...

  10. 【Python】matplotlib.plot画图横坐标混乱及间隔处理

    今天用matplotlib.plot画折线图图时发现横坐标并没有按顺序排列,出现了混乱的情况,导致图很乱,查了好多方法发现是数据类型的原因(最近我出现的好多问题都是因为数据类型不对,下次出问题要记得用 ...

最新文章

  1. Maltego发布新版本4.2.18
  2. hdu1174(3维射线与圆是否相交)
  3. python中表头格式错误导入_python读csv文件时指定行为表头或无表头的方法
  4. 字节跳动---特征提取
  5. moxy json介绍_MOXy作为您的JAX-RS JSON提供程序–服务器端
  6. linux进程管理类命令大全,Linux进程管理类命令
  7. Linux 指令的分类 (man page 可查看)
  8. SECS/GEM series: Protocol Layer
  9. FreeRTOS的HOOK,以及(23)FreeRTOS 空闲任务分析
  10. unity3d 资源网站(持续更新中。。。)
  11. CCF系列题解--2015年9月第二题 日期计算
  12. 数据分析 - pandas(7)
  13. '命名空间xxx中不存在类型或命名空间名xx(是否缺少程序集引用)'-异常报错的原因
  14. 双光子荧光成像_为什么双光子成像如此重要?
  15. 滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(10月31日~11月6日)...
  16. 吴恩达亲述:如何高效阅读论文,开启一个新的领域!
  17. SpringBoot日志文件
  18. options should NOT have additional properties
  19. 从零开始学习Windows WDF驱动程序开发
  20. H.266帧内预测:位置决定的帧内预测组合(PDPC)

热门文章

  1. JQuery序列化和反序列化
  2. OpenCV — 人脸识别
  3. 数学建模 | MATLAB学习 | 插值 一维插值函数、三次样条插值
  4. 利用 conda install --use-local 安装 解决 Tensorflow: illegal instruction (core dumped)
  5. uniapp image 图片自适应
  6. Windows脚本:打开浏览器访问任意网址
  7. 教你如何用ffmpeg处理音频格式转换(标贝科技)
  8. 这些开源项目,值得收藏深入研究
  9. PTA: 6-8 剩余不足 (10分)(c语言)
  10. 转行软件开发的通用学习路径(转自刘校长亲笔)