python sklearn-04:逻辑回归及其效果评估

来源:互联网 发布:北京数据库培训机构 编辑:程序博客网 时间:2024/06/03 20:24

scikit-learn官方文档:http://scikit-learn.org/stable/tutorial/

译文:https://muxuezi.github.io/posts/4-from-linear-regression-to-logistic-regression.html

 

目录:

1.二元分类:

   >>逻辑回归

   >>网格搜索

2.多元分类

3.多标签分类

 

 

1.二元分类:

>>逻辑回归

 逻辑回归是用来做分类任务的。分类任务的目标是找一个函数,把观测值匹配到相关的类和标签上。学习算法必须用成对的特征向量和对应的标签来估计匹配函数的参数,从而实现更好的分类效果。

在二元分类(binary classification)中,分类算法必须把一个实例配置两个类别。二元分类案例包括,预测患者是否患有某种疾病,音频中是否含有人声,杜克大学男子篮球队在NCAA比赛中第一场的输赢。

多元分类中,分类算法需要为每个实例都分类一组标签。

 

在逻辑回归里,响应变量描述了类似于掷一个硬币结果为正面的概率。如果响应变量等于或超过了指定的临界值,预测结果就是正面,否则预测结果就是反面。响应变量是一个像线性回归中的解释变量构成的函数表示,称为逻辑函数(logistic function)。一个值在{0,1}之间的逻辑函数如下所示:

F(t)=1/(1+e(-t))

Python代码  收藏代码
  1. import matplotlib.pyplot as plt  
  2. from matplotlib.font_manager import FontProperties  
  3. font = FontProperties(fname=r"c:\windows\fonts\msyh.ttc", size=10)  
  4.   
  5. import numpy as np  
  6. plt.figure()  
  7. plt.axis([-6601])  
  8. plt.grid(True)  
  9. X = np.arange(-6,6,0.1)  
  10. y = 1 / (1 + np.e ** (-X))  
  11. plt.plot(X, y, 'b-');  

 

 

在逻辑回归中, 是解释变量的线性组合,公式如下:

对数函数(logit function)是逻辑函数的逆运算:

定义了逻辑回归的模型之后,用它来完成一个分类任务。

 

#垃圾邮件分类

二元分类问题就是垃圾邮件分类(spam classification)。这里,分类垃圾短信。先用TF-IDF算法来抽取短信的特征向量,然后用逻辑回归分类。

