我正在运行逻辑回归,在文本列上运行tf-idf。这是我在逻辑回归中使用的唯一列。如何确保尽可能好地调整参数?
我希望能够运行一组步骤,这些步骤最终允许我说我的逻辑回归分类器正在运行。
from sklearn import metrics,preprocessing,cross_validation
from sklearn.feature_extraction.text import TfidfVectorizer
import sklearn.linear_model as lm
import pandas as p
loadData = lambda f: np.genfromtxt(open(f, 'r'), delimiter=' ')
print "loading data.."
traindata = list(np.array(p.read_table('train.tsv'))[:, 2])
testdata = list(np.array(p.read_table('test.tsv'))[:, 2])
y = np.array(p.read_table('train.tsv'))[:, -1]
tfv = TfidfVectorizer(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)
rd = lm.LogisticRegression(penalty='l2', dual=True, tol=0.0001,
C=1, fit_intercept=True, intercept_scaling=1.0,
class_weight=None, random_state=None)
X_all = traindata + testdata
lentrain = len(traindata)
print "fitting pipeline"
tfv.fit(X_all)
print "transforming data"
X_all = tfv.transform(X_all)
X = X_all[:lentrain]
X_test = X_all[lentrain:]
print "20 Fold CV Score: ", np.mean(cross_validation.cross_val_score(rd, X, y, cv=20, scoring='roc_auc'))
print "training on full data"
rd.fit(X, y)
pred = rd.predict_proba(X_test)[:, 1]
testfile = p.read_csv('test.tsv', sep="t", na_values=['?'], index_col=1)
pred_df = p.DataFrame(pred, index=testfile.index, columns=['label'])
pred_df.to_csv('benchmark.csv')
print "submission file created.."
您可以使用网格搜索来找出最适合您的C
值。基本上较小的C
指定更强的正则化。
>>> param_grid = {'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000] }
>>> clf = GridSearchCV(LogisticRegression(penalty='l2'), param_grid)
GridSearchCV(cv=None,
estimator=LogisticRegression(C=1.0, intercept_scaling=1,
dual=False, fit_intercept=True, penalty='l2', tol=0.0001),
param_grid={'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000]})
有关应用程序的更多详细信息,请参阅 GridSearchCv 文档。
您可以使用以下代码获取更多一般详细信息:
LR = LogisticRegression()
LRparam_grid = {
'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000],
'penalty': ['l1', 'l2'],
# 'max_iter': list(range(100,800,100)),
'solver': ['newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga']
}
LR_search = GridSearchCV(LR, param_grid=LRparam_grid, refit = True, verbose = 3, cv=5)
# fitting the model for grid search
LR_search.fit(X_train , y_train)
LR_search.best_params_
# summarize
print('Mean Accuracy: %.3f' % LR_search.best_score_)
print('Config: %s' % LR_search.best_params_)
网格搜索是查找最佳参数的残酷方法,因为它训练和测试每个可能的组合。 最好的方法是使用贝叶斯优化,它学习过去的评估分数并花费更少的计算时间。