转:sklearn包——混淆矩阵、分类报告等自动生成

来源:互联网 发布:股票网络销售违法嘛 编辑:程序博客网 时间:2024/06/08 16:55

preface:做着最近的任务,对数据处理,做些简单的提特征,用机器学习算法跑下程序得出结果,看看哪些特征的组合较好,这一系列流程必然要用到很多函数,故将自己常用函数记录上。应该说这些函数基本上都会用到,像是数据预处理,处理完了后特征提取、降维、训练预测、通过混淆矩阵看分类效果,得出报告。

1.输入

从数据集开始,提取特征转化为有标签的数据集,转为向量。拆分成训练集和测试集,这里不多讲,在上一篇博客中谈到用StratifiedKFold()函数即可。在训练集中有data和target开始。

2.处理

[python] view plain copy
  1. def my_preprocessing(train_data):  
  2.     from sklearn import preprocessing  
  3.     X_normalized = preprocessing.normalize(train_data ,norm = "l2",axis=0)#使用l2范式,对特征列进行正则  
  4.     return X_normalized  
  5.   
  6. def my_feature_selection(data, target):  
  7.     from sklearn.feature_selection import SelectKBest  
  8.     from sklearn.feature_selection import chi2  
  9.     data_new = SelectKBest(chi2, k= 50).fit_transform(data,target)  
  10.     return data_new  
  11.   
  12. def my_PCA(data):#data without target, just train data, withou train target.  
  13.     from sklearn import decomposition  
  14.     pca_sklearn = decomposition.PCA()  
  15.     pca_sklearn.fit(data)  
  16.     main_var = pca_sklearn.explained_variance_  
  17.     print sum(main_var)*0.9  
  18.     import matplotlib.pyplot as plt  
  19.     n = 15  
  20.     plt.plot(main_var[:n])  
  21.     plt.show()  
  22.   
  23. def clf_train(data,target):  
  24.     from sklearn import svm  
  25.     #from sklearn.linear_model import LogisticRegression  
  26.     clf = svm.SVC(C=100,kernel="rbf",gamma=0.001)  
  27.     clf.fit(data,target)  
  28.   
  29.     #clf_LR = LogisticRegression()  
  30.     #clf_LR.fit(x_train, y_train)  
  31.     #y_pred_LR = clf_LR.predict(x_test)  
  32.     return clf  
  33.   
  34. def my_confusion_matrix(y_true, y_pred):  
  35.     from sklearn.metrics import confusion_matrix  
  36.     labels = list(set(y_true))  
  37.     conf_mat = confusion_matrix(y_true, y_pred, labels = labels)  
  38.     print "confusion_matrix(left labels: y_true, up labels: y_pred):"  
  39.     print "labels\t",  
  40.     for i in range(len(labels)):  
  41.         print labels[i],"\t",  
  42.     print   
  43.     for i in range(len(conf_mat)):  
  44.         print i,"\t",  
  45.         for j in range(len(conf_mat[i])):  
  46.             print conf_mat[i][j],'\t',  
  47.         print   
  48.     print   
  49.   
  50. def my_classification_report(y_true, y_pred):  
  51.     from sklearn.metrics import classification_report  
  52.     print "classification_report(left: labels):"  
  53.     print classification_report(y_true, y_pred)  

my_preprocess()函数:

主要使用sklearn的preprocessing函数中的normalize()函数,默认参数为l2范式,对特征列进行正则处理。即每一个样例,处理标签,每行的平方和为1.

my_feature_selection()函数:

使用sklearn的feature_selection函数中SelectKBest()函数和chi2()函数,若是用词袋提取了很多维的稀疏特征,有必要使用卡方选取前k个有效的特征。

my_PCA()函数:

主要用来观察前多少个特征是主要特征,并且画图。看看前多少个特征占据主要部分。

clf_train()函数:

可用多种机器学习算法,如SVM, LR, RF, GBDT等等很多,其中像SVM需要调参数的,有专门调试参数的函数如StratifiedKFold()(见前几篇博客)。以达到最优。

my_confusion_matrix()函数:

主要是针对预测出来的结果,和原来的结果对比,算出混淆矩阵,不必自己计算。其对每个类别的混淆矩阵都计算出来了,并且labels参数默认是排序了的。

my_classification_report()函数:

主要通过sklearn.metrics函数中的classification_report()函数,针对每个类别给出详细的准确率、召回率和F-值这三个参数和宏平均值,用来评价算法好坏。另外ROC曲线的话,需要是对二分类才可以。多类别似乎不行。


主要参考sklearn官网
阅读全文
0 0