NLP之TEA之NB/LoR:基于NB和LoR算法对Kaggle IMDB影评数据集(国外类似豆瓣电影)情感分析进行分类
注:LoR code waiting
目录
输出结果
核心代码
数据集:Dataset之IMDB影评数据集:IMDB影评数据集的简介、下载、使用方法之详细攻略
#1、读取数据并做一些基本的预处理(比如说把评论部分的html标签去掉等等)
def review_to_wordlist(review):
'''
把IMDB的评论转成词序列
'''
review_text = BeautifulSoup(review,"lxml").get_text() # 去掉HTML标签,拿到内容
review_text = re.sub("[^a-zA-Z]"," ", review_text) # 用正则表达式取出符合规范的部分
words = review_text.lower().split() # 小写化所有的词,并转成词list
return words # 返回words
# 使用pandas读入训练和测试csv文件
train = pd.read_csv('F:/File_Python/Resources/Kaggle Film critic emotion/labeledTrainData.tsv', header=0, delimiter="\t", quoting=3)
test = pd.read_csv('F:/File_Python/Resources/Kaggle Film critic emotion/testData.tsv', header=0, delimiter="\t", quoting=3 )
y_train = train['sentiment'] # 取出情感标签,positive/褒 或者 negative/贬
train_data = [] # 将训练和测试数据都转成词list
for i in range(0,len(train['review'])):
train_data.append(" ".join(review_to_wordlist(train['review'][i])))
test_data = []
for i in range(0,len(test['review'])):
test_data.append(" ".join(review_to_wordlist(test['review'][i])))
print(train_data)
print(y_train)
#2、特征处理:从数据里面拿到有区分度的特征,采用TF-IDF向量方法
from sklearn.feature_extraction.text import TfidfVectorizer as TFIV
# 初始化TFIV对象,去停用词,加2元语言模型
tfv = TFIV(min_df=3, max_features=None, strip_accents='unicode', analyzer='word',token_pattern=r'\w{1,}', ngram_range=(1, 2), use_idf=1,smooth_idf=1,sublinear_tf=1, stop_words = 'english')
X_all = train_data + test_data # 合并训练和测试集以便进行TFIDF向量化操作
len_train = len(train_data)
tfv.fit(X_all)
X_all = tfv.transform(X_all)
X = X_all[:len_train] # 恢复成训练集和测试集部分
X_test = X_all[len_train:]
# 3、利用NB算法,多项式朴素贝叶斯
from sklearn.naive_bayes import MultinomialNB as MNB
model_NB = MNB()
model_NB.fit(X, y_train) #特征数据直接灌进来
MNB(alpha=1.0, class_prior=None, fit_prior=True)
from sklearn.cross_validation import cross_val_score
import numpy as np
print ("多项式贝叶斯分类器20折交叉验证得分: ", np.mean(cross_val_score(model_NB, X, y_train, cv=20, scoring='roc_auc')))
#4、利用LoR算法
from sklearn.linear_model import LogisticRegression as LR
from sklearn.grid_search import GridSearchCV
# 设定grid search的参数
grid_values = {'C':[30]}
# 设定打分为roc_auc
model_LR = GridSearchCV(LR(penalty = 'L2', dual = True, random_state = 0), grid_values, scoring = 'roc_auc', cv = 20)
# 数据灌进来
model_LR.fit(X,y_train)
# 20折交叉验证,开始漫长的等待...
GridSearchCV(cv=20, estimator=LogisticRegression(C=1.0, class_weight=None, dual=True,
fit_intercept=True, intercept_scaling=1, penalty='L2', random_state=0, tol=0.0001),
fit_params={}, iid=True, loss_func=None, n_jobs=1,
param_grid={'C': [30]}, pre_dispatch='2*n_jobs', refit=True,
score_func=None, scoring='roc_auc', verbose=0)
#输出结果
print (model_LR.grid_scores_)
相关文章推荐
NLP系列(4)_朴素贝叶斯实战与进阶