Kaggle competition: Don't Get Kicked!
print('Train Keyword Distribution:
')
 print(train.keyword.value_counts())
 print('
', '-' * 50, '
')
 print('Test Keyword Distribution:
')
 print(test.keyword.value_counts() )<count_missing_values>
train_df=pd.read_csv('/kaggle/input/DontGetKicked/training.csv')
 train_df.head()
print('Count NaN:')
 print(train.isnull().sum() , '
')
 print('Percentage NaN:')
 print(train.isnull().sum() / len(train))<count_missing_values>
test_df=pd.read_csv('/kaggle/input/DontGetKicked/test.csv')
 test_df.head()
print('Count NaN:')
 print(test.isnull().sum() , '
')
 print('Percentage NaN:')
 print(test.isnull().sum() / len(test))<feature_engineering>
train_df.isnull().sum()
ids_with_target_error = [328,443,513,2619,3640,3900,4342,5781,6552,6554,6570,6701,6702,6729,6861,7226]
train.loc[train['id'].isin(ids_with_target_error),'target'] = 0<feature_engineering>
test_df.isnull().sum()
train.loc[train['keyword'].notnull() , 'text'] = train['keyword'] + ' ' + train['text']
test.loc[test['keyword'].notnull() , 'text'] = test['keyword'] + ' ' + test['text']
train[train['keyword'].notnull() ].head()<drop_column>
train_df['IsBadBuy'].value_counts()
train = train.drop(['id', 'keyword', 'location'], axis=1)
 test = test.drop(['keyword', 'location'], axis=1)
 train.head()<categorify>
train_df['Model'].value_counts()
stop_words = set(stopwords.words('english'))
tknzr = TweetTokenizer(strip_handles=True)
 corpus = []
def clean_text(text):

 text = re.sub(r"\x89Û_", "", text)
 text = re.sub(r"\x89ÛÒ", "", text)
 text = re.sub(r"\x89ÛÓ", "", text)
 text = re.sub(r"\x89ÛÏWhen", "When", text)
 text = re.sub(r"\x89ÛÏ", "", text)
 text = re.sub(r"China\x89Ûªs", "China's", text)
 text = re.sub(r"let\x89Ûªs", "let's", text)
 text = re.sub(r"\x89Û÷", "", text)
 text = re.sub(r"\x89Ûª", "", text)
 text = re.sub(r"\x89Û\x9d", "", text)
 text = re.sub(r"å_", "", text)
 text = re.sub(r"\x89Û¢", "", text)
 text = re.sub(r"\x89Û¢åÊ", "", text)
 text = re.sub(r"fromåÊwounds", "from wounds", text)
 text = re.sub(r"åÊ", "", text)
 text = re.sub(r"åÈ", "", text)
 text = re.sub(r"JapÌ_n", "Japan", text)
 text = re.sub(r"Ì©", "e", text)
 text = re.sub(r"å¨", "", text)
 text = re.sub(r"SuruÌ¤", "Suruc", text)
 text = re.sub(r"åÇ", "", text)
 text = re.sub(r"å£3million", "3 million", text)
 text = re.sub(r"åÀ", "", text)
 emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F"
u"\U0001F300-\U0001F5FF"
u"\U0001F680-\U0001F6FF"
u"\U0001F1E0-\U0001F1FF"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
 text = emoji_pattern.sub(r'', text)

text = re.sub(r'[0-9]', '', text)
 for p in string.punctuation.replace('!', ''):
text = text.replace(p, '')
 text = re.sub(r'http\S+', '', text)
 text = tknzr.tokenize(text)
 text = [w.lower() for w in text if not w in stop_words]
corpus.append(text)
 text = ' '.join(text)
 return text<feature_engineering>
train_df.drop('Model',axis=1,inplace=True)
 test_df.drop('Model',axis=1,inplace=True )
%%time
train['text'] = train['text'].apply(lambda s: clean_text(s))
test['text'] = test['text'].apply(lambda s: clean_text(s))
train.sample(10 )<feature_engineering>
train_df['Trim'].value_counts()
texts = train['text'].to_numpy()
word_freq = {}
for text in texts:
for word in text.split() :
word_freq[word] = word_freq.get(word, 0)+ 1<string_transform>
train_df.drop('Trim',axis=1,inplace=True)
 test_df.drop('Trim',axis=1,inplace=True )
MAX_SEQUENCE_LENGTH = 40
tokenizer = Tokenizer()
tokenizer.fit_on_texts(texts)
 sequences = tokenizer.texts_to_sequences(texts)
 word_index = tokenizer.word_index
num_words = len(word_index)+ 1
print('Found %s unique tokens.' %(num_words - 1))
data = pad_sequences(
 sequences,
maxlen=MAX_SEQUENCE_LENGTH,
padding='post',
truncating='post' 
)
 labels = train['target'].to_numpy()
print('Shape of data tensor:', data.shape)
 print('Shape of label tensor:', labels.shape )<prepare_x_and_y>
train_df['SubModel'].value_counts()
x_train = data
y_train = labels<feature_engineering>
train_df.drop('SubModel',axis=1,inplace=True)
 test_df.drop('SubModel',axis=1,inplace=True )
vectorizer = CountVectorizer()
x_train_vectorized = vectorizer.fit_transform(train['text'])
 print(vectorizer.get_feature_names() [2500:2600] )<prepare_x_and_y>
train_df['Color'].value_counts()
y_train_NB = np.array(train['target'] )<train_model>
test_df['Color'].value_counts()
model_NB = BernoulliNB(alpha=1.0)
 model_NB.fit(x_train_vectorized, y_train_NB)
 x_test_NB = vectorizer.transform(test['text'] )<load_pretrained>
train_df['Color'].fillna(value='Color_Unknown', inplace=True)
 test_df['Color'].fillna(value='Color_Unknown', inplace=True )
embeddings_df = pd.read_pickle('/kaggle/input/fasttext-twitter-derived-embeddings/twitter_derived_embeddings')
 embeddings_df.head()<categorify>
print("No.of null values in Color column"+str(train_df['Color'].isnull().sum()))
print("No.of null values in Color column"+str(test_df['Color'].isnull().sum()))
EMBEDDING_DIM = 400
fasttext_embedding_idx = {}
for idx, row in embeddings_df.iterrows() :
word = row[0]
embeddings = np.asarray(row[1], 'float32')
 fasttext_embedding_idx[word] = embeddings
fasttext_embedding_idx['earthquake'][:20]<feature_engineering>
train_df['Transmission'].value_counts()
iv = 0
oov = 0
embedding_idx = fasttext_embedding_idx
embedding_matrix = np.zeros(( num_words, EMBEDDING_DIM))
for word, i in word_index.items() :
embedding_vector = embedding_idx.get(word)
 if embedding_vector is not None:
iv += 1
embedding_matrix[i] = embedding_vector
else: oov += 1
print('%i tokens in vocab, %i tokens out of vocab' %(iv, oov))<choose_model_class>
test_df['Transmission'].value_counts()
model_NN = tf.keras.Sequential()
embedding_layer = Embedding(
 num_words,
EMBEDDING_DIM,
weights = [embedding_matrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=False
)<choose_model_class>
train_df[train_df['Transmission']=='Manual']
model_NN = tf.keras.Sequential([
tf.keras.layers.Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32'),
embedding_layer,
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)) ,
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dropout(DROPOUT),
tf.keras.layers.Dense(1, activation='sigmoid')
 ])
 loss = tf.keras.losses.BinaryCrossentropy(
 from_logits=False,
name='binary_crossentropy' 
)
 optimizer = tf.keras.optimizers.Adam(LEARN_RATE)
 model_NN.compile(
 loss=loss,
optimizer=optimizer,
metrics=['accuracy']
)<train_model>
train_df['Transmission'].replace("Manual","MANUAL",inplace=True )
model_NN.fit(x_train, y_train, epochs=EPOCHS, validation_split=SPLIT, batch_size=BATCH_SIZE )<feature_engineering>
train_df['Transmission'].value_counts()
test_tokenizer = Tokenizer()
test_texts = test['text'].to_numpy()
test_tokenizer.fit_on_texts(texts)
 test_sequences = test_tokenizer.texts_to_sequences(test_texts)
 test_word_index = test_tokenizer.word_index
print('Found %s unique tokens.' % len(word_index))
test_data = pad_sequences(test_sequences, maxlen=MAX_SEQUENCE_LENGTH)
 test_embedding_matrix = np.zeros(( len(test_word_index)+ 1, EMBEDDING_DIM))
for word, i in test_word_index.items() :
embedding_vector = embedding_idx.get(word)
 if embedding_vector is not None:
test_embedding_matrix[i] = embedding_vector<predict_on_test>
train_df['Transmission'].fillna(value="Transmission_unk",inplace=True)
 test_df['Transmission'].fillna(value="Transmission_unk",inplace=True )
raw_lstm_preds = model_NN.predict(test_data)
 lstm_preds = raw_lstm_preds.round().astype(int )<load_from_csv>
train_df['WheelTypeID'].value_counts()
train = pd.read_csv(os.path.join(DATA_PATH, 'train.csv'))
test = pd.read_csv(os.path.join(DATA_PATH, 'test.csv'))<import_modules>
train_df.drop('WheelTypeID',inplace=True,axis=1)
 test_df.drop('WheelTypeID',inplace=True,axis=1 )
import tensorflow_hub as hub<load_from_url>
train_df['WheelType'].value_counts()
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py 
<choose_model_class>
test_df['WheelType'].value_counts()
%%time
max_seq_length = 180
bert_url = "https://tfhub.dev/tensorflow/bert_en_cased_L-12_H-768_A-12/1"
input_word_ids = tf.keras.layers.Input(shape=(max_seq_length,), dtype=tf.int32)
 input_mask = tf.keras.layers.Input(shape=(max_seq_length,), dtype=tf.int32)
 segment_ids = tf.keras.layers.Input(shape=(max_seq_length,), dtype=tf.int32)
 bert_inputs = [input_word_ids, input_mask, segment_ids]
bert_layer = hub.KerasLayer(bert_url, trainable=True)
 pooled_output, sequence_output = bert_layer(bert_inputs )<categorify>
train_df['WheelType'].fillna(value="WheelType_unk",inplace=True)
 test_df['WheelType'].fillna(value="WheelType_unk",inplace=True )
def bert_encode(texts, tokenizer, max_len=512):
all_tokens = []
all_masks = []
all_segments = []
for text in texts:
text = tokenizer.tokenize(text)
 text = text[:max_len-2]
input_sequence = ["[CLS]"] + text + ["[SEP]"]
pad_len = max_len - len(input_sequence)
 tokens = tokenizer.convert_tokens_to_ids(input_sequence)
 tokens += [0] * pad_len
pad_masks = [1] * len(input_sequence)+ [0] * pad_len
segment_ids = [0] * max_len
all_tokens.append(tokens)
 all_masks.append(pad_masks)
 all_segments.append(segment_ids)
 return np.array(all_tokens), np.array(all_masks), np.array(all_segments )<feature_engineering>
train_df['WheelType'].value_counts()
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case )<prepare_x_and_y>
train_df['Nationality'].value_counts()
x_train_bert = bert_encode(train['text'].values, tokenizer, max_len=max_seq_length)
 x_test_bert = bert_encode(test['text'].values, tokenizer, max_len=max_seq_length)
 y_train_bert = train['target'].values<choose_model_class>
test_df['Nationality'].value_counts()
dense = tf.keras.layers.Dense(64, activation='relu' )(sequence_output[:, 0, :])
 pred = tf.keras.layers.Dense(1, activation='sigmoid' )(dense)
 model_NN_bert = tf.keras.models.Model(inputs=bert_inputs, outputs=pred)
 optimizer = tf.keras.optimizers.Adam(LEARN_RATE)
 model_NN_bert.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
 model_NN_bert.summary()<train_model>
train_df['Nationality'].fillna(value="Nationality_unk",inplace=True)
 test_df['Nationality'].fillna(value="Nationality_unk",inplace=True )
model_NN_bert.fit(
 x_train_bert,
y_train_bert,
validation_split=SPLIT,
epochs=EPOCHS,
batch_size=BATCH_SIZE
)<predict_on_test>
train_df['Size'].value_counts()
bert_preds = model_NN_bert.predict(x_test_bert )<predict_on_test>
test_df['Size'].value_counts()
nb_preds_prob = model_NB.predict_proba(x_test_NB)[:, 1]<predict_on_test>
train_df['Size'].fillna(value="Size_unk",inplace=True)
 test_df['Size'].fillna(value="Size_unk",inplace=True )
nb_preds = model_NB.predict(x_test_NB)
 sample_submission['target'] = nb_preds
sample_submission.head()<data_type_conversions>
train_df['TopThreeAmericanName'].value_counts()
np.allclose(nb_preds, nb_preds_prob.round().astype(int))<save_to_csv>
test_df['TopThreeAmericanName'].value_counts()
sample_submission.to_csv('submission_NB.csv', index=False )<prepare_output>
train_df['TopThreeAmericanName'].fillna(value="Top_unk",inplace=True)
 test_df['TopThreeAmericanName'].fillna(value="Top_unk",inplace=True )
sample_submission['target'] = lstm_preds
sample_submission.head()<save_to_csv>
train_df['PRIMEUNIT'].value_counts()
sample_submission.to_csv('submission_NN.csv', index=False )<data_type_conversions>
test_df['PRIMEUNIT'].value_counts()
sample_submission['target'] = bert_preds.round().astype(int)
 sample_submission.head()<save_to_csv>
train_df['PRIMEUNIT'].fillna(value="Prime_unk",inplace=True)
 test_df['PRIMEUNIT'].fillna(value="Prime_unk",inplace=True )
sample_submission.to_csv('submission_NN_bert.csv', index=False )<prepare_output>
train_df['AUCGUART'].replace("AGREEN","GREEN",inplace=True)
 test_df['AUCGUART'].replace("ARED","RED",inplace=True)
bert_preds = bert_preds[:, 0]
bert_preds[:5]<define_variables>
train_df['AUCGUART'].fillna(value="AUC_unk",inplace=True)
 test_df['AUCGUART'].fillna(value="AUC_unk",inplace=True )
ensemble_preds =.5*nb_preds_prob +.5*bert_preds<data_type_conversions>
train_df.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice',
'MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice',
'MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice'],inplace=True,axis=1)
 test_df.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice',
'MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice',
'MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice'],inplace=True,axis=1 )
sample_submission['target'] = ensemble_preds.round().astype(int)
 sample_submission.head()<save_to_csv>
