text_plain_output_1.png
from sklearn.ensemble import RandomForestClassifier

from sklearn.linear_model import LogisticRegression

from sklearn.metrics import classification_report, confusion_matrix, accuracy_score,make_scorer

from sklearn.naive_bayes import MultinomialNB

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

df_sample = pd.read_csv('/kaggle/input/train.csv')
df_actual = pd.read_csv('/kaggle/input/test.csv')

df_sample['length'] = np.NaN
for i in range(0, len(df_sample['text'])):
    df_sample['length'][i] = len(df_sample['text'][i])
df_sample.length = df_sample.length.astype(int)

vectorizer = TfidfVectorizer(ngram_range=(1, 3), min_df=3, strip_accents='unicode', use_idf=1, smooth_idf=1, sublinear_tf=1, max_features=None)
vectorizer.fit(list(df_sample['text_processed']) + list(df_actual['text_processed']))

X_train_onehot = vectorizer.transform(X_train).todense()
X_val_onehot = vectorizer.transform(X_val).todense()

from sklearn.linear_model import LogisticRegression
lr_clf = LogisticRegression(max_iter=150, penalty='l2', solver='lbfgs', random_state=0)
lr_clf.fit(X_train_onehot, y_train)
lr_pred = lr_clf.predict(X_val_onehot)

from sklearn.metrics import log_loss
logloss_lr = log_loss(y_val, lr_clf.predict_proba(X_val_onehot))

from sklearn.naive_bayes import MultinomialNB
mnb_clf = MultinomialNB()
mnb_clf.fit(X_train_onehot, y_train)
mnb_pred = mnb_clf.predict(X_val_onehot)

logloss_mnb = log_loss(y_val, mnb_clf.predict_proba(X_val_onehot))

rf_clf = RandomForestClassifier(random_state=0, n_estimators=100, max_depth=None, verbose=0, n_jobs=-1)
rf_clf.fit(X_train_onehot, y_train)
rf_pred = rf_clf.predict(X_val_onehot)

logloss_rf = log_loss(y_val, rf_clf.predict_proba(X_val_onehot))

lr_predictions_val = lr_clf.predict_proba(X_val_onehot)
mnb_predictions_val = mnb_clf.predict_proba(X_val_onehot)
rf_predictions_val = rf_clf.predict_proba(X_val_onehot)

predictions_val = 1 / 5 * lr_predictions_val[:, 1] + 1 / 5 * rf_predictions_val[:, 1]
predictions_val = 1 / 3 * lr_predictions_val[:, 1] + 1 / 3 * mnb_predictions_val[:, 1]
predictions_val = np.where(predictions_val > 0.5, 1, 0)

df_actual = pd.read_csv('../input/nlp-getting-started/test.csv')

X_test = df_actual['text_processed']
X_test_onehot = vectorizer.transform(X_test).todense()

lr_predictions = lr_clf.predict_proba(X_test_onehot)
mnb_predictions = mnb_clf.predict_proba(X_test_onehot)
rf_predictions = rf_clf.predict_proba(X_test_onehot)

predictions = 1 / 5 * lr_predictions[:, 1] + 1 / 5 * rf_predictions[:, 1]
predictions = 1 / 3 * lr_predictions[:, 1] + 1 / 3 * mnb_predictions[:, 1]

predictions = np.where(predictions > 0.5, 1, 0)

df_submission = pd.read_csv('../input/nlp-getting-started/test.csv')
df_submission['target'] = predictions
df_submission.to_csv('submission.csv', index=False)