Kaggle competition: Natural Language Processing with Disaster Tweets
xgb_clf = XGBClassifier(n_estimators=100, max_depth = 12 )<train_model>
from keras.layers import Embedding
from keras import layers
from keras.utils import to_categorical
from keras.regularizers import l2
from keras import Model, Input
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.models import load_model
xgb_clf.fit(x_train, y_train)
 xgb_clf.score(x_test, y_test )<predict_on_test>
from sklearn.model_selection import train_test_split
import tensorflow as tf
import keras
import regex as re
y_pred = xgb_clf.predict(x_test )<compute_test_metric>
train = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 train
print(classification_report(y_test, y_pred, target_names=label_dict.values()))<train_model>
test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 test
xgb_clf.fit(dtrain[features], label )<predict_on_test>
sub = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv")
 sub
y_test_hat = xgb_clf.predict(dtest[features] )<save_to_csv>
X = [str(train.iloc[i].keyword)+" "+str(train.iloc[i].location)+" "+train.iloc[i].text for i in range(len(train)) ]
X = [re.sub(r'[^a-zA-Z\ ]', '', s ).lower() for s in X]
X = np.array(X)
 y = [train.iloc[i].target for i in range(len(train)) ]
y = np.array(y)
 print(np.unique(y, return_counts=True))
X.shape, y.shape, X[0]
df_submit = pd.read_csv(submit_path, index_col=0)
 df_submit['Cover_Type'] =y_test_hat
df_submit.to_csv('submit_kaggle.csv' )<import_modules>
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.1)
 X_train.shape, X_val.shape, y_train.shape, y_val.shape
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import MinMaxScaler
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.pipeline import Pipeline
from xgboost import XGBClassifier<load_from_csv>
_, counts = np.unique(y_train, return_counts=True)
 class_weight = [np.size(y_train)/c for c in counts]
counts, class_weight
test = pd.read_csv(".. /input/forest-cover-type-prediction/test.csv")
 train = pd.read_csv(".. /input/forest-cover-type-prediction/train.csv" )<count_missing_values>
vectorizer = TextVectorization(max_tokens=20000, output_sequence_length=200)
 text_ds = tf.data.Dataset.from_tensor_slices(X_train ).batch(128)
 vectorizer.adapt(X_train )
train.isnull().sum()<count_missing_values>
output = vectorizer(np.array([["the cat sat on the"]]))
output.numpy() [0, :5]
train.isnull().sum()<prepare_x_and_y>
voc = vectorizer.get_vocabulary()
word_index = dict(zip(voc, range(2, len(voc))))
X_train=train.drop(['Id', 'Cover_Type'],axis=1)
 y_train=train.Cover_Type
X_test=test.drop('Id',axis=1)
 test_id=test.Id<normalization>
test = [b"the", b"cat", b"sat", b"on", b"the"]
[word_index[w] for w in test]
scaler = MinMaxScaler()
Xs_train = scaler.fit_transform(X_train)
 Xs_test = scaler.transform(X_test)
<train_on_grid>
path_to_glove_file = "/kaggle/input/glove6b/glove.6B.100d.txt"
embeddings_index = {}
with open(path_to_glove_file)as f:
for line in f:
word, coefs = line.split(maxsplit=1)
 coefs = np.fromstring(coefs, "f", sep=" ")
 embeddings_index[word] = coefs
print("Found %s word vectors." % len(embeddings_index))
lr_pipe = Pipeline(
 steps = [ 
('scaler', MinMaxScaler()), 
('classifier', LogisticRegression(solver='lbfgs', n_jobs=-1))
] 
)
 lr_param_grid = {
'classifier__C': [1, 10, 100,1000],
}
np.random.seed(1)
 grid_search = GridSearchCV(lr_pipe, lr_param_grid, cv=5, refit='True')
 grid_search.fit(X_train, y_train)
 print(grid_search.best_score_)
 print(grid_search.best_params_)
 0.640079365079365 
<train_on_grid>
num_tokens = len(voc)+ 2
embedding_dim = 100
hits = 0
misses = 0
embedding_matrix = np.zeros(( num_tokens, embedding_dim))
for word, i in word_index.items() :
embedding_vector = embeddings_index.get(word.decode("utf-8"))
if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
hits += 1
else:
misses += 1
print("Converted %d words(%d misses)" %(hits, misses))
xrf_pipe = Pipeline(
 steps = [ 
('classifier', ExtraTreesClassifier(n_estimators=500,random_state=0))
] 
)
 param_grid = {
'classifier__min_samples_leaf': [1,4,7,8],
'classifier__max_depth': [31,34,36,39],
}
xrf_grid_search = GridSearchCV(xrf_pipe, param_grid, cv=5, refit='True', n_jobs=-1)
 xrf_grid_search.fit(X_train, y_train)
 print(xrf_grid_search.best_score_)
 print(xrf_grid_search.best_params_)