train_df.drop('PurchDate',axis=1,inplace=True)
 test_df.drop('PurchDate',axis=1,inplace=True )
sample_submission.to_csv('submission_ensemble.csv', index=False )<load_from_csv>
train_df.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!= 'object'
np.random.seed(33)
 train = pd.read_csv("/kaggle/input/titanic/train.csv")
 test = pd.read_csv("/kaggle/input/titanic/test.csv")
 m = {'m' : 1, 'f' : 0}
train['Sex'] = train['Sex'].str[0].map(m)
 test['Sex'] = test['Sex'].str[0].map(m)
 p_tr = train['Name'].str.split(',' ).map(lambda x: x[1] ).str.strip().str.split('.' ).map(lambda x: x[0])
 mapdict = {}
for i,v in enumerate(list(p_tr.unique())) :
mapdict[v] = i
train = pd.concat([train,p_tr.map(mapdict ).rename('P')], axis=1)
 p_ts = test['Name'].str.split(',' ).map(lambda x: x[1] ).str.strip().str.split('.' ).map(lambda x: x[0])
 test = pd.concat([test,p_ts.map(mapdict ).rename('P')], axis=1)
 m = {'child' : 0, 'adult' : 1}
train["AgeCat"]= pd.cut(train["Age"], bins=[0,15,max(train["Age"]+1)], labels=['child','adult'] ).map(m ).fillna(1 ).astype(int)
 test["AgeCat"]= pd.cut(test["Age"], bins=[0,15,max(test["Age"]+1)], labels=['child','adult'] ).map(m ).fillna(1 ).astype(int)
 Y = train["Survived"]
