Kaggle competition: Digit Recognizer
svm_clf = SVC(gamma = "auto")
 svm_scores = cross_val_score(svm_clf, X_train, y_train, cv = 10)
 svm_scores.mean()<predict_on_test>
%matplotlib inline
svm_clf.fit(X_train, y_train)
 y_pred = svm_clf.predict(X_test )<save_to_csv>
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv' )
output = pd.DataFrame({'PassengerId': test_data.index, 'Survived': y_pred})
 output.to_csv('submission.csv', index = False)
 print("Your submission was successfully saved!" )<install_modules>
X = train.drop('label', axis = 1)
 y = train['label']
del train
!pip install tensorflow_hub
!pip install bert-for-tf2
!pip install tensorflow
!pip install sentencepiece
!pip install transformers<feature_engineering>
X = X / 255.0
test = test / 255.0
BertTokenizer = bert.bert_tokenization.FullTokenizer
bert_layer = hub.KerasLayer("https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/1", trainable=False)
 vocabulary_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
to_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = BertTokenizer(vocabulary_file, to_lower_case )<string_transform>
labels = pd.get_dummies(y)
 labels = labels.values
stopwrds = set(stopwords.words('english'))
TAG_RE = re.compile(r'<[^>]+>')
 def remove_tags(text):
return TAG_RE.sub('', text)
 def preprocess_text(sen):
sentence = emoji.demojize(sen)
 sentence = re.sub(r"http:\S+",'',sentence)
 sentence = ' '.join([x for x in nltk.word_tokenize(sentence)if x not in stopwrds])
 sentence = remove_tags(sentence)
 sentence = re.sub(r'['+string.punctuation+']','',sentence)
 sentence = re.sub(r"\s+[a-zA-Z]\s+", ' ', sentence)
 sentence = re.sub(r'\s+', ' ', sentence)
 return sentence.strip()<categorify>
X_train, X_val, y_train, y_val = train_test_split(X, labels, test_size = 0.1,random_state=42 )
def tokenize_bert(data):
tokenized = data.apply(( lambda x: tokenizer.convert_tokens_to_ids(['[CLS]'])+ tokenizer.convert_tokens_to_ids(tokenizer.tokenize(x))))
return tokenized
def pad_mask(data_tokenized,max_len):
padded = tf.keras.preprocessing.sequence.pad_sequences(data_tokenized, maxlen=max_len, dtype='int32', padding='post',value=0.0)
 masked = np.where(padded!=0,1,0)
 return padded, masked
def get_max_len(data):
max_len = 0
for val in data:
tmp = len(tokenizer.tokenize(val))
if tmp > max_len:
max_len = tmp
return max_len<categorify>
from keras.preprocessing.image import ImageDataGenerator
def encode(df):
tweet = tf.ragged.constant([tokenizer.convert_tokens_to_ids(tokenizer.tokenize(s)) for s in df])
 cls1 = [tokenizer.convert_tokens_to_ids(['[CLS]'])]*tweet.shape[0]
input_word_ids = tf.concat([cls1, tweet], axis=-1)
 input_mask = tf.ones_like(input_word_ids ).to_tensor()
type_cls = tf.zeros_like(cls1)
 type_tweets = tf.zeros_like(tweet)
 input_type_ids = tf.concat([type_cls, type_tweets], axis=-1 ).to_tensor()
inputs = {
'input_ids': input_word_ids.to_tensor() ,
'input_mask': input_mask,
'input_type_ids': input_type_ids}
return inputs<load_from_csv>
datagen = ImageDataGenerator(shear_range= 0.2, zoom_range= 0.2)
 datagen.fit(X_train )
