Kaggle competition: Bag of Words Meets Bags of Popcorn
rf4 = RandomForestRegressor(random_state=42, n_estimators=200, n_jobs=-1, min_samples_leaf=4)
 rf4.fit(train[feats], train['count'])
 preds4 = rf4.predict(valid[feats])
 mean_squared_error(valid['count'], preds4)**(1/2 )<save_to_csv>
train = pd.read_csv(".. /input/word2vec-nlp-tutorial/labeledTrainData.tsv", delimiter='\t')
 train.head()
test['count'] = np.exp(rf4.predict(test[feats]))
test[['datetime', 'count']].to_csv('rf4.csv', index=False )<set_options>
train_imdb = pd.read_csv(".. /input/imdb-review-dataset/imdb_master.csv", encoding="latin-1", index_col=0)
 train_imdb.head()
%matplotlib inline
warnings.filterwarnings("ignore", category=FutureWarning)
 warnings.filterwarnings("ignore" )<load_from_csv>
train_imdb = train_imdb[train_imdb.label != 'unsup']
train_imdb["sentiment"] = train_imdb.label.map({"neg": 0, "pos": 1})
 train_imdb.drop(["type", "file", "label"], axis=1, inplace=True )
train = pd.read_csv("/kaggle/input/bike-sharing-demand/train.csv")
 test = pd.read_csv("/kaggle/input/bike-sharing-demand/test.csv" )<count_missing_values>
train_enhanced = pd.concat([train, train_imdb], ignore_index=True)
 X_train = train_enhanced.review
y_train = train_enhanced.sentiment
train.isnull().sum()<count_missing_values>
stopwords_eng = set(stopwords.words("english"))
def clean_text(text):
text = text.lower()
text = re.sub("[^a-z]", " ", text)
 words = [word for word in text.split() if word not in stopwords_eng]
text = " ".join(words)
 return text
X_train = X_train.map(clean_text)
 X_train.head()
train.isnull().sum()<prepare_x_and_y>
num_words = 6000
tokenizer = Tokenizer(num_words=num_words)
 tokenizer.fit_on_texts(X_train)
 X_train_seq = pd.Series(tokenizer.texts_to_sequences(X_train))
X_train_seq.head()
target = train['count']<import_modules>
X_train_pad = pad_sequences(X_train_seq, maxlen=512 )
from scipy import stats
from scipy.stats import norm<feature_engineering>
model = Sequential()
model.add(Embedding(input_dim=num_words, output_dim=64))
model.add(LSTM(32, return_sequences=True))
model.add(GlobalMaxPool1D())
 model.add(Dense(16, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1, activation='sigmoid'))
model.summary()
train["log_count"] = np.log(target+1 )<concatenate>
model.compile(loss="binary_crossentropy", optimizer='adam', metrics=['accuracy'])
 batch_size = 32
epochs = 2
validation_split = 0.01
model.fit(x=X_train_pad, y=y_train, batch_size=batch_size, epochs=epochs, validation_split=validation_split )
feature_names=list(test)
 df_train=train[feature_names]
df=pd.concat(( df_train, test))<import_modules>
test = pd.read_csv(".. /input/word2vec-nlp-tutorial/testData.tsv", delimiter="\t")
 test.head()
import datetime<data_type_conversions>
X_test = test.review.map(clean_text)
 X_test.head()
tmp = pd.to_datetime(train['datetime'] )<feature_engineering>
X_test_seq = tokenizer.texts_to_sequences(X_test)
 X_test_pad = pad_sequences(X_test_seq, maxlen=512 )
df['datetime'] = pd.to_datetime(df['datetime'])
 df['day'] = df['datetime'].dt.day
df['hour'] = df['datetime'].dt.hour
df['dayofweek'] = df['datetime'].dt.dayofweek
df['month'] = df['datetime'].dt.month
df['year'] = df['datetime'].dt.year
df['weekend'] =(df['dayofweek'] ==5)|(df['dayofweek'] == 6 )<feature_engineering>
pred = model.predict(x=X_test_pad)
 y_pred =(pred >= 0.5)* 1
<drop_column><EOS> 
results = pd.DataFrame({"id": test.id, "sentiment": y_pred.flatten() })
 results.to_csv("submission.csv", index=False )