Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<prepare_x_and_y>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import re
import spacy
import nltk
import pickle
from nltk.tokenize import RegexpTokenizer
from scipy import sparse
from sklearn.naive_bayes import BernoulliNB
from sklearn.linear_model import LogisticRegressionCV
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import cross_val_score
from sklearn.metrics import f1_score
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer
X_train, X_val, y_train, y_val = type_select(types = '3JHN')
<define_search_space>
train = pd.read_csv(r'.. /input/nlp-getting-started/train.csv')
 test = pd.read_csv(r'.. /input/nlp-getting-started/test.csv')
 train.head()
model_parameters = {'n_estimators': [50, 100, 150, 200, 250, 300],
'max_depth':[5,7,9,11], 'learning_rate': [0.07, 0.1, 0.15],
'gamma': [0, 0.0001, 0.001, 0.01],
'subsample': [0.5, 0.8, 1], 'colsample_bytree': [0.5, 0.66, 1]
}
fit_params = {'eval_metric': 'mae',
'early_stopping_rounds': 5,
'eval_set': [(X_val, y_val)]}<init_hyperparams>
plt.bar(train['target'].value_counts().index, train['target'].value_counts().values )
best_parameters = {'base_score': 0.5,
'booster': 'gbtree',
'colsample_bylevel': 1,
'colsample_bynode': 1,
'colsample_bytree': 1,
'gamma': 0,
'importance_type': 'gain',
'learning_rate': 0.15,
'max_delta_step': 0,
'max_depth': 11,
'min_child_weight': 1,
'missing': None,
'n_estimators': 300,
'n_jobs': 4,
'nthread': None,
'objective': 'reg:squarederror',
'random_state': 0,
'reg_alpha': 0,
'reg_lambda': 1,
'scale_pos_weight': 1,
'seed': None,
'silent': None,
'subsample': 0.8,
'verbosity': 1
}<train_model>
train['location'].value_counts(dropna=False )
<prepare_x_and_y>
train_for_plot = train.fillna('NOINFO')
 locations = train_for_plot['location'].value_counts(dropna=False)
 freq_locations = list(locations.index )
def build_x_y_data(some_csv, coupling_type, n_atoms):
full = build_couple_dataframe(some_csv, structures_csv, coupling_type, n_atoms=n_atoms)
 df = take_n_atoms(full, n_atoms)
 df = df.fillna(0)
 print(df.columns)
 if 'scalar_coupling_constant' in df:
X_data = df.drop(['scalar_coupling_constant'], axis=1 ).values.astype('float32')
 y_data = df['scalar_coupling_constant'].values.astype('float32')
 else:
X_data = df.values.astype('float32')
 y_data = None
return X_data, y_data<find_best_model_class>
frames = [train, test]
full_data = pd.concat(frames )
def train_and_predict_for_one_coupling_type(coupling_type, submission, n_atoms, random_state=128):
print(f'*** Training Model for {coupling_type} ***')
 X_data, y_data = build_x_y_data(train_csv, coupling_type, n_atoms)
 X_test, _ = build_x_y_data(test_csv, coupling_type, n_atoms)
 y_pred = np.zeros(X_test.shape[0], dtype='float32')
 model_ = XGBRegressor(**best_parameters)
 model_.fit(X_data, y_data)
 y_pred += model_.predict(X_test)
 submission.loc[test_csv['type'] == coupling_type, 'scalar_coupling_constant'] = y_pred<prepare_output>
locs = full_data['location'].value_counts(dropna=True )
submission = submission_csv.copy()
for coupling_type in model_params.keys() :
cv_score = train_and_predict_for_one_coupling_type(
 coupling_type, submission, n_atoms=model_params[coupling_type] )<save_to_csv>