finetune_train = pd.read_csv('/kaggle/input/twitter-sentiment-analysis-hatred-speech/train.csv',encoding="utf-8")
 finetune_test = pd.read_csv('/kaggle/input/twitter-sentiment-analysis-hatred-speech/test.csv',encoding="utf-8")
 finetune_train["hashtags"]=finetune_train["tweet"].apply(lambda x:re.findall(r"
finetune_test["hashtags"]=finetune_test["tweet"].apply(lambda x:re.findall(r"
finetune_train["hashtags"]=finetune_train["hashtags"].apply(lambda x: ' '.join(x))
finetune_test["hashtags"]=finetune_test["hashtags"].apply(lambda x: ' '.join(x))
finetune_train = finetune_train.rename(columns={'label':'target'})
 finetune_test = finetune_test.rename(columns={'label':'target'})
 finetune_train.info()<load_from_csv>
from keras.layers import Dropout, Conv2D, MaxPool2D, Dense, Flatten
from keras.models import Sequential
train = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv",encoding="utf-8")
 test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv",encoding="utf-8")
 train["hashtags"]=train["text"].apply(lambda x:re.findall(r"
test["hashtags"]=test["text"].apply(lambda x:re.findall(r"
train["hashtags"]=train["hashtags"].apply(lambda x: ' '.join(x))
test["hashtags"]=test["hashtags"].apply(lambda x: ' '.join(x))
train.info()<feature_engineering>
model = Sequential()
model.add(Conv2D(filters = 64, kernel_size = 5,activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 64, kernel_size = 5,activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.2))
model.add(Conv2D(filters = 32, kernel_size = 3,activation ='relu'))
model.add(Conv2D(filters = 32, kernel_size = 3,activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.2))
model.add(Dense(10, activation = "softmax"))
finetune_train["clean"] = finetune_train["tweet"].apply(lambda x: preprocess_text(x.lower()))
finetune_test["clean"] = finetune_test["tweet"].apply(lambda x: preprocess_text(x.lower()))
print("length of finetune train set:",len(finetune_train))
print("length of finetune test set:",len(finetune_test))<data_type_conversions>
model.compile(optimizer= 'adam', loss= 'categorical_crossentropy', metrics= ['accuracy'] )
train["clean"] = train["text"].apply(lambda x: preprocess_text(x.lower()))
test["clean"] = test["text"].apply(lambda x: preprocess_text(x.lower()))
print("length of train set:",len(train))
print("length of test set:",len(test))<string_transform>
performance = model.fit_generator(datagen.flow(X_train,y_train, batch_size=64), epochs = 30,
validation_data =(X_val,y_val), verbose = 3 )
def extract_features(df,test_df):
txt=' '.join(df[df["target"]==1]["clean"])
 disaster_unigram=nltk.FreqDist(nltk.word_tokenize(txt))
txt=' '.join(df[df["target"]==0]["clean"])
 nondisaster_unigram=nltk.FreqDist(nltk.word_tokenize(txt))
txt=' '.join(df[df["target"]==1]["clean"])
 disaster_bigram=nltk.FreqDist(nltk.bigrams(nltk.word_tokenize(txt)))
 txt=' '.join(df[df["target"]==0]["clean"])
 nondisaster_bigram=nltk.FreqDist(nltk.bigrams(nltk.word_tokenize(txt)))
 txt=' '.join(df[df["target"]==1]["hashtags"])
 disaster_unigram_hash=nltk.FreqDist(nltk.word_tokenize(txt))
txt=' '.join(df[df["target"]==0]["hashtags"])
 nondisaster_unigram_hash=nltk.FreqDist(nltk.word_tokenize(txt))
df["unigram_disas"]=df["clean"].apply(lambda x: sum([disaster_unigram.get(wrd)for wrd in nltk.word_tokenize(x)if disaster_unigram.get(wrd)!=None])/len(disaster_unigram))
test_df["unigram_disas"]=test_df["clean"].apply(lambda x: sum([disaster_unigram.get(wrd)for wrd in nltk.word_tokenize(x)if disaster_unigram.get(wrd)!=None])/len(disaster_unigram))
df["unigram_nondisas"]=df["clean"].apply(lambda x: sum([nondisaster_unigram.get(wrd)for wrd in nltk.word_tokenize(x)if nondisaster_unigram.get(wrd)!=None])/len(nondisaster_unigram))
test_df["unigram_nondisas"]=test_df["clean"].apply(lambda x: sum([nondisaster_unigram.get(wrd)for wrd in nltk.word_tokenize(x)if nondisaster_unigram.get(wrd)!=None])/len(nondisaster_unigram))
df["unigram_disas_hash"]=df["hashtags"].apply(lambda x: sum([disaster_unigram_hash.get(wrd)for wrd in nltk.word_tokenize(x)if disaster_unigram_hash.get(wrd)!=None])/len(disaster_unigram_hash))
test_df["unigram_disas_hash"]=test_df["hashtags"].apply(lambda x: sum([disaster_unigram_hash.get(wrd)for wrd in nltk.word_tokenize(x)if disaster_unigram_hash.get(wrd)!=None])/len(disaster_unigram_hash))
df["unigram_nondisas_hash"]=df["hashtags"].apply(lambda x: sum([nondisaster_unigram_hash.get(wrd)for wrd in nltk.word_tokenize(x)if nondisaster_unigram_hash.get(wrd)!=None])/len(nondisaster_unigram_hash))
test_df["unigram_nondisas_hash"]=test_df["hashtags"].apply(lambda x: sum([nondisaster_unigram_hash.get(wrd)for wrd in nltk.word_tokenize(x)if nondisaster_unigram_hash.get(wrd)!=None])/len(nondisaster_unigram_hash))
df["bigram_disas"]=df["clean"].apply(lambda x: sum([disaster_bigram.get(wrd)for wrd in nltk.bigrams(nltk.word_tokenize(x)) if disaster_bigram.get(wrd)!=None])/len(disaster_bigram)if x.strip() !='' else 0)
 test_df["bigram_disas"]=test_df["clean"].apply(lambda x: sum([disaster_bigram.get(wrd)for wrd in nltk.bigrams(nltk.word_tokenize(x)) if disaster_bigram.get(wrd)!=None])/len(disaster_bigram)if x.strip() !='' else 0)
 df["bigram_nondisas"]=df["clean"].apply(lambda x: sum([nondisaster_bigram.get(wrd)for wrd in nltk.bigrams(nltk.word_tokenize(x)) if nondisaster_bigram.get(wrd)!=None])/len(nondisaster_bigram)if x.strip() !='' else 0)
 test_df["bigram_nondisas"]=test_df["clean"].apply(lambda x: sum([nondisaster_bigram.get(wrd)for wrd in nltk.bigrams(nltk.word_tokenize(x)) if nondisaster_bigram.get(wrd)!=None])/len(nondisaster_bigram)if x.strip() !='' else 0)
 return df,test_df<feature_engineering>
from sklearn.metrics import confusion_matrix, classification_report
finetune_train,finetune_test = extract_features(finetune_train,finetune_test)
 finetune_train.head(2 )<feature_engineering>
pred = model.predict(X_val )
train,test = extract_features(train,test)
 train.head(2 )<categorify>
pred_classes = np.argmax(pred,axis = 1)
 y_true = np.argmax(y_val,axis = 1 )
def build_bert(max_len):
input_ids = keras.layers.Input(shape=(max_len,), name="input_ids", dtype=tf.int32)
 input_typ = keras.layers.Input(shape=(max_len,), name="input_type_ids", dtype=tf.int32)
 input_mask = keras.layers.Input(shape=(max_len,), name="input_mask", dtype=tf.int32)
 input_features = keras.layers.Input(shape=(6,), name="input_features", dtype=tf.float32)
 bert_inputs = {"input_ids": input_ids, "input_mask": input_mask,'input_type_ids':input_typ}
bert_model = hub.KerasLayer("https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/1", trainable=True,name='keraslayer')
 pooled_output, _ = bert_model([input_ids, input_mask,input_typ])
 merge_two = concatenate([pooled_output, input_features])
 out = keras.layers.Dense(1, activation='sigmoid' )(merge_two)
 model = keras.Model(inputs=[bert_inputs,input_features],outputs=out)
 return model<categorify>
print(classification_report(y_true, pred_classes))
all_df = pd.concat([finetune_train,finetune_test])
 max_len = get_max_len(all_df["clean"])+ 1
encode_ds_all = encode(all_df["clean"] )<categorify>
test_preds = model.predict(test)
 test_preds = np.argmax(test_preds,axis = 1)
 test_preds = pd.Series(test_preds,name="Label" )
encode_ds_tr = {'input_ids':encode_ds_all["input_ids"][0:31962,:],
'input_mask':encode_ds_all["input_mask"][0:31962,:],
'input_type_ids':encode_ds_all["input_type_ids"][0:31962,:]}<categorify>
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),test_preds],axis = 1)
 submission.to_csv("cnn_mnist.csv",index=False )