Kaggle competition: Bag of Words Meets Bags of Popcorn
df = df.drop(columns=['temp','atemp'] )<import_modules>
warnings.filterwarnings('ignore')
 data_path = '.. /input/'
%matplotlib inline
from sklearn.model_selection import KFold, cross_val_score, train_test_split, GridSearchCV
from sklearn.metrics import mean_squared_log_error
from sklearn.preprocessing import RobustScaler
from sklearn.pipeline import make_pipeline
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor
from sklearn.linear_model import ElasticNet, Lasso, LinearRegression<split>
lbl_train = pd.read_csv(data_path+'labeledTrainData.tsv', sep='\t')
 print("Shape: {}".format(lbl_train.shape))
print(lbl_train.columns )
new_train = df[:train.shape[0]]
new_test = df[train.shape[0]:]<prepare_x_and_y>
unlbl_train = pd.read_csv(data_path+'unlabeledTrainData.tsv', sep='\t', error_bad_lines=False)
 print("Shape: {}".format(unlbl_train.shape))
print(unlbl_train.columns )
target = train['log_count']<split>
test = pd.read_csv(data_path+'testData.tsv', sep='\t')
 print(f'Shape: {test.shape}')
 print(test.columns )
X_train, X_val, y_train, y_val = train_test_split(new_train, target, test_size=0.2, shuffle=True )<compute_test_metric>
samplesub = pd.read_csv(data_path+'./sampleSubmission.csv')
 samplesub.head(3 )