features = ["Pclass", "Sex", "Fare", "SibSp", "Parch", "P", "AgeCat"]
X = train[features]
X_test = test[features]
def normalize_dataframe(X):
mean = np.nanmean(X, axis=0)
 inds = np.where(np.isnan(X))
Xa = np.array(X)
 Xa[inds] = np.take(mean, inds[1])
 std = np.std(Xa, axis=0)
 norm_X =(Xa-mean)/std
return norm_X
norm_X = np.array(normalize_dataframe(X))
norm_X_test = np.array(normalize_dataframe(X_test))<normalization>
not_cat=train_df.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_df.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!= 'object']
imp = SimpleImputer(strategy="most_frequent")
 X_nona = imp.fit_transform(X)
 X_test_nona = imp.transform(X_test )<import_modules>
for i in not_cat:
maximum=np.max(train_df[i])
 train_df[i]=train_df[i]/maximum
maximum_test=np.max(test_df[i])
 test_df[i]=test_df[i]/maximum_test
from tensorflow.keras.layers import Input, Dense, BatchNormalization, Dropout, Activation
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import BinaryCrossentropy<define_search_model>
categorical=train_df.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_df.drop(['RefId','IsBadBuy'],axis=1 ).dtypes=='object']
def model(input_shape,
hidden_init = 'lecun_normal',
hidden_activation = 'selu'):
X_input = Input(shape=input_shape)
 X = Dense(64, kernel_initializer=hidden_init )(X_input)
 X = BatchNormalization()(X)
 X = Activation(hidden_activation )(X)
 X = Dense(64,kernel_initializer=hidden_init )(X)
 X = Activation(hidden_activation )(X)
 X = Dense(64, kernel_initializer=hidden_init )(X)
 X = BatchNormalization()(X)
 X = Activation(hidden_activation )(X)
 X = Dropout(0.7 )(X)
 X = Dense(10, kernel_initializer=hidden_init )(X)
 X = Dense(1, kernel_initializer=hidden_init, activation = 'sigmoid' )(X)
 model = Model(inputs=X_input, outputs=X)
 return model
