import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.grid_search import GridSearchCV

train = pd.read_csv('labeledTrainData.tsv', delimiter='\t', quoting=3)
test = pd.read_csv('testData.tsv', delimiter='\t', quoting=3)

parameters = {
        'clf__C': [pow(10, x) for x in range(-2,2)]
}

pipeline = Pipeline([
        ('vect', TfidfVectorizer()),
        ('clf', LogisticRegression())
])

grid_search = GridSearchCV(pipeline, parameters, 'roc_auc', n_jobs=-1, verbose=2)
grid_search.fit(train['review'], train['sentiment'])

print("Best score: {}".format(grid_search.best_score_))
print("Best parameters set:")
best_parameters = grid_search.best_estimator_.get_params()
for param_name in sorted(parameters.keys()):
        print("\t{}: {}".format(param_name, best_parameters[param_name]))

test_pred = grid_search.predict_proba(test['review'])
submission = pd.DataFrame({'id': test['id'], 'sentiment': test_pred[:,1]})
submission.to_csv('submission-tfidf.csv', index=False, quoting=3)
        
