用前5000条数据对模型进行训练和调参(sklearn中的网格搜索)兄下面的结果来看,单个模型效果最好的是LR,也可能存在参数范围选取不当等原因。
模型 | 最优参数 | F-1 |
LogisticRegression | C=4.0 max_iter: 100 |
0.6600 |
SVM | C=0.7 kernel:linear |
0.6222 |
LightGBM | learning_rate: 0.5 |
0.6336 |
AdaBoost | learning_rate: 0.6 n_estimators: 50 |
0.4006 |
import pandas as pd
from sklearn.cross_validation import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn import svm
import lightgbm as lgb
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import f1_score
train_data = pd.read_csv('./new_data/train_set.csv',nrows = 5000)
train_data.drop(columns=['article','id'], inplace = True)
tfidf=TfidfVectorizer()
X_train=tfidf.fit_transform(train_data['word_seg'])
Y_train = train_data['class']-1
x_train, x_test, y_train, y_test = train_test_split(X_train, Y_train, test_size=0.25, random_state=33)
#logisticRegression
lg = LogisticRegression()
param_lg = {'C':[4.0,3.0,2.0,1.0],
'penalty':['l1','l2'],
'maxiter':[10,20,50,100]
}
lg = GridSearchCV(lg, param_lg)
lg.fit(x_train, y_train)#训练分类器
y_predict = lg.predict(x_test)
acc_train = float((y_predict==y_test).sum())/(len(y_test))#验证集错误率
print("accuracy of LR:",acc_train)
print(f1_score(y_test, y_predict , average='weighted'))
lg.best_estimator_.get_params()
#svm
clf = svm.SVC(C=0.6, kernel='rbf', gamma=20, decision_function_shape='ovr')
param_svm = {'C':[0.3,0.5,0.6,0.7],
'kernel':['rbf','linear'],
'gamma':[18,20,22]
}
clf = GridSearchCV(clf, param_svm)
clf.fit(x_train, y_train)
y_sv = clf.predict(x_test)
ac = float((y_sv == y_test).sum())/(len(y_test))
print("accuracy of SVM:", ac)
print(f1_score(y_test, y_sv , average='weighted'))
clf.best_estimator_.get_params()
#adaboost
ada = AdaBoostClassifier()
param_ada = {'C':[0.3,0.5,0.6,0.7],
'kernel':['rbf','linear'],
'gamma':[18,20,22]
}
ada = GridSearchCV(ada, param_ada)
ada.fit(x_train, y_train)
y_ada = ada.predict(x_test)
ac = float((y_ada == y_test).sum())/(len(y_test))
print("accuracy of ada:", ac)
print(f1_score(y_test, y_ada , average='weighted'))
ada.best_estimator_.get_params()
#lightgbm
lightgbm = lgb.sklearn.LGBMClassifier()
param_grid = {
'learning_rate': [0.01, 0.1, 0.5],
'n_estimators': [30, 40]
}
lightgbm = GridSearchCV(lightgbm, param_grid)
lightgbm.fit(x_train, y_train)
y_lgb = lightgbm.predict(x_test)
acc = lightgbm.score(x_test, y_test)
print(f1_score(y_test, y_lgb , average='weighted'))
lightgbm.best_estimator_.get_params()