def experiment(model_class, vectorizer, xval):
name = model_class.__class__.__name__
model = model_class.fit(X, y)
model_weights = vectorizer.inverse_transform(model.coef_)[0]
with open('weights.%s.txt' % name, 'w') as f:
f.write('%s\t%f\n' % ('(intercept)', model.intercept_))
f.writelines('%s\t%f\n' % k for k in model_weights.items())
r2_scores = cross_validation.cross_val_score(model, X, y, scoring='r2', cv=xval)
mae_scores = cross_validation.cross_val_score(model, X, y, scoring='mean_absolute_error', cv=xval)
print '-'*80
print 'r2\t%.4f\t%s' % (np.mean(r2_scores), name)
print 'mae\t%.4f\t%s' % (np.mean(mae_scores), name)
评论列表
文章目录