Kaggle competition: Kannada MNIST
members = pd.read_csv('.. /input/members_v3.csv')
 train = pd.merge(train, members, how='left', on='msno')
 test = pd.merge(test, members, how='left', on='msno')
 members = []; print('members merge...' )<categorify>
def get_data_labels(csv,label):
fileraw = pd.read_csv(csv)
 labels = fileraw[label].to_numpy()
data = fileraw.drop([label],axis=1 ).to_numpy(dtype=np.float32 ).reshape(( fileraw.shape[0],28,28))
data = np.expand_dims(data, axis=1)
 return data, labels
gender = {'male':1, 'female':2}
train['gender'] = train['gender'].map(gender)
 test['gender'] = test['gender'].map(gender)
 train = train.fillna(0)
 test = test.fillna(0 )<categorify>
train_data, train_labels = get_data_labels(DATAPATH/'train.csv','label')
 test_data, test_labels = get_data_labels(DATAPATH/'test.csv','id')
 other_data, other_labels = get_data_labels(DATAPATH/'Dig-MNIST.csv','label' )
train = train.fillna(0)
 test = test.fillna(0)
 train.head()<import_modules>
plt.title(f'Training Label: {train_labels[5]}')
 plt.imshow(train_data[8,0],cmap='gray');
from keras.models import Sequential
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import LambdaCallback
from keras.layers.normalization import BatchNormalization
from keras.optimizers import SGD
from keras.constraints import maxnorm
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Lambda
from keras.layers.core import Dropout
from keras import regularizers
from keras.models import Model, load_model
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.layers import Embedding
from keras.layers import Conv1D, GlobalMaxPooling1D
from keras.datasets import imdb
from keras.models import Sequential
from keras.layers import Dense, Activation, MaxPooling1D 
<prepare_x_and_y>
np.random.seed(60)
 ran_10_pct_idx =(np.random.random_sample(train_labels.shape)) <.001
train_90_labels = train_labels[np.invert(ran_10_pct_idx)]
train_90_data = train_data[np.invert(ran_10_pct_idx)]
valid_10_labels = train_labels[ran_10_pct_idx]
valid_10_data = train_data[ran_10_pct_idx]
cols = [c for c in train.columns if c not in ['is_churn','msno']]
X_train = StandardScaler().fit_transform(train[cols].as_matrix())
 y_train = train['is_churn'].as_matrix()
X_test = StandardScaler().fit_transform(test[cols].as_matrix() )<choose_model_class>
train_ds = ArrayDataset(train_90_data,train_90_labels)
 valid_ds = ArrayDataset(valid_10_data,valid_10_labels)
 other_ds = ArrayDataset(other_data, other_labels)
 test_ds = ArrayDataset(test_data, test_labels )
lsize = 128
model = Sequential()
model.add(Dense(lsize, input_dim=int(X_train.shape[1]),activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(rate=0.25))
model.add(Dense(int(lsize/2), activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(rate=0.25))
model.add(Dense(int(lsize/4),kernel_regularizer=regularizers.l2(0.1), activation='relu'))
model.add(Dropout(rate=0.1))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adadelta', metrics=['accuracy'])
 model.summary()<train_model>
bs = 256
databunch = DataBunch.create(train_ds, valid_ds, test_ds=test_ds, bs=bs )
history = model.fit(X_train, y_train, epochs=10, batch_size=1026,
validation_split=0.2, verbose=1)
<save_to_csv>
def conv2(ni,nf,stride=2,ks=5): return conv_layer(ni,nf,stride=stride,ks=ks)
predictions = model.predict(X_test)
 test['is_churn'] = predictions.clip(0.+1e-15, 1-1e-15)
 test[['msno','is_churn']].to_csv('submission_NN.csv', index=False )<set_options>
best_architecture = nn.Sequential(
 conv2(1,32,stride=1,ks=5),
conv2(32,32,stride=1,ks=5),
conv2(32,32,stride=2,ks=8),
nn.Dropout(0.2),
conv2(32,64,stride=1,ks=5),
conv2(64,64,stride=1,ks=5),
conv2(64,64,stride=2,ks=5),
nn.Dropout(0.6),
Flatten() ,
nn.Linear(3136, 256),
relu(inplace=True),
nn.BatchNorm1d(256),
nn.Dropout(0.2),
nn.Linear(256,10)
 )
if torch.cuda.is_available() :
device = torch.device("cuda")
 print('We will use the GPU:', torch.cuda.get_device_name(0))
else:
print('No GPU available, using the CPU instead.')
 device = torch.device("cpu" )<load_from_csv>
learn = Learner(databunch, best_architecture, loss_func = nn.CrossEntropyLoss() , metrics=[accuracy] )
df_train=pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 df_test=pd.read_csv("/kaggle/input/nlp-getting-started/test.csv" )<categorify>
learn.fit_one_cycle(53)
def preprocess(text):
text=text.lower()
text = re.sub(r'https?:\/\/.*[\r
]*', '', text)
 text = re.sub(r'http?:\/\/.*[\r
]*', '', text)
 text=text.replace(r'&amp;?',r'and')
 text=text.replace(r'&lt;',r'<')
 text=text.replace(r'&gt;',r'>')
 text = re.sub(r"(?:\@)\w+", '', text)
 text=text.encode("ascii",errors="ignore" ).decode()
text=re.sub(r'[:"
text=re.sub(r'[!]+','!',text)
 text=re.sub(r'[?]+','?',text)
 text=re.sub(r'[.]+','.',text)
 text=re.sub(r"'","",text)
 text=re.sub(r"\(","",text)
 text=re.sub(r"\)","",text)
 text=" ".join(text.split())
 return text
df_train['text'] = df_train['text'].apply(preprocess)
 df_test['text'] = df_test['text'].apply(preprocess)
 df_train=df_train[df_train["text"]!='']<count_values>
preds, ids = learn.get_preds(DatasetType.Test)
 y = torch.argmax(preds, dim=1 )
df_train["target"].value_counts()<define_variables>
submission = pd.DataFrame({ 'id': ids,'label': y })
 submission.to_csv(path_or_buf ="submission.csv", index=False )