model = model(X.shape[1])
 opt = Adam(lr=0.01, beta_1=0.9, beta_2=0.999, decay=0.01)
 model.compile(optimizer=opt, loss=BinaryCrossentropy(from_logits=True),
metrics=['accuracy'])
 K.clear_session()
tf.random.set_seed(33)
 model.fit(X_nona, Y, epochs=50, batch_size=30)
 predictions = model.predict(X_test_nona)
 predictions = np.greater(predictions,0.5 ).astype(int )<save_to_csv>
train_df[categorical[0]]
prS = pd.Series(predictions.reshape(predictions.shape[0]))
output = pd.concat([test['PassengerId'],prS], axis=1)
 output.columns=['PassengerId','Survived']
output.head()
output.to_csv('KerasNN_submission.csv', index=False)
 print("Your submission was successfully saved!" )<load_pretrained>
pd.get_dummies(train_df[categorical[0]] )
mnist = input_data.read_data_sets("/data/mnist", one_hot = True)
<define_variables>
for i in categorical:
dummies=pd.get_dummies(train_df[i])
 dummies.columns=str(i)+'_'+dummies.columns
train_df=pd.concat([train_df,dummies],axis=1)
 train_df.drop(i,inplace=True,axis=1)
 dummies=pd.get_dummies(test_df[i])
 dummies.columns=str(i)+'_'+dummies.columns
