2元分类代码示例

# -*- coding: utf-8 -*-


import sys


#载入pandas模块
import pandas as pd

#载入 numpy

import numpy

#加载 sklearn.feature_extraction.text.TfidfVectorizer 
from sklearn.feature_extraction.text import TfidfVectorizer


#加载 sklearn.linear_model.logistic.LogisticRegression
from sklearn.linear_model.logistic import LogisticRegression


#加载 sklearn.cross_validation.train_test_split
from sklearn.cross_validation import train_test_split


#加载 sklearn.cross_validation.cross_val_score
from sklearn.cross_validation import cross_val_score

#加载sklearn.metrics 相关函数   
from sklearn.metrics import precision_score, recall_score,\
f1_score, make_scorer, roc_curve, auc,classification_report,\
accuracy_score, confusion_matrix

#载入matplotlib.pyplot
import matplotlib.pyplot as plt
#载入网格搜索相关模块
from sklearn.grid_search import GridSearchCV

#载入pipeline不同参数组合做交叉验证步骤
from sklearn.pipeline import Pipeline


def wanggesousuo():
    #加载数据 没有有header  且以\t 为定界符  header 用0,1,2,3 表示
    df=pd.read_csv('SMSSpamCollection',delimiter='\t',header=None,names=['label','message'])




    #定义管道
    pipeline = Pipeline([
        ('vect',TfidfVectorizer()),
        ('clf',LogisticRegression())
        ])

    #设置管道参数
    parameters = {
    #定义TfidfVectorizer max_df 意义为 设置的参数构造语料库的关键词集
    #小数点则为%比 某个词的document frequence大于max_df 则不会成为关键词
    'vect__max_df': (0.25, 0.5, 0.75),
    #定义TfidfVectorizer  设为english将使用内置的英语停用词  若为None 则根据max_df自建停用词
    'vect__stop_words': ('english', None),
    #定义TfidfVectorizer  对所有关键词的term frequency进行降序排序  取前N个作为关键词
    'vect__max_features': (2500, 5000, 10000, None),
    #定义TfidfVectorizer  切词的长度
    'vect__ngram_range': ((1, 1), (1, 2)),
    #定义TfidfVectorizer 若为false 不使用tf*idf相当于 使用CountVectorizer 一个效果
    'vect__use_idf': (True, False),
    #定义TfidfVectorizer norm只有2中可能 1为l1 2为l2 若为l2 整行权值将归一化  l1也为none
    'vect__norm': ('l1', 'l2'),
    #定义LogisticRegression 只有2中可能 l1 l2  'sag' and 'lbfgs' solvers support only l2 penalties.
    'clf__penalty': ('l1', 'l2'),
    #定义LogisticRegression C参数  默认为1   必须是一个浮点型  逆向正则有关
    'clf__C': (0.01, 0.1, 1, 10),
    }

    #使用网格搜索  n_jobs指定并发进程最大数量 设置为-1表示使用所有CPU核心进程
    grid_search =GridSearchCV(pipeline, parameters, n_jobs=1, verbose=1, scoring='accuracy', cv=3)

    #分割测试集  训练集
    x_train,x_test,y_train,y_test=train_test_split(df['message'],df['label'],random_state=1);


    #使用网格搜索 开始训练
    grid_search.fit(x_train,y_train)


    #打印训练最佳效果:
    print grid_search.best_score_





#wanggesousuo()





# #分割测试集  训练集
# x_train,x_test,y_train,y_test=train_test_split(df['message'],df['label'],random_state=1);


# #建立TfidfVectorizer实例 计算TF-IDF权重
# vectorizer=TfidfVectorizer()
# x_train=vectorizer.fit_transform(x_train)
# x_test=vectorizer.transform(x_test)



# #建立LogisticRegression实例训练模型
# classifier=LogisticRegression()
# #训练
# classifier.fit(x_train, y_train)
# #预测
# predictions=classifier.predict(x_test)

# #x_test预测输出概率
# predictions2=classifier.predict_proba(x_test)

# print y_test
# #通过实际测试值 和和预测概率 计算 召回率 和误警率
# false_positive_rate, recall, thresholds = roc_curve(y_test, predictions2[:, 1],pos_label='spam')


# #计算auc值
# roc_auc = auc(false_positive_rate, recall)

# print false_positive_rate
# print recall

# #配置标题
# plt.title('Receiver Operating Characteristic')
# #绘图
# plt.plot(false_positive_rate, recall, 'b', label=roc_auc)
# #说明的位置 如果不设置 label不会生效
# plt.legend(loc=4)
# #设置x y限制
# plt.xlim([0.0, 1.0])
# plt.ylim([0.0, 1.0])
# #指定x y 标签
# plt.ylabel('Recall')
# plt.xlabel('Fall-out')

# plt.show()

plt.title('Receiver Operating Characteristic')
plt.plot([0,1],[0,1])
plt.show()




# #循环预测值   打印预测值  和测试值
# #n=0
# #k=0
# # for x,y in enumerate(predictions):
# #     print  y,y_test.iloc[2]
# #     k+=1
# #     if y == y_test.iloc[2]:
# #         pass
# #     else:
# #         n+=1


# #输出测试结果
# print classifier.score(x_test,y_test)


# #使用交叉检测
# scores = cross_val_score(classifier,x_test,y_test,cv=5)


# #输出交叉测试结果平均值 和 单次的值
# print numpy.mean(scores),scores


# #定义精确率的 pos_label 为ham
# precision_scoring=make_scorer(precision_score,pos_label='ham')


# #使用交叉测试计算精确率
# precisions = cross_val_score(classifier, x_train, y_train, cv=5, scoring=precision_scoring)


# #输出交叉测试结果平均值 和 单次的值
# print numpy.mean(precisions),precisions


# #定义召回率的 pos_label 为ham
# recall_scoring=make_scorer(recall_score,pos_label='ham')


# #使用交叉测试计算召回率
# recalls = cross_val_score(classifier,x_train,y_train,cv=5,scoring=recall_scoring)


# #输出交叉测试结果平均值 和 单次的值
# print numpy.mean(recalls),recalls


# #定义综合评价指标的 pos_label 为ham
# f1_scoring=make_scorer(f1_score,pos_label='ham')


# #使用交叉测试计算综合评价指标
# f1 = cross_val_score(classifier,x_train,y_train,cv=5,scoring=f1_scoring)


# #输出交叉测试结果平均值 和 单次的值
# print numpy.mean(f1),f1