Kaggle competition: Bag of Words Meets Bags of Popcorn
drop_features = ['count', 'casual', 'registered', 'datetime', 'date', 'datetime', 'windspeed', 'month']
X_train = train.drop(drop_features, axis=1)
 X_test = test.drop(drop_features, axis=1 )<compute_test_metric>
zf1 = zipfile.ZipFile("/kaggle/input/word2vec-nlp-tutorial/labeledTrainData.tsv.zip")
 train = pd.read_csv(zf1.open("labeledTrainData.tsv"), sep="\t")
 zf2 = zipfile.ZipFile("/kaggle/input/word2vec-nlp-tutorial/testData.tsv.zip")
 test = pd.read_csv(zf2.open("testData.tsv"), sep="\t")
 train.drop(columns=['id'], inplace=True)
 train.drop_duplicates(keep=False,inplace=True)
def rmsle(y_true, y_pred, convertExp=True):
if convertExp:
y_true = np.exp(y_true)
 y_pred = np.exp(y_pred)
 log_true = np.nan_to_num(np.array([np.log(y+1)for y in y_true]))
log_pred = np.nan_to_num(np.array([np.log(y+1)for y in y_pred]))
output = np.sqrt(np.mean(( log_true - log_pred)**2))
return output<train_model>
imdb_master = pd.read_csv(".. /input/imdb-review-dataset/imdb_master.csv", encoding = "ISO-8859-1")
 imdb_master.drop(inplace=True, columns=['file','type','Unnamed: 0'])
 imdb_master.columns = ['review','sentiment']
imdb_master=imdb_master[(imdb_master.sentiment == "neg")|(imdb_master.sentiment == "pos")]
di = {"neg": int("0"), "pos": int("1")}
imdb_master['sentiment']=imdb_master['sentiment'].map(di ).astype('int64')
 imdb_master.drop_duplicates(keep=False,inplace=True)
linear_reg_model = LinearRegression()
log_y = np.log1p(y)
 linear_reg_model.fit(X_train, log_y)
 preds = linear_reg_model.predict(X_train)
 print('Linear Regression RMSLE:', rmsle(log_y, preds, True))<choose_model_class>
example1 = BeautifulSoup(train["review"][0])
 letters_only = re.sub("[^a-zA-Z]",
" ",
example1.get_text())
 lower_case = letters_only.lower()
words = lower_case.split()
ridge_model = Ridge()
ridge_params = {'max_iter':[3000], 'alpha':[0.1, 1, 2, 3, 4, 10, 30, 100, 200, 300, 400, 800, 900, 1000]}
rmsle_scorer = metrics.make_scorer(rmsle, greater_is_better=False)
 gridsearch_ridge_model = GridSearchCV(estimator=ridge_model,
param_grid=ridge_params,
scoring=rmsle_scorer,
cv=5)
 log_y = np.log1p(y)
 gridsearch_ridge_model.fit(X_train, log_y)
 print('Best Parameter:', gridsearch_ridge_model.best_params_)
 preds = gridsearch_ridge_model.best_estimator_.predict(X_train)
 print('Ridge Regression RMSLE:', rmsle(log_y, preds, True))<choose_model_class>
import nltk
lasso_model = Lasso()
lasso_alpha = 1/np.array([0.1, 1, 2, 3, 4, 10, 30, 100, 200, 300, 400, 800, 900, 1000])
 lasso_params = {'max_iter':[3000], 'alpha':lasso_alpha}
gridsearch_lasso_model = GridSearchCV(estimator=lasso_model,
param_grid=lasso_params,
scoring=rmsle_scorer,
cv=5)
 log_y = np.log1p(y)
 gridsearch_lasso_model.fit(X_train, log_y)
 print('Best Parameter:', gridsearch_lasso_model.best_params_)
 preds = gridsearch_lasso_model.best_estimator_.predict(X_train)
 print('Lasso Regression RMSLE:', rmsle(log_y, preds, True))<choose_model_class>