test_df=pd.concat([test_df,dummies],axis=1)
 test_df.drop(i,inplace=True,axis=1 )
for i in [random_crop,random_rot]:
a=random.sample(range(0,55000),22500*2)
 for j in a:
train_.append(i(mnist.train.images[j]))
labels_.append(mnist.train.labels[j] )<choose_model_class>
for i in train_df.drop('IsBadBuy',axis=1 ).columns:
if i not in test_df.columns:
test_df[i]=np.zeros(len(test_df))
def model(x):
x = layers.conv2d(x,32*2,(3,3),padding='VALID',activation_fn=None)
 x = tf.nn.relu(layers.batch_norm(x))
x = layers.conv2d(x,64*2,(3,3),padding='VALID',activation_fn=None)
 x = tf.nn.relu(layers.batch_norm(x))
x = layers.max_pool2d(x,2)
 x = layers.conv2d(x,64*2,(3,3),padding='SAME',activation_fn=None)
 x = tf.nn.relu(layers.batch_norm(x))
x = layers.conv2d(x,128*2,(3,3),padding='SAME',activation_fn=None)
 x = tf.nn.relu(layers.batch_norm(x))
x = layers.max_pool2d(x,2)
 x = layers.conv2d(x,128*2,(3,3),padding='VALID',activation_fn=None)
 x = tf.nn.relu(layers.batch_norm(x))
x = layers.conv2d(x,256*2,(3,3),padding='VALID',activation_fn=None)
 x = tf.nn.relu(layers.batch_norm(x))
x = layers.max_pool2d(x,2)
 x = layers.flatten(x)
 x = layers.fully_connected(x,64,activation_fn=None)
 x = tf.nn.relu(layers.batch_norm(x))
x = layers.fully_connected(x,10,activation_fn=None)
 x = tf.nn.softmax(layers.batch_norm(x))