数据源:UCI Machine Learning Repository(http://archive.ics.uci.edu/ml/datasets/SMS+Spam+Collection)的短信垃圾分类数据集(SMS SpamClassification Data Set)。

Python代码  收藏代码
  1. #首先,用Pandas做一些描述性统计:  
  2.   
  3. import pandas as pd  
  4. from sklearn.feature_extraction.text import TfidfVectorizer  
  5. from sklearn.linear_model.logistic import  LogisticRegression  
  6. from sklearn.cross_validation import train_test_split  
  7.   
  8. df = pd.read_csv(r'D:\每日工作\学习笔记\test\mlslpic\SMSSpamCollection', delimiter='\t', header=None)  
  9.   
  10. print (df.head())  
  11. print ('spam短信数量:',df[df[0]== 'spam'][0].count())  
  12. print ('spam短信数量:',df[df[0]== 'ham'][0].count())  
  13.   
  14. out:  
  15.       0                                                  1  
  16. 0   ham  Go until jurong point, crazy.. Available only ...  
  17. 1   ham                      Ok lar... Joking wif u oni...  
  18. 2  spam  Free entry in 2 a wkly comp to win FA Cup fina...  
  19. 3   ham  U dun say so early hor... U c already then say...  
  20. 4   ham  Nah I don't think he goes to usf, he lives aro...  
  21.   
  22. spam短信数量: 747  
  23. ham短信数量: 4825     
  24.   
  25.   
  26. import pandas as pd  
  27. from sklearn.feature_extraction.text import TfidfVectorizer  
  28. from sklearn.linear_model.logistic import  LogisticRegression  
  29. from sklearn.cross_validation import train_test_split, cross_val_score  
  30.   
  31. #用pandas加载数据.csv文件,然后用train_test_split分成训练集(75%)和测试集(25%):  
  32.   
  33. df = pd.read_csv(r'D:\每日工作\学习笔记\test\mlslpic\SMSSpamCollection', delimiter='\t', header=None)  
  34.   
  35. X_train_raw,X_test_raw,y_train,y_test = train_test_split(df[1],df[0])  
  36.   
  37. #用TF-IDF算法来抽取短信的特征向量  
  38. vectorizer = TfidfVectorizer()  
  39. X_train = vectorizer.fit_transform(X_train_raw)  
  40. X_test = vectorizer.transform(X_test_raw)  
  41.   
  42. #用逻辑回归分类  
  43. classifer = LogisticRegression()  
  44. classifer.fit(X_train,y_train)  
  45. predictions = classifer.predict(X_test)  
  46.   
  47. for i,predictions in enumerate(predictions[-5:]):  
  48.     print ('预测类型:%s. 信息: %s' %(predictions,X_test_raw.iloc[i]))  
  49.   
  50.       
  51. out:  
  52. 预测类型:ham. 信息: MOON has come to color your dreams, STARS to make them musical and my SMS to give you warm and Peaceful Sleep. Good Night  
  53. 预测类型:ham. 信息: Your B4U voucher w/c 27/03 is MARSMS. Log onto www.B4Utele.com for discount credit. To opt out reply stop. Customer care call 08717168528  
  54. 预测类型:ham. 信息: Adult 18 Content Your video will be with you shortly  
  55. 预测类型:ham. 信息: Had your mobile 11mths ? Update for FREE to Oranges latest colour camera mobiles & unlimited weekend calls. Call Mobile Upd8 on freefone 08000839402 or 2StopTxt  
  56. 预测类型:ham. 信息: Well, I have to leave for my class babe ... You never came back to me ... :-( ... Hope you have a nice sleep, my love  
  57.   
  58. #效果评估:  
  59. #准确率:scikit-learn提供了accuracy_score来计算:LogisticRegression.score()  
  60. #准确率是分类器预测正确性的比例,但是并不能分辨出假阳性错误和假阴性错误  
  61. scores = cross_val_score(classifer,X_train,y_train,cv=5)  
  62. print ('准确率:',np.mean(scores),scores)  
  63.   
  64. out:  
  65. 准确率: 0.957646620634 [ 0.96052632  0.95933014  0.95454545  0.95095694  0.96287425]  
  66.   
  67. #精确率和召回率:  
  68. #精确率是指分类器预测出的垃圾短信中真的是垃圾短信的比例,P=TP/(TP+FP)  
  69. #召回率在医学上也叫做灵敏度,在本例中知所有真的垃圾短信被分类器正确找出来的比例,R=TP/(TP+FN)  
  70. #precisions = cross_val_score(classifer, X_train, y_train, cv=5, scoring='precision')  
  71. #print ('精确率:',np.mean(precisions),precisions)  
  72.   
  73. import numpy as np  
  74. import pandas as pd  
  75. from sklearn.feature_extraction.text import TfidfVectorizer  
  76. from sklearn.linear_model.logistic import LogisticRegression  
  77. from sklearn.cross_validation import train_test_split, cross_val_score  
  78. df = pd.read_csv('mlslpic/sms.csv')  
  79. X_train_raw, X_test_raw, y_train, y_test = train_test_split(df['message']  
  80. , df['label'])  
  81. vectorizer = TfidfVectorizer()  
  82. X_train = vectorizer.fit_transform(X_train_raw)  
  83. X_test = vectorizer.transform(X_test_raw)  
  84. classifier = LogisticRegression()  
  85. classifier.fit(X_train, y_train)  
  86.   
  87. scores = cross_val_score(classifier, X_train, y_train, cv=5)  
  88. print('准确率:',np.mean(scores), scores)  
  89. precisions = cross_val_score(classifier, X_train, y_train, cv=5, scoring='precision')  
  90. print('精确率:', np.mean(precisions), precisions)  
  91. recalls = cross_val_score(classifier, X_train, y_train, cv=5, scoring='recall')  
  92. print('召回率:', np.mean(recalls), recalls)  
  93.   
  94.   
  95. out:  
  96. 准确率: 0.958373205742 [ 0.96291866 0.95334928 0.95813397 0.96172249 0.95574163]  
  97. 精确率: 0.99217372134 [ 0.9875 0.98571429 1. 1. 0.98765432]  
  98. 召回率: 0.672121212121 [ 0.71171171 0.62162162 0.66363636 0.63636364 0.72727273]  
  99. #分类器精确率99.2%,分类器预测出的垃圾短信中99.2%都是真的垃圾短信。召回率比较低67.2%,就是说真实的垃圾短信中,32.8%被当作正常短信了,没有被识别出来。  
  100.   
  101.   
  102. #综合评价指标  
  103. f1s = cross_val_score(classifier, X_train, y_train, cv=5, scoring='f1')  
  104. print('综合评价指标:', np.mean(f1s), f1s)  
  105. #综合评价指标是80%。由于精确率和召回率的差异比较小,所以综合评价指标的罚值也比较小。有时也会用F0.5和F2,表示精确率权重大于召回率,或召回率权重大于精确率。  
  106.   
  107. #ROC AUC  
  108. #ROC曲线(Receiver Operating Characteristic,ROC curve)可以用来可视化分类器的效果。和准确率不同,ROC曲线对分类比例不平衡的数据集不敏感,ROC曲线显示的是对超过限定阈值的所有预测结果的分类器效果。ROC曲线画的是分类器的召回率与误警率(fall-out)的曲线。误警率也称假阳性率,是所有阴性样本中分类器识别为阳性的样本所占比例:  
  109. #F=FP/(TN+FP) AUC是ROC曲线下方的面积,它把ROC曲线变成一个值,表示分类器随机预测的效果. from sklearn.metrics import roc_curve, auc  
  110. from sklearn.metrics import roc_curve, auc  
  111. predictions = classifier.predict_proba(X_test)  
  112. false_positive_rate, recall, thresholds = roc_curve(y_test, predictions[:, 1])  
  113. roc_auc = auc(false_positive_rate, recall)  
  114. plt.title('Receiver Operating Characteristic')  
  115. plt.plot(false_positive_rate, recall, 'b', label='AUC = %0.2f' % roc_auc)  
  116. plt.legend(loc='lower right')  
  117. plt.plot([01], [01], 'r--')  
  118. plt.xlim([0.01.0])  
  119. plt.ylim([0.01.0])  
  120. plt.ylabel('Recall')  
  121. plt.xlabel('Fall-out')  
  122. plt.show()  

 

>>网格搜索

网格搜索(Grid search)就是用来确定最优超参数的方法。其原理就是选取可能的参数不断运行模型获取最佳效果。网格搜索用的是穷举法,其缺点在于即使每个超参数的取值范围都很小,计算量也是巨大的。不过这是一个并行问题,参数与参数彼此独立,计算过程不需要同步,所有很多方法都可以解决这个问题。scikit-learn有GridSearchCV()函数解决这个问题:

Python代码  收藏代码
  1. import pandas as pd  
  2. from sklearn.feature_extraction.text import TfidfVectorizer  
  3. from sklearn.linear_model.logistic import LogisticRegression  
  4. from sklearn.grid_search import GridSearchCV  
  5. from sklearn.pipeline import Pipeline  
  6. from sklearn.cross_validation import train_test_split  
  7. from sklearn.metrics import precision_score, recall_score, accuracy_score  
  8. pipeline = Pipeline([  
  9. ('vect', TfidfVectorizer(stop_words='english')),  
  10. ('clf', LogisticRegression())  
  11. ])  
  12. parameters = {  
  13. 'vect__max_df': (0.250.50.75),  
  14. 'vect__stop_words': ('english'None),  
  15. 'vect__max_features': (2500500010000None),  
  16. 'vect__ngram_range': ((11), (12)),  
  17. 'vect__use_idf': (TrueFalse),  
  18. 'vect__norm': ('l1''l2'),  
  19. 'clf__penalty': ('l1''l2'),  
  20. 'clf__C': (0.010.1110),  
  21. }  
  22. grid_search = GridSearchCV(pipeline, parameters, n_jobs=-1, verbose=1, sc  
  23. oring='accuracy', cv=3)  
  24. df = pd.read_csv('mlslpic/sms.csv')  
  25. X, y, = df['message'], df['label']  
  26. X_train, X_test, y_train, y_test = train_test_split(X, y)  
  27. grid_search.fit(X_train, y_train)  
  28. print('最佳效果:%0.3f' % grid_search.best_score_)  
  29. print('最优参数组合:')  
  30. best_parameters = grid_search.best_estimator_.get_params()  
  31. for param_name in sorted(parameters.keys()):  
  32. print('\t%s: %r' % (param_name, best_parameters[param_name]))  
  33. predictions = grid_search.predict(X_test)  
  34. print('准确率:', accuracy_score(y_test, predictions))  
  35. print('精确率:', precision_score(y_test, predictions))  
  36. print('召回率:', recall_score(y_test, predictions))  
  37.   
  38.   
  39. out:  
  40. [Parallel(n_jobs=-1)]: Done 1 jobs | elapsed: 1.8s  
  41. [Parallel(n_jobs=-1)]: Done 50 jobs | elapsed: 10.1s  
  42. [Parallel(n_jobs=-1)]: Done 200 jobs | elapsed: 27.4s  
  43. [Parallel(n_jobs=-1)]: Done 450 jobs | elapsed: 54.2s  
  44. [Parallel(n_jobs=-1)]: Done 800 jobs | elapsed: 1.6min  
  45. [Parallel(n_jobs=-1)]: Done 1250 jobs | elapsed: 2.4min  
  46. [Parallel(n_jobs=-1)]: Done 1800 jobs | elapsed: 3.4min  
  47. [Parallel(n_jobs=-1)]: Done 2450 jobs | elapsed: 4.6min  
  48. [Parallel(n_jobs=-1)]: Done 3200 jobs | elapsed: 6.0min  

 

GridSearchCV()函数的参数有待评估模型pipeline,超参数词典parameters和效果评价指

标scoring。n_jobs是指并发进程最大数量,设置为-1表示使用所有CPU核心进程。经过网格计算后的超参数在训练集中取得了很好的效

果。

 

2.多类分类:

scikit-learn用one-vs.-all或one-vs.-the-rest方法实现多类分类,就是把多类中的每个类都作为二元分类处理。分类器预测样本不同类型,将具有最大置信水平的类型作为样本类型。LogisticRegression()通过one-vs.-all策略支持多类分类。

数据集可以从kaggle (https://www.kaggle.com/c/sentiment-analysis-on-movie-reviews)

 

用烂番茄(Rotten Tomatoes)网站影评短语数据对电影进行评价。每个影评可以归入下面5个类项:不给力(negative),不太给力(somewhat negative),中等(neutral),有点给力(somewhat positive), 给力(positive)。解释变量不会总是直白的语言,因为影评内容千差万别,有讽刺的,否定的,以及其他语义的表述,语义并不直白。

Python代码  收藏代码
  1. import pandas as pd  
  2. from sklearn.feature_extraction.text import TfidfVectorizer  
  3. from sklearn.linear_model.logistic import LogisticRegression  
  4. from sklearn.cross_validation import train_test_split  
  5. from sklearn.metrics import classification_report, accuracy_score, confusion_matrix  
  6. from sklearn.pipeline import Pipeline  
  7. from sklearn.grid_search import GridSearchCV  
  8. import zipfile  
  9. pipeline = Pipeline([  
  10. ('vect', TfidfVectorizer(stop_words='english')),  
  11. ('clf', LogisticRegression())  
  12. ])  
  13. parameters = {  
  14. 'vect__max_df': (0.250.5),  
  15. 'vect__ngram_range': ((11), (12)),  
  16. 'vect__use_idf': (TrueFalse),  
  17. 'clf__C': (0.1110),  
  18. }  
  19. z = zipfile.ZipFile(r'D:\每日工作\学习笔记\test\mlslpic\train.tsv.zip')  
  20. df = pd.read_csv(z.open(z.namelist()[0]), header=0, delimiter='\t')  
  21. X, y = df['Phrase'], df['Sentiment'].as_matrix()  
  22. X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.5)  
  23. grid_search = GridSearchCV(pipeline, parameters, n_jobs=3, verbose=1, scoring='accuracy')  
  24. grid_search.fit(X_train, y_train)  
  25. print('最佳效果:%0.3f' % grid_search.best_score_)  
  26. print('最优参数组合:')  
  27. best_parameters = grid_search.best_estimator_.get_params()  
  28. for param_name in sorted(parameters.keys()):  
  29.     print('\t%s: %r' % (param_name, best_parameters[param_name]))  
  30.       
  31. out:  
  32. Fitting 3 folds for each of 24 candidates, totalling 72 fits  
  33. [Parallel(n_jobs=3)]: Done  44 tasks      | elapsed:  1.2min  
  34. [Parallel(n_jobs=3)]: Done  72 out of  72 | elapsed:  3.1min finished  
  35. 最佳效果:0.618  
  36. 最优参数组合:  
  37.         clf__C: 10  
  38.         vect__max_df: 0.25  
  39.         vect__ngram_range: (12)  
  40.         vect__use_idf: False  
  41.           
  42.           
  43.   
  44. 多类分类效果评估      
  45. predictions = grid_search.predict(X_test)  
  46. print('准确率:', accuracy_score(y_test, predictions))  
  47. print('混淆矩阵:', confusion_matrix(y_test, predictions))  
  48. print('分类报告:', classification_report(y_test, predictions))  
  49.   
  50. out:  
  51. 准确率: 0.63526848648  
  52. 混淆矩阵: [[ 1144  1747   597    74    11]  
  53.  [  909  6011  6093   561    32]  
  54.  [  228  3185 32607  3667   162]  
  55.  [   23   399  6476  8203  1267]  
  56.  [    2    40   479  2508  1605]]  
  57. 分类报告:              precision    recall  f1-score   support  
  58.   
  59.           0       0.50      0.32      0.39      3573  
  60.           1       0.53      0.44      0.48     13606  
  61.           2       0.70      0.82      0.76     39849  
  62.           3       0.55      0.50      0.52     16368  
  63.           4       0.52      0.35      0.42      4634  
  64.   
  65. avg / total       0.62      0.64      0.62     78030  

 

 

3.多标签分类:

多标签分类(multi-label classification)。每个样本可以拥有全部类型的一部分类型。一般有两种解决方法:

问题转化方法(Problem transformation)可以将多标签问题转化成单标签问题。 

方法1:训练集里面每个样本通过幂运算转换成单标签。这种幂运算虽然直观,但是并不实用,因为这样做多出来的标

签只有一小部分样本会用到。而且,这些标签只能在训练集里面学习这些类似,在测试集中依然无法使用。

方法2:每个标签都用二元分类处理。每个标签的分类器都预测样本是否属于该标签。这个问题确保了单标签问题和多标签问题有同样的训练集,只是忽略了标签之间的关联关系。

 

多标签分类效果评估:

最常用的手段是汉明损失函数(Hamming

loss)和杰卡德相似度(Jaccard similarity)。

汉明损失函数表示错误标签的平均比例,是一个函数,当预测全部正确,即没有错误标签时,值为0。

杰卡德相似度或杰卡德相指数(Jaccardindex),是预测标签和真实标签的交集数量除以预测标签和真实标签的并集数量。其值在{0,1}之间,J(Predicted,True)=|Predicted ∩ True|/|Predicted ∪ True|

 

Python代码  收藏代码
  1. import numpy as np  
  2. from sklearn.metrics import hamming_loss, jaccard_similarity_score  
  3. print(hamming_loss(np.array([[0.01.0], [1.01.0]]), np.array([[0.01.0], [1.01.0]])))  
  4. print(hamming_loss(np.array([[0.01.0], [1.01.0]]), np.array([[1.01.0], [1.01.0]])))  
  5. print(hamming_loss(np.array([[0.01.0], [1.01.0]]), np.array([[1.01.0], [0.01.0]])))  
  6. print(jaccard_similarity_score(np.array([[0.01.0], [1.01.0]]), np.array([[0.01.0], [1.01.0]])))  
  7. print(jaccard_similarity_score(np.array([[0.01.0], [1.01.0]]), np.array([[1.01.0], [1.01.0]])))  
  8. print(jaccard_similarity_score(np.array([[0.01.0], [1.01.0]]), np.array([[1.01.0], [0.01.0]])))  
  9.   
  10. out:  
  11. 0.0  
  12. 0.25  
  13. 0.5  
  14. 1.0  
  15. 0.75  
  16. 0.5  

原创粉丝点击