def rmsle_score(preds, true):
rmsle_score =(np.sum(( np.log1p(preds)-np.log1p(true)) **2)/len(true)) **0.5
return rmsle_score<import_modules>
print(f'pos:
{lbl_train.review[np.random.randint(0, 25000)]}')
 print(f'neg:
{test.review[np.random.randint(0, 25000)]}' )
RMSLE = make_scorer(rmsle_score )<import_modules>
def clean_review(review):
review = re.sub(r'<br />', '', review)
 tokenizer = RegexpTokenizer(r'\w+')
 review = tokenizer.tokenize(review)
 stemmer = PorterStemmer()
review = ' '.join([stemmer.stem(y)for y in review])
 return review
lbl_train.review = lbl_train.review.apply(lambda x: clean_review(x))
test.review = test.review.apply(lambda x: clean_review(x))
unlbl_train = unlbl_train.review.apply(lambda x: clean_review(x))
import statsmodels.api as sm<data_type_conversions>
X_train, X_test, y_train, y_test = train_test_split(lbl_train.review, lbl_train.sentiment,
test_size=0.2, random_state=13)
 X_train.shape, X_test.shape
model = sm.OLS(target.values, new_train.astype(float))<train_model>
%%time
lg_pipeline = Pipeline([ 
('tfidf', TfidfVectorizer(max_df=0.9)) , 
('lg', LogisticRegression(n_jobs=-1))
])
 lg_pipeline.fit(X_train, y_train)
 print(f'Accuracy: {np.mean(lg_pipeline.predict(X_test)==y_test)}' )
re = model.fit()<define_search_space>
%%time
rfc_pipeline = Pipeline([ 
('tfidf', TfidfVectorizer(max_df=0.9)) , 
('rfc', RandomForestClassifier(n_estimators=100, n_jobs=-1))
])
 rfc_pipeline.fit(X_train, y_train)
 print(f"Accuracy: {np.mean(rfc_pipeline.predict(X_test)==y_test)}" )
param = {'alpha':[1e-03,0.01,0.1,0.5,0.8,1], 'normalize':[True,False], 'tol':[1e-06,1e-05,5e-05,1e-04,5e-04,1e-03]}<choose_model_class>
%%time
nb_pipeline = Pipeline([ 
('tfidf', TfidfVectorizer(max_df=0.9)) , 
('nb', BernoulliNB())
 ])
 nb_pipeline.fit(X_train, y_train)
 print(f'Accuracy: {np.mean(nb_pipeline.predict(X_test)==y_test)}' )
lasso = make_pipeline(GridSearchCV(Lasso(random_state=1),param,
cv=10, scoring = RMSLE))<train_model>
lg_params = {
'tfidf__ngram_range': [(1, 1),(1, 2),(1, 3)],
'lg__C': [0.1, 1, 10],
}
rfc_params = {
'tfidf__ngram_range': [(1, 1),(1, 2),(1, 3)],
'rfc__max_features': ['auto', 'sqrt'],
'rfc__bootstrap': [True, False],
'rfc__n_estimators': [200, 400, 600],
'rfc__min_samples_split': [2, 5, 10],
'rfc__min_samples_leaf': [1, 2, 4]
}
lasso.fit(X_train,y_train )<predict_on_test>
%%time
lg_grid = GridSearchCV(lg_pipeline, param_grid=lg_params, cv=3, verbose=True, n_jobs=-1)
la_yhat = lasso.predict(X_val )<compute_test_metric>
%%time
rfc_grid = RandomizedSearchCV(rfc_pipeline, param_distributions=rfc_params, cv=3, verbose=True, n_jobs=-1)
s_lasso = rmsle_score(la_yhat,y_val)
 s_lasso<predict_on_test>
lg_best_params = {'lg__C': 10, 'tfidf__ngram_range':(1, 2)}
pred_la = lasso.predict(new_test )<define_search_space>
rfc_best_params = {'tfidf__ngram_range':(1, 3),
'rfc__n_estimators': 400,
'rfc__min_samples_split': 5,
'rfc__min_samples_leaf': 4,
'rfc__max_features': 'sqrt',
'rfc__bootstrap': False}
param_e = {'alpha' :[0.1,1.0,10], 'max_iter' :[1000000], 'l1_ratio':[0.04,0.05],
'normalize':[True,False]}<choose_model_class>
lg_pipeline.set_params(**lg_best_params)
 rfc_pipeline.set_params(**rfc_best_params)
 lg_pipeline.fit(lbl_train.review, lbl_train.sentiment)
 rfc_pipeline.fit(lbl_train.review, lbl_train.sentiment )
Enet = make_pipeline(GridSearchCV(ElasticNet(random_state=1),param_e,
cv=10, scoring = RMSLE))<train_model>
nb_pipeline.set_params(tfidf__ngram_range=(1,3))
nb_pipeline.fit(lbl_train.review, lbl_train.sentiment )
Enet.fit(X_train,y_train )<predict_on_test>
lg_preds = lg_pipeline.predict(test.review)
 rfc_preds = rfc_pipeline.predict(test.review )
Enet_yhat = Enet.predict(X_val )<compute_test_metric>
nb_preds = nb_pipeline.predict(test.review )
s_Enet = rmsle_score(Enet_yhat,y_val)
 s_Enet<predict_on_test>
predictions = pd.DataFrame({'lg': lg_preds, 'rfc': rfc_preds, 'nb': nb_preds} ).mode(axis=1 ).rename(columns={0: 'sentiment'})
 submission = pd.DataFrame({'id': test.id.values, 'sentiment': predictions.sentiment.values} )
pred_Enet = Enet.predict(new_test )<define_search_space>
submission.to_csv('submission2.csv', index=False)
param_Rf = {'min_samples_split' : [3,4,6,10], 'n_estimators' : [70,100], 'random_state': [5] }<choose_model_class>
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Embedding, Flatten, Dense, Dropout, BatchNormalization
from keras.layers import LSTM, Bidirectional, GlobalMaxPool1D
from keras.models import Sequential
from keras.optimizers import Adam
RF = make_pipeline(GridSearchCV(RandomForestRegressor(random_state=1),param_Rf,
cv=10, scoring = RMSLE))<train_model>
vocab_size = 10000
t = Tokenizer(num_words=vocab_size)
 t.fit_on_texts(lbl_train.review )
RF.fit(X_train,y_train )<compute_test_metric>
sequences = t.texts_to_sequences(lbl_train.review)
 sequences = pad_sequences(sequences, maxlen=150 )
RF_yhat = RF.predict(X_val)
 s_RF = rmsle_score(RF_yhat,y_val)
 s_RF<predict_on_test>
model = Sequential()
model.add(Embedding(input_dim=vocab_size, output_dim=128, input_length=150, name='embed'))
model.add(Bidirectional(LSTM(32, return_sequences=True, name='lstm')))
 model.add(GlobalMaxPool1D(name='gmax1'))
model.add(Dense(20, name='dense1'))
model.add(Dropout(0.05, name='drop1'))
model.add(Dense(1, activation='sigmoid', name='softmax'))
pred_RF = RF.predict(new_test )<define_search_space>
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
 model.fit(sequences, lbl_train.sentiment.values, validation_split=0.2, epochs=5, batch_size=128, verbose=2 )
param_GB = [{'learning_rate': [1,0.1,0.01,0.001],
'n_estimators': [50, 100, 200, 500, 1000]}]<choose_model_class>
test_sequences = t.texts_to_sequences(test.review)
 test_sequences = pad_sequences(test_sequences, maxlen=150 )
GB = make_pipeline(GridSearchCV(GradientBoostingRegressor(random_state=1),param_GB,
cv=10, scoring = RMSLE))<train_model>
preds = model.predict(test_sequences )
GB.fit(X_train,y_train )<compute_train_metric>
preds =(preds>0.5 )
GB_yhat = GB.predict(X_val)
 s_GB = rmsle_score(GB_yhat,y_val)
 s_GB<predict_on_test>
preds = [int(p)for p in preds]
pred_GB = GB.predict(new_test )<define_search_space>
submission2 = pd.DataFrame({'id': test.id.values, 'sentiment': preds})
 submission2.to_csv('submission2.csv', index=False )
param_lgb = param_grid = [{
'n_estimators': [400, 700, 1000],
'max_depth': [15,20,25],
'num_leaves': [50, 100, 200],
'min_split_gain': [0.3, 0.4],
}]<choose_model_class>
FileLink('./submission2.csv' )