Kaggle competition: Natural Language Processing with Disaster Tweets
Rankg_g_tansform = NamedOutTWrapper(QuantileTransformer(n_quantiles=100,random_state=0, output_distribution="normal"),columns= GENES+CELLS,inplace=True )<choose_model_class>
%matplotlib inline
nlp = spacy.load('en_core_web_lg')
 review_data = pd.read_csv('.. /input/nlp-getting-started/train.csv')
PCA_g_tansform = NamedOutTWrapper(PCA(20),columns= GENES,prefix ='_g' )<choose_model_class>
true_ratio=review_data.target.mean()
print('true_ratio is {0:.2f} %'.format(true_ratio*100))
PCA_c_tansform = NamedOutTWrapper(PCA(20),columns= CELLS,prefix ='_c' )<train_model>
X_train, X_test, y_train, y_test = train_test_split(vectorsF, review_data.target,
test_size=0.2, random_state=1)
 model = LinearSVC(random_state=1, dual=False)
 model.fit(X_train, y_train)
 print(f'Model test accuracy: {model.score(X_test, y_test)*100:.3f}%' )
class ColumnDropper(BaseEstimator, TransformerMixin):
def __init__(self, cols):
self.cols=cols
def fit(self, X, y = None):
return self
def transform(self, X, y = None):
return X.drop(self.cols,axis=1)
<drop_column>
clf1 = svm.SVC(kernel='linear')
 clf1.fit(X_train, y_train)
 print(f'Model test accuracy: {clf1.score(X_test, y_test)*100:.3f}%' )
CatDropper =ColumnDropper(cols=['cp_type','cp_time','cp_dose'] )<define_variables>
clf2 = svm.SVC(kernel='rbf')
 clf2.fit(X_train, y_train)
 print(f'Model test accuracy: {clf2.score(X_test, y_test)*100:.3f}%' )
transformers_list=[map_controls,map_dose,map_time,Rankg_g_tansform,CatDropper]<define_variables>
model = tf.keras.Sequential([
tf.keras.layers.Dense(48, activation='relu', input_shape=[300]),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
 ])
 model.compile(loss='binary_crossentropy', optimizer='adam',metrics=['accuracy'])
 model.summary()
exp_name = 'test_DAE_0.2_all_together'<create_dataframe>
num_epochs = 10
model.fit(X_train, y_train, epochs=num_epochs, validation_data=(X_test, y_test))
def run_inference(X_train,y_train,X_valid,y_valid,X_test,fold, seed,inference_only=False,**kwargs):
seed_everything(seed)
 if not inference_only:
train_dataset = MoADataset(X_train, y_train)
 valid_dataset = MoADataset(X_valid, y_valid)
 trainloader = torch.utils.data.DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
 validloader = torch.utils.data.DataLoader(valid_dataset, batch_size=BATCH_SIZE, shuffle=False)
 testdataset = TestDataset(X_test)
 testloader = torch.utils.data.DataLoader(testdataset, batch_size=BATCH_SIZE, shuffle=False)
 model = DAE_Model(
 num_features= X_train.shape[1] ,
num_targets= X_train.shape[1],
**kwargs 
)
 model.load_state_dict(torch.load(f".. /input/swap-dae-noise0-2/FOLD{fold}_{exp_name}.pth",map_location=torch.device('cpu')))
 model.to(DEVICE)
 if not inference_only:
oof = inference_infer_features_fn(model, validloader, DEVICE)
 else:
oof= 0
predictions = infer_features_fn(model, testloader, DEVICE)
 predictions = predictions
return oof, predictions<define_variables>
from sklearn.linear_model import Lasso
DEVICE =('cuda' if torch.cuda.is_available() else 'cpu')
 EPOCHS = 1000
BATCH_SIZE = 640
LEARNING_RATE = 2e-3
WEIGHT_DECAY = 1e-8
NFOLDS = 10
EARLY_STOPPING_STEPS = 10
EARLY_STOP = False
GAMMA=0.5
FACTOR=0.75
hidden_size=1100
hidden_size2=1300
PATIENCE=10
THRESHOLD = 5e-3<predict_on_test>
from sklearn.ensemble import RandomForestRegressor
def infer_features_fn(model, dataloader, device):
model.eval()
preds = []
for data in dataloader:
inputs = data['x'].to(device)
 with torch.no_grad() :