return x
batch=200
x = tf.placeholder(tf.float32, shape=(None, 28, 28,1))
y = tf.placeholder(tf.int32,shape=(None,10))
models=[model(x)for _ in range(9)]
y_pred=tf.reduce_mean(models,0)
 loss=tf.reduce_mean(tf.losses.softmax_cross_entropy(y,logits=y_pred))
opt=tf.train.AdamOptimizer().minimize(loss)
 correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_pred, 1))<set_options>
for i in test_df.columns:
if i not in train_df.columns:
train_df[i]=np.zeros(len(train_df))
sess=tf.Session()
sess.run(tf.global_variables_initializer() )<prepare_x_and_y>
test_df=test_df[train_df.drop('IsBadBuy',axis=1 ).columns]
epoch=0
k=0
batch=200
for i in range(int(len(train_)/batch*15)) :
if k>=len(train_):
epoch+=1
k=0
x_=np.array(train_[k:k+batch])
 y_=np.array(labels_[k:k+batch])
 sess.run(opt,{x:x_.reshape(-1,28,28,1),y:y_})
 k+=batch
if i%72.5==0:
print("EPOCH: ",i/725 )<load_from_csv>
X=train_df.drop(['RefId','IsBadBuy'],axis=1)
 y=train_df['IsBadBuy']
csv=pd.read_csv(".. /input/test.csv")
 test=[list(csv.iloc[i])for i in range(len(csv)) ]
label=[]
for i in range(140):
label.extend(np.argmax(sess.run(y_pred,{x:np.reshape(test[i*200:(i+1)*200],(-1,28,28,1)) /255}),1))<save_to_csv>
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=42 )
csv=pd.DataFrame({'ImageId':range(1,28001),'Label':label})
 csv.to_csv("submission.csv",index=False )<load_from_csv>
from sklearn.neighbors import KNeighborsClassifier
img_rows, img_cols = 28, 28
X = pd.read_csv(".. /input/digit-recognizer/train.csv")
 y = X['label']
X = X.drop(['label'],axis = 1)
 X = X.values.reshape(-1, 28, 28, 1 ).astype('float32')/255
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42,stratify=y)
 test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 sample = pd.read_csv(".. /input/digit-recognizer/sample_submission.csv")
 print(sample.shape )<data_type_conversions>
from sklearn.neighbors import KNeighborsClassifier
X_unseen = test.values.reshape(-1, 28, 28, 1 ).astype('float32')/255<define_variables>
from sklearn.neighbors import KNeighborsClassifier
IMAGE_HT_WID = 28<import_modules>
KNN=KNeighborsClassifier(n_neighbors=11)
 KNN.fit(X_train,y_train )
import tensorflow as tf
from tensorflow import keras<choose_model_class>
KNN.score(X_test,y_test )
print(tf.__version__)
 IMG_SHAPE =(IMAGE_HT_WID, IMAGE_HT_WID, 1)
 model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32,(3, 3), activation=tf.nn.tanh, input_shape=IMG_SHAPE,padding='same'),
tf.keras.layers.GaussianNoise(0.1),
tf.keras.layers.Conv2D(32,(3, 3), activation=tf.nn.tanh),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout (.25),
tf.keras.layers.Conv2D(64,(3, 3), activation=tf.nn.tanh,padding='same'),
tf.keras.layers.Conv2D(64,(3, 3), activation=tf.nn.tanh),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout (.25),
tf.keras.layers.Conv2D(128,(3, 3), activation=tf.nn.tanh,padding='same'),
tf.keras.layers.Conv2D(128,(3, 3), activation=tf.nn.tanh),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout (.25),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dropout (.5),
tf.keras.layers.Dense(10, activation='softmax')
 ])
<choose_model_class>
predict=KNN.predict(test_df.drop('RefId',axis=1))
sgd = tf.keras.optimizers.SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)
 model.compile(loss='sparse_categorical_crossentropy', optimizer=sgd, metrics=['sparse_categorical_accuracy'])
 model.summary()<define_variables>
result=pd.DataFrame(data=predict,columns=['IsBadBuy'])
 result.head()
EPOCHS=32<choose_model_class>
result['RefId']=test_df['RefId']
result.set_index('RefId',inplace=True )
reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1,
patience=2, verbose=2, mode='auto',
min_lr=1e-6 )<choose_model_class>
result.head()
result.to_csv('Submission.csv' )