print(stopwords.words("english"))
randomforest_model = RandomForestRegressor()
rf_params = {'random_state':[42], 'n_estimators':[100, 120, 140]}
gridsearch_random_forest_model = GridSearchCV(estimator=randomforest_model,
param_grid=rf_params,
scoring=rmsle_scorer,
cv=5)
 log_y = np.log1p(y)
 gridsearch_random_forest_model.fit(X_train, log_y)
 print('Best Parameter:', gridsearch_random_forest_model.best_params_)
 preds = gridsearch_random_forest_model.best_estimator_.predict(X_train)
 print('Random Forest Regression RMSLE:', rmsle(log_y, preds, True))<save_to_csv>
words = [w for w in words if not w in stopwords.words("english")]
submission['count'] = np.exp(randomforest_preds)
 submission.to_csv('submission.csv', index=False )<load_pretrained>
def review_to_words(raw_review):
review_text = BeautifulSoup(raw_review ).get_text()
letters_only = re.sub("[^a-zA-Z]", " ", review_text)
 words = letters_only.lower().split()
stops = set(stopwords.words("english"))
meaningful_words = [w for w in words if not w in stops]
return(" ".join(meaningful_words))
register_matplotlib_converters()<compute_test_metric>
num_reviews = train["review"].size
print("Cleaning and parsing the training set movie reviews...
 ")
 clean_train_reviews = []
for i in range(0, num_reviews):
if(( i+1)%1000 == 0):
print("Review %d of %d
" %(i+1, num_reviews))
clean_train_reviews.append(review_to_words(train["review"][i]))
print('Test log and exp')
 test_count = 100
print('original value', test_count)
 x = np.log1p(test_count)
 print('log1p', x)
 print('expm1', np.expm1(x))<load_from_csv>
print("Creating the bag of words...
 ")
 vectorizer = CountVectorizer(analyzer = "word", \
tokenizer = None, \
preprocessor = None, \
stop_words = None, \
max_features = 5000)
 train_data_features = vectorizer.fit_transform(clean_train_reviews)
 train_data_features = train_data_features.toarray()
df = pd.read_csv('.. /input/bike-sharing-demand/train.csv',parse_dates=['datetime'],index_col=0)
 df_test = pd.read_csv('.. /input/bike-sharing-demand/test.csv',parse_dates=['datetime'],index_col=0)
<define_variables>
forest = RandomForestClassifier(n_estimators = 100,)
 %time forest = forest.fit(train_data_features, train["sentiment"] )
columns = ['count', 'season', 'holiday', 'workingday', 'weather', 'temp',
'atemp', 'humidity', 'windspeed', 'year', 'month', 'day', 'dayofweek','hour']<feature_engineering>
model=forest
def add_features(df):
df['year'] = df.index.year
df['month'] = df.index.month
df['day'] = df.index.day
df['dayofweek'] = df.index.dayofweek
df['hour'] = df.index.hour<feature_engineering>
xgboost = xgb.XGBRegressor(colsample_bytree=0.4,
gamma=0,
learning_rate=0.07,
max_depth=3,
min_child_weight=1.5,
n_estimators=10000,
reg_alpha=0.75,
reg_lambda=0.45,
subsample=0.6,
seed=42)
 lightgbm = lgb.LGBMClassifier()
add_features(df)
 add_features(df_test )<groupby>
<feature_engineering><EOS> 
num_reviews = len(test["review"])
 clean_test_reviews = []
print("Cleaning and parsing the test set movie reviews...
 ")
 for i in range(0,num_reviews):
if(( i+1)% 1000 == 0):
print("Review %d of %d
" %(i+1, num_reviews))
clean_review = review_to_words(test["review"][i])
 clean_test_reviews.append(clean_review)
 test_data_features = vectorizer.transform(clean_test_reviews)
 test_data_features = test_data_features.toarray()
result = model.predict_proba(test_data_features)
 output = pd.DataFrame(data={"id":test["id"], "sentiment":result[:,1]})
 output.to_csv("Bag_of_Words_model_random_forest.csv", index=False, quoting=3 )