locations_nan = locs[locs > 8]
print(locations_nan.index )
submission.to_csv(f'{SUBMISSIONS_PATH}/submission.csv' )<import_modules>
def change_location(dataset, name='dataset'):
dataset['location'] = dataset['location'].replace('United States', 'USA')
 dataset['location'] = dataset['location'].replace('US', 'USA')
 dataset['location'] = dataset['location'].replace('Worldwide', 'Anywhere')
 dataset['location'] = dataset['location'].replace('worldwide', 'Anywhere')
 dataset['location'] = dataset['location'].replace('Everywhere', 'Anywhere')
 dataset['location'] = dataset['location'].replace('California, USA', 'California')
 dataset['location'] = dataset['location'].replace('United Kingdom', 'UK')
 dataset['location'] = dataset['location'].replace('San Francisco, CA', 'San Francisco')
 dataset['location'] = dataset['location'].replace('Chicago, IL', 'Chicago')
 dataset['location'] = dataset['location'].replace('New York, NY', 'New York')
 dataset['location'] = dataset['location'].replace('Los Angeles, CA', 'Los Angeles')
 dataset['location'] = dataset['location'].replace('NYC', 'New York')
 dataset['location'] = dataset['location'].replace('New York City', 'New York')
 dataset['location'] = dataset['location'].replace('Earth', 'Anywhere')
 dataset['location'] = dataset['location'].replace('ss', 'Anywhere')
 dataset['location'] = dataset['location'].replace('304', 'Anywhere')
 dataset['location'] = dataset['location'].replace('World', 'Anywhere')
 dataset['location'] = dataset['location'].replace('??????', 'Anywhere')
 dataset['location'] = dataset['location'].replace('Planet Earth', 'Anywhere')
 dataset['location'] = dataset['location'].replace('Washington, D.C.', 'Washington, DC')
 dataset['location'] = dataset['location'].replace('Denver, Colorado', 'Denver')
 dataset['location'] = dataset['location'].replace('Denver, CO', 'Denver')
 dataset['location'] = dataset['location'].replace(' Road to the Billionaires Club', 'Anywhere')
 dataset['location'] = dataset['location'].replace('Pennsylvania, USA', 'Pennsylvania')
 dataset['location'] = dataset['location'].replace('Paterson, New Jersey', 'Paterson')
 locations = dataset['location'].value_counts(dropna=True)
 anywhere_locations = locations[locations <= 8]
for loc in list(anywhere_locations.index):
dataset['location'] = dataset['location'].replace(loc, 'noinfo')
 dataset['location'].fillna('noinfo', inplace=True)
 change_location(train, 'train')
 change_location(test, 'test')
 frames = [train, test]
full_data = pd.concat(frames)
 print(full_data['location'].value_counts() )
print(tf.__version__)
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
keywords = train_for_plot['keyword'].value_counts(dropna=False)
 keywords
train =pd.read_csv(os.path.join(dirname,'train.csv'))
test =pd.read_csv(os.path.join(dirname,'test.csv'))
sample_submission =pd.read_csv(os.path.join(dirname,'sample_submission.csv'))<prepare_x_and_y>
train['target'].value_counts() [1] / train['target'].value_counts() [0]
X_train=train.drop('label',axis=1)
 Y_train=train.label
X_test = test.drop('id', axis = 1)
<feature_engineering>
np.random.seed(144)
 train.fillna('noinfo', inplace=True)
 test.fillna('noinfo', inplace=True)
 shuffled_train = train.iloc[np.random.permutation(len(train)) ]
max_iter = 100
cv = 5
clf = LogisticRegressionCV(cv = cv, n_jobs = -1, max_iter = max_iter, scoring='f1')
 X = shuffled_train[['location', 'keyword']]
Y = shuffled_train['target']
enc = OneHotEncoder()
X = enc.fit_transform(X)
 clf.fit(X, Y)
 print('Best mean score from 5 cross validations: ')
 print(clf.scores_[1].mean(axis=0 ).max() )
X_train = X_train / 255.0
X_test = X_test / 255.0<categorify>
np.random.seed(144)
 shuffled_train = train.iloc[np.random.permutation(len(train)) ]