<compute_train_metric>
X_train = vectorizer(np.array([[s] for s in X_train])).numpy()
X_val = vectorizer(np.array([[s] for s in X_val])).numpy()
X_test = vectorizer(np.array([[s] for s in X_test])).numpy()
X_train.shape, X_val.shape, X_test.shape
xrf_model = xrf_grid_search.best_estimator_
cv_score = cross_val_score(xrf_model, X_train, y_train, cv = 5)
 print(cv_score)
 print(( cv_score.mean() , cv_score.std() * 2))<train_on_grid>
embedding_layer = Embedding(
 num_tokens,
embedding_dim,
embeddings_initializer=keras.initializers.Constant(embedding_matrix),
trainable=False,
)
rf_pipe = Pipeline(
 steps = [ 
('classifier', RandomForestClassifier(n_estimators=500,random_state=0))
] 
)
 param_grid = {
'classifier__min_samples_leaf': [1,4,7,8],
'classifier__max_depth': [31,34,36,39],
}
np.random.seed(1)
 rf_grid_search = GridSearchCV(rf_pipe, param_grid, cv=5, refit='True', n_jobs=-1)
 rf_grid_search.fit(X_train, y_train)
 print(rf_grid_search.best_score_)
 print(rf_grid_search.best_params_)
<compute_train_metric>
dropout_rate = 0.5
l2_rate = 1.e-6
rf_model = rf_grid_search.best_estimator_
cv_score = cross_val_score(rf_model, X_train, y_train, cv = 5)
 print(cv_score)
 print(( cv_score.mean() , cv_score.std() * 2))<train_on_grid>
int_sequences_input = keras.Input(shape=(None,), dtype="int64")
 embedded_sequences = embedding_layer(int_sequences_input)
 x = layers.Conv1D(128, 5, activation="relu", kernel_regularizer=l2(l2_rate))(embedded_sequences)
 x = layers.MaxPooling1D(5 )(x)
 x = layers.Dropout(dropout_rate )(x)
 x = layers.Conv1D(128, 5, activation="relu", kernel_regularizer=l2(l2_rate))(x)
 x = layers.MaxPooling1D(5 )(x)
 x = layers.Dropout(dropout_rate )(x)
 x = layers.Conv1D(128, 5, activation="relu", kernel_regularizer=l2(l2_rate))(x)
 x = layers.GlobalMaxPooling1D()(x)
 x = layers.Dropout(dropout_rate )(x)
 x = layers.Dense(128, activation="relu", kernel_regularizer=l2(l2_rate))(x)
 x = layers.Dropout(dropout_rate )(x)
 preds = layers.Dense(1, activation="sigmoid", kernel_regularizer=l2(l2_rate))(x)
 model = keras.Model(int_sequences_input, preds)
 model.summary()
xgd_pipe = Pipeline(
 steps = [ 
('classifier', XGBClassifier(n_estimators=50, subsample=0.5))
] 
)
 param_grid = {
'classifier__learning_rate' : [0.45],
'classifier__min_samples_split' : [8, 16, 32],
'classifier__min_samples_leaf' : [2],
'classifier__max_depth': [15]
}
np.random.seed(1)
 xgd_grid_search = GridSearchCV(xgd_pipe, param_grid, cv=5,
refit='True', verbose = 10, n_jobs=-1)
 xgd_grid_search.fit(X_train, y_train)
 print(xgd_grid_search.best_score_)
 print(xgd_grid_search.best_params_)
<compute_train_metric>
early_stopping = EarlyStopping(patience=10, verbose=1)
 model_checkpoint = ModelCheckpoint("best_model.h5", save_best_only=True, verbose=1 )
xgd_model = xgd_grid_search.best_estimator_
cv_score = cross_val_score(xgd_model, X_train, y_train, cv = 5)
 print(cv_score)
 print(( cv_score.mean() , cv_score.std() * 2))<predict_on_test>
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["acc"])
 model.fit(X_train, y_train, batch_size=128, epochs=100, validation_data=(X_val, y_val),
class_weight={0: class_weight[0], 1: class_weight[1]},
callbacks=[early_stopping, model_checkpoint] )
test_pred = xrf_grid_search.predict(X_test )<prepare_output>
best_model = load_model("best_model.h5" )
submission = pd.DataFrame({
'Id':test_id,
'Cover_Type':test_pred
})
 submission.head()<save_to_csv>
y_pred = best_model.predict(X_test, verbose=1, batch_size=128 )
submission.to_csv('my_submission.csv', index=False )<set_options>
for i in range(len(y_pred)) :
sub.iloc[i].target = y_pred[i]
sub
<load_from_csv><EOS> 
sub.to_csv("submission.csv", index=False )