outputs = model(inputs,mode='get_features')
 preds.append(torch.cat(outputs,axis=1 ).detach().cpu().numpy())
 preds = np.concatenate(preds)
 return preds 
<load_from_csv>
from xgboost import XGBRegressor
SEED = [0]
train,target_cols = train_short_form_loader('.. /input/lish-moa/train_features.csv','.. /input/lish-moa/train_targets_scored.csv')
 test = test_short_form_loader(".. /input/lish-moa/test_features.csv")
 train = pd.concat([train,test])
 train[target_cols]= train[target_cols].fillna(0)
 test = train.copy()
oof = np.zeros(( len(train), len(target_cols)))
 predictions = np.zeros(( len(test), len(target_cols)))
 for seed in SEED:
train = multifold_indexer(train,target_cols,n_splits=NFOLDS)
 for fold in range(NFOLDS):
train_df = train[train['kfold'] != fold]
valid_df = train[train['kfold'] == fold]
feature_cols = [col for col in train_df.columns if not(col in target_cols+['kfold'])]
pipeline_val = make_pipeline(*transformers_list)
 X_train, y_train = train_df[feature_cols], train_df[target_cols]
X_valid, y_valid = valid_df[feature_cols], valid_df[target_cols].values
X_train = pipeline_val.fit_transform(X_train)
 X_train = X_train.values
X_valid = pipeline_val.transform(X_valid)
 valid_index = X_valid.index
X_valid = X_valid.values
y_train = y_train.values
X_test = test[feature_cols]
X_test = pipeline_val.transform(X_test ).values
pred_ = run_inference(X_train,y_train,X_valid,y_valid,X_test,fold, seed,inference_only=True)
 break<create_dataframe>
import tensorflow as tf
from tensorflow import keras
transformed_features = pd.DataFrame(pred_[1],index=test.index )<rename_columns>
train_length=round(review_data.shape[0]*0.9)
 test_length=review_data.shape[0]-train_length
training_sentences = []
training_labels = []
testing_sentences = []
testing_labels = []
for i in range(train_length):
training_sentences.append(review_data.text[i])
 training_labels.append(review_data.target[i])
 for i in range(train_length, review_data.shape[0]):
testing_sentences.append(review_data.text[i])
 testing_labels.append(review_data.target[i])
 training_labels_final = np.array(training_labels)
 testing_labels_final = np.array(testing_labels )
transformed_features.columns = [str(i)for i in range(len(transformed_features.columns)) ]<data_type_conversions>
vocab_size = 10000
embedding_dim = 32
max_length = 25
trunc_type='post'
oov_tok = "<OOV>"
tokenizer = Tokenizer(num_words = vocab_size, oov_token=oov_tok)
 tokenizer.fit_on_texts(training_sentences)
 word_index = tokenizer.word_index
sequences = tokenizer.texts_to_sequences(training_sentences)
 padded = pad_sequences(sequences,maxlen=max_length, truncating=trunc_type)
 testing_sequences = tokenizer.texts_to_sequences(testing_sentences)
 testing_padded = pad_sequences(testing_sequences,maxlen=max_length )
corr_matrix = transformed_features.corr().abs()
sol =(corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1 ).astype(np.bool)) 
.stack() 
.sort_values(ascending=False))<count_values>
reverse_word_index = dict([(value, key)for(key, value)in word_index.items() ])
 def decode_review(text):
return ' '.join([reverse_word_index.get(i, '?')for i in text])
 i=8
print(decode_review(padded[16]))
print(training_sentences[i] )
sum(sol > 0.5 )<randomize_order>
model = tf.keras.Sequential([
tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(16, activation='relu'),
tf.keras.layers.Dense(8, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
 ])
 model.compile(loss='binary_crossentropy', optimizer='adam',metrics=['accuracy'])
 model.summary()
multin = sol.loc[sol > 0.5].index<count_unique_values>
num_epochs = 10
model.fit(padded, training_labels_final, epochs=num_epochs, validation_data=(testing_padded, testing_labels_final))
len(set(multin.get_level_values(0 ).unique().tolist() + multin.get_level_values(1 ).unique().tolist()))<import_modules>
test_data=pd.read_csv('.. /input/nlp-getting-started/test.csv' )
from sklearn.base import BaseEstimator, TransformerMixin<categorify>
y=clf2.predict(vectorsT )
<set_options><EOS> 
submission_df = pd.DataFrame({'Id':test_data['id'],'target':y})
 submission_df.to_csv('submission.csv',index=False)