clf = BernoulliNB(fit_prior = False)
 X = shuffled_train[['location', 'keyword']]
Y = shuffled_train['target']
enc = OneHotEncoder()
X = enc.fit_transform(X)
 score = cross_val_score(estimator=clf, X=X, y=Y, scoring='f1')
 print('Best mean score from 5 cross validations: ')
 print(score.mean() )
Y_train = to_categorical(Y_train,num_classes=10)
 display(Y_train )<split>
train['text'] = train['text'].str.lower()
test['text'] = test['text'].str.lower()
X_train,X_val,y_train,y_val=train_test_split(X_train,Y_train,random_state=42,test_size=0.10)
<choose_model_class>
train['text'] = train['text'].apply(lambda x:
re.sub(r'https?://\S+|www\.\S+','', x))
test['text'] = test['text'].apply(lambda x:
re.sub(r'https?://\S+|www\.\S+','', x))
kernel_size_3 =(3,3)
 kernel_size_5 =(5,5)
 filters_32 = 32
filters_64 = 64
filters_128 = 128
filters_256 = 256
model = Sequential()
model.add(Conv2D(filters_64, kernel_size_3, activation='relu', input_shape=(28,28,1),padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(filters_64, kernel_size_3, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(filters_64, kernel_size_3, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPool2D(( 2, 2)))
 model.add(Dropout(0.3))
model.add(Conv2D(filters_128, kernel_size_5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(filters_128, kernel_size_5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(filters_128, kernel_size_5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPool2D(( 2, 2)))
 model.add(Dropout(0.3))
model.add(Conv2D(filters_256, kernel_size_5, activation='relu'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPool2D(( 2, 2)))
 model.add(Dropout(0.3))
model.add(Flatten())
 model.add(Dense(256, activation='relu',kernel_regularizer=regularizers.l2(0.02)))
 model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(Dropout(0.3))
model.add(Dense(256, activation='relu',kernel_regularizer=regularizers.l2(0.02)))
 model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(Dense(512, activation='relu',kernel_regularizer=regularizers.l2(0.02)))
 model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(Dropout(0.3))
model.add(Dense(10, activation='softmax'))<choose_model_class>
train['text'] = train['text'].apply(lambda x:
re.sub(r'@[A-Za-z0-9]+','', x))
test['text'] = test['text'].apply(lambda x:
re.sub(r'@[A-Za-z0-9]+','', x))
train['text'] = train['text'].apply(lambda x:
re.sub(r'
test['text'] = test['text'].apply(lambda x:
re.sub(r'
model.compile(loss='categorical_crossentropy',optimizer=Adadelta(learning_rate=1.0, rho=0.95),metrics=['accuracy'] )<train_model>
train['text'] = train['text'].apply(lambda x:
re.sub(r'[^\w\s]','', x))
test['text'] = test['text'].apply(lambda x:
re.sub(r'[^\w\s]','', x))
history = model.fit(X_train, y_train, batch_size = 256, epochs = 10, validation_data =(X_val, y_val), verbose = 2 )<train_model>
w_tokenizer = nltk.tokenize.WhitespaceTokenizer()
lemmatizer = nltk.stem.WordNetLemmatizer()
def lemmatize_text(text):
return ' '.join([lemmatizer.lemmatize(w)for w in w_tokenizer.tokenize(text)])
 train['text'] = train.text.apply(lambda x: lemmatize_text(x))
test['text'] = test.text.apply(lambda x: lemmatize_text(x))
datagen = ImageDataGenerator(
 rotation_range=10,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range = 10,
horizontal_flip = False,
zoom_range = 0.15)
 datagen.fit(X_train )<define_variables>
all_stopwords = set(stopwords.words('english'))
BATCH_SIZE = 512
EPOCHS = 50<train_model>
def stopword_count(data, column):
count_dict = dict.fromkeys(all_stopwords, 0)
 def row_count(row):
for word in row.split() :
if word in all_stopwords:
count_dict[str(word)] += 1
data[column].apply(lambda x: row_count(x))
return count_dict
history = model.fit_generator(datagen.flow(X_train,y_train, batch_size=BATCH_SIZE),
epochs = EPOCHS,
shuffle=True,
validation_data =(X_val,y_val),
verbose = 1,
steps_per_epoch=X_train.shape[0] // BATCH_SIZE )<predict_on_test>
stopwords = stopword_count(train, 'text')
 stopwords = pd.Series(stopwords, index=stopwords.keys())
 stopwords = stopwords[stopwords > 0]
stopwords = stopwords.sort_values(ascending=False )
y_pre_test=model.predict(X_val)
 y_pre_test=np.argmax(y_pre_test,axis=1)
 y_test=np.argmax(y_val,axis=1 )<compute_test_metric>
train['text'] = train['text'].apply(lambda x: ' '.join([word for word in x.split() if word not in(all_stopwords)]))
test['text'] = test['text'].apply(lambda x: ' '.join([word for word in x.split() if word not in(all_stopwords)]))
conf=confusion_matrix(y_test,y_pre_test)
 conf=pd.DataFrame(conf,index=range(0,10),columns=range(0,10))<define_variables>
eng_words = set(nltk.corpus.words.words())
 train['text'] = train['text'].apply(lambda x: ' '.join(w for w in x.split() if not any(j.isdigit() for j in w)))
 test['text'] = test['text'].apply(lambda x: ' '.join(w for w in x.split() if not any(j.isdigit() for j in w)) )
x=(y_pre_test-y_test!=0 ).tolist()
x=[i for i,l in enumerate(x)if l!=False]<predict_on_test>
vect = CountVectorizer(min_df=3, ngram_range=(1,1))
enc = OneHotEncoder()
full_data = pd.concat(( train, test))
enc.fit(full_data[['location', 'keyword']])
 vect.fit(full_data['text'])
 print(len(vect.get_feature_names()))
print(len(enc.get_feature_names()))
results = model.predict(X_test)

results = np.argmax(results,axis = 1 )<save_to_csv>
np.random.seed(15)
 shuffled_train = train.iloc[np.random.permutation(len(train)) ]
shuffled_test = test.iloc[np.random.permutation(len(test)) ]
n_estimators = [60, 80, 100]
X_vect = vect.transform(shuffled_train['text'] ).todense()
X_onehot = enc.transform(shuffled_train[['location', 'keyword']] ).todense()
X = np.concatenate(( X_vect, X_onehot), axis=1)
 Y = shuffled_train['target']
for n_est in n_estimators:
clf = RandomForestClassifier(n_estimators=n_est, max_features='sqrt', verbose=0, n_jobs=-1)
 score = cross_val_score(estimator=clf, X=X, y=Y, scoring='f1', cv=3)
 print('f1 scores from 3 validations')
 print(score)
 print('
' )
sample_sub =pd.read_csv(os.path.join(dirname,'sample_submission.csv'))
sample_sub['label'] = results
sample_sub.to_csv('submission.csv',index=False )<import_modules>
vect = TfidfVectorizer(min_df=3, ngram_range=(1,1))
enc = OneHotEncoder()
full_data = pd.concat(( train, test))
enc.fit(full_data[['location', 'keyword']])
 vect.fit(full_data['text'])
 print(len(vect.get_feature_names()))
print(len(enc.get_feature_names()))
device = "cuda"<define_search_model>
np.random.seed(15)
 shuffled_train = train.iloc[np.random.permutation(len(train)) ]
clf = BernoulliNB(fit_prior = False)
 X_vect = vect.transform(shuffled_train['text'] ).todense()
X_onehot = enc.transform(shuffled_train[['location', 'keyword']] ).todense()
X = np.concatenate(( X_vect, X_onehot), axis=1)
 Y = shuffled_train['target']
X_sparsed = sparse.csr_matrix(X)
 score = cross_val_score(estimator=clf, X=X_sparsed, y=Y, scoring='f1', cv=5)
 print('Best scores from 3 cross validations: ')
 print(score )
class Sq_Ex_Block(nn.Module):
def __init__(self, in_ch, r):
super(Sq_Ex_Block, self ).__init__()
self.se = nn.Sequential(
 GlobalAvgPool() ,
nn.Linear(in_ch, in_ch//r),
nn.ReLU(inplace=True),
nn.Linear(in_ch//r, in_ch),
nn.Sigmoid() 
)
 def forward(self, x):
se_weight = self.se(x ).unsqueeze(-1 ).unsqueeze(-1)
 return x.mul(se_weight)
 class GlobalAvgPool(nn.Module):
def __init__(self):
super(GlobalAvgPool, self ).__init__()
def forward(self, x):
return x.view(*(x.shape[:-2]),-1 ).mean(-1)
 class SE_Net(nn.Module):
def __init__(self,in_channels):
super(SE_Net,self ).__init__()
self.c1 = nn.Conv2d(in_channels=in_channels, out_channels=64,kernel_size=3,stride=1,padding=0)
 self.bn1 = nn.BatchNorm2d(num_features=64,eps=1e-3,momentum=0.01)
 self.c2 = nn.Conv2d(64,64,3,1,0)
 self.bn2 = nn.BatchNorm2d(64,1e-3,0.01)
 self.c3 = nn.Conv2d(64,64,5,1,2)
 self.bn3 = nn.BatchNorm2d(64,1e-3,0.01)
 self.m1 = nn.MaxPool2d(2)
 self.d1 = nn.Dropout(0.4)
 self.c4 = nn.Conv2d(64,128,3,1,0)
 self.bn4 = nn.BatchNorm2d(128,1e-3,0.01)
 self.c5 = nn.Conv2d(128,128,3,1,0)
 self.bn5 = nn.BatchNorm2d(128,1e-3,0.01)
 self.c6 = nn.Conv2d(128,128,5,1,2)
 self.bn6 = nn.BatchNorm2d(128,1e-3,0.01)
 self.m2 = nn.MaxPool2d(2)
 self.d2 = nn.Dropout(0.4)
 self.c7 = nn.Conv2d(128,256,3,1,0)
 self.bn7 = nn.BatchNorm2d(256,1e-3,0.01)
 self.se3 = Sq_Ex_Block(in_ch=256,r=8)
 self.m3 = nn.MaxPool2d(2)
 self.d3 = nn.Dropout(0.4)
 self.fc1 = nn.Linear(256*1*1,256)
 self.bn8 = nn.BatchNorm1d(256,1e-3,0.01)
 self.out = nn.Linear(256,10)
 self.init_linear_weights()
def forward(self,x):
x = self.bn1(F.leaky_relu(self.c1(x),0.1))
x = self.bn2(F.leaky_relu(self.c2(x),0.1))
x = self.bn3(F.leaky_relu(self.c3(x),0.1))
x = self.d1(self.m1(x))
x = self.bn4(F.leaky_relu(self.c4(x),0.1))
x = self.bn5(F.leaky_relu(self.c5(x),0.1))
x = self.bn6(F.leaky_relu(self.c6(x),0.1))
x = self.d2(self.m2(x))
x = self.bn7(F.leaky_relu(self.c7(x),0.1))
x = self.se3(x)
 x = self.d3(self.m3(x))
x = x.view(-1, 256*1*1)
 x = self.bn8(F.leaky_relu(self.fc1(x),0.1))
return self.out(x)
 def init_linear_weights(self):
nn.init.kaiming_normal_(self.fc1.weight, mode='fan_in')
 nn.init.kaiming_normal_(self.out.weight, mode='fan_in' )<categorify>
np.random.seed(15)
 shuffled_train = train.iloc[np.random.permutation(len(train)) ]
shuffled_test = test.iloc[np.random.permutation(len(test)) ]
max_iter = 400
X_vect = vect.transform(shuffled_train['text'] ).todense()
X_onehot = enc.transform(shuffled_train[['location', 'keyword']] ).todense()
X = np.concatenate(( X_vect, X_onehot), axis=1)
 Y = shuffled_train['target']
X_sparsed = sparse.csr_matrix(X)
 clf = LogisticRegressionCV(n_jobs = -1, max_iter = max_iter, scoring='f1')
 score = cross_val_score(estimator=clf, X=X_sparsed, y=Y, scoring='f1', cv=5)
 print('scores from 5 cross validations: ')
 print(score)
 print('
' )
trans = transforms.Compose([
transforms.RandomAffine(degrees=10,translate=(0.15,0.15),scale=[0.9,1.1],shear=5),
transforms.ToTensor() ,
])
 trans_val = transforms.Compose([
transforms.ToTensor() ,
])
 trans_test = transforms.Compose([
transforms.ToTensor() ,
] )<load_from_csv>
np.random.seed(15)
 shuffled_train = train.iloc[np.random.permutation(len(train)) ]
shuffled_test = test.iloc[np.random.permutation(len(test)) ]
max_iter = 70000
X_vect = vect.transform(shuffled_train['text'] ).todense()
X_onehot = enc.transform(shuffled_train[['location', 'keyword']] ).todense()
X = np.concatenate(( X_vect, X_onehot), axis=1)
 Y = shuffled_train['target']
X_sparsed = sparse.csr_matrix(X)
 kernels = ['linear', 'rbf', 'poly']
for kernel in kernels:
clf = SVC(max_iter = max_iter, kernel=kernel)
 score = cross_val_score(estimator=clf, X=X_sparsed, y=Y, scoring='f1', cv=5)
 print(kernel)
 print(score)
 print('
' )
global_data = pd.read_csv("/kaggle/input/Kannada-MNIST/train.csv")
 global_data_test = pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv")
 class KMnistDataset(Dataset):
def __init__(self,data_len=None, is_validate=False,validate_rate=None,indices=None, data=None):
self.is_validate = is_validate
self.data = global_data
if data_len == None:
data_len = len(self.data)
 self.indices = indices
if self.is_validate:
self.len = int(data_len*validate_rate)
 self.offset = int(data_len*(1-validate_rate))
self.transform = trans_val
else:
self.len = int(data_len*(1-validate_rate))
self.offset = 0
self.transform = trans
def __getitem__(self, idx):
idx += self.offset
idx = self.indices[idx]
img = self.data.iloc[idx, 1:].values.astype(np.uint8 ).reshape(( 28, 28))
label = self.data.iloc[idx, 0]
img = Image.fromarray(img)
 img = self.transform(img)
 label = torch.as_tensor(label, dtype=torch.uint8)
 return img, label
def __len__(self):
return self.len
class TestDataset(Dataset):
def __init__(self,data_len=None):
self.data = global_data_test
self.transform = trans_test
if data_len == None:
self.len = len(self.data)
 def __getitem__(self, idx):
img = self.data.iloc[idx, 1:].values.astype(np.uint8 ).reshape(( 28, 28))
img = Image.fromarray(img)
 img = self.transform(img)
 return img, torch.Tensor([])
 def __len__(self):
return self.len<init_hyperparams>
from datetime import datetime
batch_size = 1024
num_workers = 8
epochs = 70
lr = 1e-3
val_period = 1
val_rate = 0.1<choose_model_class>
vect = CountVectorizer(min_df=3, ngram_range=(1,1))
enc = OneHotEncoder()
full_data = pd.concat(( train, test))
enc.fit(full_data[['location', 'keyword']])
 vect.fit(full_data['text'])
 print(len(vect.get_feature_names()))
print(len(enc.get_feature_names()))
model = SE_Net(in_channels=1)
 if device == "cuda":
model.cuda()
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters() ,lr=lr,betas=(0.9,0.99))
lr_scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, verbose=True, patience=15,factor=0.1 )<create_dataframe>
max_iter = 400
X_vect_train = vect.transform(train['text'] ).todense()
X_onehot_train = enc.transform(train[['location', 'keyword']] ).todense()
X_train = np.concatenate(( X_vect_train, X_onehot_train), axis=1)
 Y_train = train['target']
X_vect_test = vect.transform(test['text'] ).todense()
X_onehot_test = enc.transform(test[['location', 'keyword']] ).todense()
X_test = np.concatenate(( X_vect_test, X_onehot_test), axis=1)
 X_sparsed_train = sparse.csr_matrix(X_train)
 X_sparsed_test = sparse.csr_matrix(X_test)
 start = datetime.now()
clf = LogisticRegressionCV(n_jobs = -1, max_iter = max_iter, scoring='f1')
 clf.fit(X_sparsed_train, Y_train)
 print('Training time')
 print(datetime.now() - start)
 start = datetime.now()
LogReg_predict = clf.predict_proba(X_sparsed_test)
 LogReg_predict = LogReg_predict[:, 1]
print('Predicting time')
 print(datetime.now() - start )
indices_len = len(global_data)
 indices = np.arange(indices_len)
 train_dataset = KMnistDataset(data_len=None,is_validate=False, validate_rate=val_rate,indices=indices)
 train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=num_workers)
 val_dataset = KMnistDataset(data_len=None,is_validate=True, validate_rate=val_rate, indices=indices)
 val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers )<train_model>
X_vect_train = vect.transform(train['text'] ).todense()
X_onehot_train = enc.transform(train[['location', 'keyword']] ).todense()
X_train = np.concatenate(( X_vect_train, X_onehot_train), axis=1)
 Y_train = train['target']
X_vect_test = vect.transform(test['text'] ).todense()
X_onehot_test = enc.transform(test[['location', 'keyword']] ).todense()
X_test = np.concatenate(( X_vect_test, X_onehot_test), axis=1)
 X_sparsed_train = sparse.csr_matrix(X_train)
 X_sparsed_test = sparse.csr_matrix(X_test)
 start = datetime.now()
clf = BernoulliNB(fit_prior = False)
 clf.fit(X_sparsed_train, Y_train)
 print('Training time')
 print(datetime.now() - start)
 start = datetime.now()
NaiveBayes_predict = clf.predict_proba(X_sparsed_test)
 NaiveBayes_predict = NaiveBayes_predict[:, 1]
print('Predicting time')
 print(datetime.now() - start )
min_loss = 10000
max_acc = 0
best_model_dict = None
print("Start training...")
 for ep in range(0,epochs+1):
model.train()
data_num = 0
for idx, data in enumerate(train_loader):
img, target = data
img, target = img.to(device), target.to(device,dtype=torch.long)
 pred = model(img)
 loss = criterion(pred,target)
 data_num += img.size(0)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
if ep!=0 and ep%val_period == 0:
model.eval()
acc = 0
val_loss = 0
data_num = 0
with torch.no_grad() :
for idx, data in enumerate(val_loader):
img, target = data
img, target = img.to(device), target.to(device,dtype=torch.long)
 pred = model(img)
 val_loss += criterion(pred, target ).item()
_,pred_class = torch.max(pred.data, 1)
 acc +=(pred_class == target ).sum().item()
data_num += img.size(0)
 acc /= data_num
val_loss /= data_num
lr_scheduler.step(val_loss)
 if optimizer.param_groups[0]['lr'] < 1e-4:
break
if acc >= max_acc:
max_acc = acc
min_loss = val_loss
best_model_dict = model.state_dict()
print("Episode:{}, Validation Loss:{},Accuracy:{:.4f}%,learning rate:{}" 
.format(ep,val_loss,acc*100,optimizer.param_groups[0]['lr']))
print("===================Best Model, Loss:{} Accuracy:{}==================".format(min_loss,max_acc))
print("====================================================================")
 torch.cuda.empty_cache()<train_model>
max_iter = 70000
X_vect_train = vect.transform(train['text'] ).todense()
X_onehot_train = enc.transform(train[['location', 'keyword']] ).todense()
X_train = np.concatenate(( X_vect_train, X_onehot_train), axis=1)
 Y_train = train['target']
X_vect_test = vect.transform(test['text'] ).todense()
X_onehot_test = enc.transform(test[['location', 'keyword']] ).todense()
X_test = np.concatenate(( X_vect_test, X_onehot_test), axis=1)
 X_sparsed_train = sparse.csr_matrix(X_train)
 X_sparsed_test = sparse.csr_matrix(X_test)
 start = datetime.now()
clf = SVC(max_iter = max_iter, kernel='linear')
 clf.fit(X_sparsed_train, Y_train)
 print('Training time')
 print(datetime.now() - start)
 start = datetime.now()
SVM_predict = clf.predict(X_sparsed_test)
 print('Predicting time')
 print(datetime.now() - start )
result = np.array([],dtype=np.int)
 test_dataset = TestDataset(data_len=None)
 test_loader = DataLoader(test_dataset, batch_size=256, shuffle=False, num_workers=8)
 test_model = SE_Net(in_channels=1)
 test_model.load_state_dict(best_model_dict)
 if device == "cuda":
test_model.cuda()
test_model.eval()
with torch.no_grad() :
for idx, data in enumerate(test_loader):
img = data[0].to(device)
 pred = test_model(img)
 _,pred_class = torch.max(pred.data, 1)
 result = np.concatenate([result,pred_class.cpu().numpy() ],axis=0)
 print("shape of the result:",np.shape(result))<save_to_csv>
max_iter = 80
X_vect_train = vect.transform(train['text'] ).todense()
X_onehot_train = enc.transform(train[['location', 'keyword']] ).todense()
X_train = np.concatenate(( X_vect_train, X_onehot_train), axis=1)
 Y_train = train['target']
X_vect_test = vect.transform(test['text'] ).todense()
X_onehot_test = enc.transform(test[['location', 'keyword']] ).todense()
X_test = np.concatenate(( X_vect_test, X_onehot_test), axis=1)
 start = datetime.now()
clf = RandomForestClassifier(n_estimators=max_iter, max_features='sqrt', verbose=0, n_jobs=-1)
 clf.fit(X_train, Y_train)
 print('Training time')
 print(datetime.now() - start)
 start = datetime.now()
RF_predict = clf.predict_proba(X_test)
 RF_predict = RF_predict[:, 1]
print('Predicting time')
 print(datetime.now() - start )
sample_sub=pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')
 sample_sub['label']=result
sample_sub.to_csv('submission.csv',index=False)
 sample_sub.head()<import_modules>
predict =(LogReg_predict + NaiveBayes_predict + RF_predict)/ 3
import csv
import numpy as np
import keras
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense,Dropout,Activation,BatchNormalization
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping
from keras import layers,regularizers
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
from keras.optimizers import Adam,RMSprop<prepare_x_and_y>
predict[predict >= 0.5] = int(1)
 predict[predict < 0.5] = int(0 )
decay=1e-4
xtrain = []
ytrain = []
xtest = []
xval = []
yval = []<load_from_csv>
result_df = pd.DataFrame(test['id'].values, columns=['id'])
 result_df['target'] = predict.astype(int)
 result_df.to_csv(path_or_buf=r'submission_Ensamble.csv', index=False )
<data_type_conversions><EOS> 
for name, predict in [('LogReg', LogReg_predict), 
('NaiveBayes', NaiveBayes_predict),('RF', RF_predict),('SVM', SVM_predict)]:
predict[predict >= 0.5] = int(1)
 predict[predict < 0.5] = int(0)
 result_df = pd.DataFrame(test['id'].values, columns=['id'])
 result_df['target'] = predict.astype(int)
 result_df.to_csv(path_or_buf=r'submission_' + name + '.csv', index=False )